-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
138 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Add pluck to enumerable... its already done for us in rails 5+ | ||
module Enumerable | ||
def pluck(key) | ||
map { |element| element[key] } | ||
def pluck(*keys) | ||
map { |element| keys.map { |key| element[key] } } | ||
.flatten(keys.count > 1 ? 0 : 1) | ||
end | ||
end unless Enumerable.method_defined? :pluck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
ruby/hyper-model/spec/batch1/misc/where_and_class_method_delegation_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'spec_helper' | ||
require 'rspec-steps' | ||
|
||
RSpec::Steps.steps 'the where method and class delegation', js: true do | ||
|
||
before(:each) do | ||
require 'pusher' | ||
require 'pusher-fake' | ||
Pusher.app_id = "MY_TEST_ID" | ||
Pusher.key = "MY_TEST_KEY" | ||
Pusher.secret = "MY_TEST_SECRET" | ||
require "pusher-fake/support/base" | ||
|
||
Hyperstack.configuration do |config| | ||
config.transport = :pusher | ||
config.channel_prefix = "synchromesh" | ||
config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret, use_tls: false}.merge(PusherFake.configuration.web_options) | ||
end | ||
end | ||
|
||
before(:step) do | ||
stub_const 'TestApplicationPolicy', Class.new | ||
TestApplicationPolicy.class_eval do | ||
always_allow_connection | ||
regulate_all_broadcasts { |policy| policy.send_all } | ||
allow_change(to: :all, on: [:create, :update, :destroy]) { true } | ||
end | ||
ApplicationController.acting_user = nil | ||
isomorphic do | ||
User.alias_attribute :surname, :last_name | ||
User.class_eval do | ||
def self.with_size(attr, size) | ||
where("LENGTH(#{attr}) = ?", size) | ||
end | ||
end | ||
end | ||
|
||
@user1 = User.create(first_name: "Mitch", last_name: "VanDuyn") | ||
User.create(first_name: "Joe", last_name: "Blow") | ||
@user2 = User.create(first_name: "Jan", last_name: "VanDuyn") | ||
User.create(first_name: "Ralph", last_name: "HooBo") | ||
end | ||
|
||
it "can take a hash like value" do | ||
expect do | ||
ReactiveRecord.load { User.where(surname: "VanDuyn").pluck(:id, :first_name) } | ||
end.on_client_to eq User.where(surname: "VanDuyn").pluck(:id, :first_name) | ||
end | ||
|
||
it "and will update the collection on the client " do | ||
User.create(first_name: "Paul", last_name: "VanDuyn") | ||
expect do | ||
User.where(surname: "VanDuyn").pluck(:id, :first_name) | ||
end.on_client_to eq User.where(surname: "VanDuyn").pluck(:id, :first_name) | ||
end | ||
|
||
it "or it can take SQL plus params" do | ||
expect do | ||
Hyperstack::Model.load { User.where("first_name LIKE ?", "J%").pluck(:first_name, :surname) } | ||
end.on_client_to eq User.where("first_name LIKE ?", "J%").pluck(:first_name, :surname) | ||
end | ||
|
||
it "class methods will be called from collections" do | ||
expect do | ||
Hyperstack::Model.load { User.where(last_name: 'VanDuyn').with_size(:first_name, 3).pluck('first_name') } | ||
end.on_client_to eq User.where(last_name: 'VanDuyn').with_size(:first_name, 3).pluck('first_name') | ||
end | ||
|
||
it "where-s can be chained (cause they are just class level methods after all)" do | ||
expect do | ||
Hyperstack::Model.load { User.where(last_name: 'VanDuyn').where(first_name: 'Jan').pluck(:id) } | ||
end.on_client_to eq User.where(last_name: 'VanDuyn', first_name: 'Jan').pluck(:id) | ||
end | ||
|
||
end |