Skip to content

Commit 5948569

Browse files
committed
Merge branch 'issue-113' into edge
2 parents 7d299d5 + c8c681d commit 5948569

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

ruby/hyper-model/lib/active_record_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def finder_method(name, &block)
8787
this.acting_user = acting_user
8888
# returns a PsuedoRelationArray which will respond to the
8989
# __secure_collection_check method
90-
ReactiveRecordPsuedoRelationArray.new([this.instance_exec(*args, &block)])
90+
ReactiveRecordPsuedoRelationArray.new([*this.instance_exec(*args, &block)])
9191
ensure
9292
this.acting_user = old
9393
end

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/collection.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,16 @@ def empty?
546546
end
547547

548548
def method_missing(method, *args, &block)
549-
if [].respond_to? method
549+
if args.count == 1 && method.start_with?('find_by_')
550+
apply_scope(:___hyperstack_internal_scoped_find_by, method.sub(/^find_by_/, '') => args.first).first
551+
elsif args.count == 1 && method == :find_by
552+
apply_scope(:___hyperstack_internal_scoped_find_by, args.first).first
553+
elsif args.count == 1 && method == :find
554+
apply_scope(:___hyperstack_internal_scoped_find_by, @target_klass.primary_key => args.first).first
555+
elsif [].respond_to? method
550556
all.send(method, *args, &block)
551557
elsif ScopeDescription.find(@target_klass, method)
552558
apply_scope(method, *args)
553-
elsif args.count == 1 && method.start_with?('find_by_')
554-
apply_scope(:___hyperstack_internal_scoped_find_by, method.sub(/^find_by_/, '') => args.first).first
555559
elsif @target_klass.respond_to?(method) && ScopeDescription.find(@target_klass, "_#{method}")
556560
apply_scope("_#{method}", *args).first
557561
else

ruby/hyper-model/spec/batch3/edge_cases_spec.rb

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,54 @@ class TestComponent77 < HyperComponent
124124
end
125125
end
126126

127-
it 'can use find_by method on scopes' do
128-
isomorphic do
129-
Todo.finder_method :with_title do |title|
130-
find_by_title(title)
127+
describe 'can use finder methods on scopes' do
128+
before(:each) do
129+
isomorphic do
130+
Todo.finder_method :with_title do |title|
131+
find_by_title(title)
132+
end
133+
Todo.scope :completed, -> () { where(completed: true) }
131134
end
132-
Todo.scope :completed, -> () { where(completed: true) }
135+
FactoryBot.create(:todo, title: 'todo 1', completed: true)
136+
FactoryBot.create(:todo, title: 'todo 2', completed: true)
137+
FactoryBot.create(:todo, title: 'todo 1', completed: false)
138+
FactoryBot.create(:todo, title: 'todo 2', completed: false)
139+
end
140+
it 'find_by_xxx' do
141+
expect_promise do
142+
Hyperstack::Model.load do
143+
Todo.completed.find_by_title('todo 2').id
144+
end
145+
end.to eq(Todo.completed.find_by_title('todo 2').id)
146+
expect_promise do
147+
Hyperstack::Model.load do
148+
Todo.completed.find_by_title('todo 3')
149+
end
150+
end.to be_falsy
151+
end
152+
it 'find' do
153+
expect_promise do
154+
Hyperstack::Model.load do
155+
Todo.completed.find(2).title
156+
end
157+
end.to eq(Todo.completed.find(2).title)
158+
expect_promise do
159+
Hyperstack::Model.load do
160+
Todo.completed.find(3)
161+
end
162+
end.to be_falsy
163+
end
164+
it 'find_by' do
165+
expect_promise do
166+
Hyperstack::Model.load do
167+
Todo.completed.find_by(title: 'todo 2').id
168+
end
169+
end.to eq(Todo.completed.find_by(title: 'todo 2').id)
170+
expect_promise do
171+
Hyperstack::Model.load do
172+
Todo.completed.find_by(title: 'todo 3')
173+
end
174+
end.to be_falsy
133175
end
134-
FactoryBot.create(:todo, title: 'todo 1', completed: true)
135-
FactoryBot.create(:todo, title: 'todo 2', completed: true)
136-
FactoryBot.create(:todo, title: 'todo 1', completed: false)
137-
FactoryBot.create(:todo, title: 'todo 2', completed: false)
138-
expect_promise do
139-
Hyperstack::Model.load do
140-
Todo.completed.find_by_title('todo 2').id
141-
end
142-
end.to eq(Todo.completed.find_by_title('todo 2').id)
143176
end
144-
145177
end

0 commit comments

Comments
 (0)