|
| 1 | +require 'spec_helper' |
| 2 | +require 'rspec-steps' |
| 3 | + |
| 4 | +RSpec::Steps.steps 'the where method and class delegation', js: true do |
| 5 | + |
| 6 | + before(:each) do |
| 7 | + require 'pusher' |
| 8 | + require 'pusher-fake' |
| 9 | + Pusher.app_id = "MY_TEST_ID" |
| 10 | + Pusher.key = "MY_TEST_KEY" |
| 11 | + Pusher.secret = "MY_TEST_SECRET" |
| 12 | + require "pusher-fake/support/base" |
| 13 | + |
| 14 | + Hyperstack.configuration do |config| |
| 15 | + config.transport = :pusher |
| 16 | + config.channel_prefix = "synchromesh" |
| 17 | + config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret, use_tls: false}.merge(PusherFake.configuration.web_options) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + before(:step) do |
| 22 | + stub_const 'TestApplicationPolicy', Class.new |
| 23 | + TestApplicationPolicy.class_eval do |
| 24 | + always_allow_connection |
| 25 | + regulate_all_broadcasts { |policy| policy.send_all } |
| 26 | + allow_change(to: :all, on: [:create, :update, :destroy]) { true } |
| 27 | + end |
| 28 | + ApplicationController.acting_user = nil |
| 29 | + isomorphic do |
| 30 | + User.alias_attribute :surname, :last_name |
| 31 | + User.class_eval do |
| 32 | + def self.with_size(attr, size) |
| 33 | + where("LENGTH(#{attr}) = ?", size) |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + @user1 = User.create(first_name: "Mitch", last_name: "VanDuyn") |
| 39 | + User.create(first_name: "Joe", last_name: "Blow") |
| 40 | + @user2 = User.create(first_name: "Jan", last_name: "VanDuyn") |
| 41 | + User.create(first_name: "Ralph", last_name: "HooBo") |
| 42 | + end |
| 43 | + |
| 44 | + it "can take a hash like value" do |
| 45 | + expect do |
| 46 | + ReactiveRecord.load { User.where(surname: "VanDuyn").pluck(:id, :first_name) } |
| 47 | + end.on_client_to eq User.where(surname: "VanDuyn").pluck(:id, :first_name) |
| 48 | + end |
| 49 | + |
| 50 | + it "and will update the collection on the client " do |
| 51 | + User.create(first_name: "Paul", last_name: "VanDuyn") |
| 52 | + expect do |
| 53 | + User.where(surname: "VanDuyn").pluck(:id, :first_name) |
| 54 | + end.on_client_to eq User.where(surname: "VanDuyn").pluck(:id, :first_name) |
| 55 | + end |
| 56 | + |
| 57 | + it "or it can take SQL plus params" do |
| 58 | + expect do |
| 59 | + Hyperstack::Model.load { User.where("first_name LIKE ?", "J%").pluck(:first_name, :surname) } |
| 60 | + end.on_client_to eq User.where("first_name LIKE ?", "J%").pluck(:first_name, :surname) |
| 61 | + end |
| 62 | + |
| 63 | + it "class methods will be called from collections" do |
| 64 | + expect do |
| 65 | + Hyperstack::Model.load { User.where(last_name: 'VanDuyn').with_size(:first_name, 3).pluck('first_name') } |
| 66 | + end.on_client_to eq User.where(last_name: 'VanDuyn').with_size(:first_name, 3).pluck('first_name') |
| 67 | + end |
| 68 | + |
| 69 | + it "where-s can be chained (cause they are just class level methods after all)" do |
| 70 | + expect do |
| 71 | + Hyperstack::Model.load { User.where(last_name: 'VanDuyn').where(first_name: 'Jan').pluck(:id) } |
| 72 | + end.on_client_to eq User.where(last_name: 'VanDuyn', first_name: 'Jan').pluck(:id) |
| 73 | + end |
| 74 | + |
| 75 | +end |
0 commit comments