Skip to content

Commit c8c681d

Browse files
committed
got find_by and find working on scopes - part of #113
1 parent a6ef9d8 commit c8c681d

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,6 @@ def apply_scope(name, *vector)
252252
collection = build_child_scope(description, *description.name, *vector)
253253
collection.reload_from_db if name == "#{description.name}!"
254254
collection
255-
rescue
256-
debugger
257-
nil
258255
end
259256

260257
def child_scopes
@@ -549,12 +546,16 @@ def empty?
549546
end
550547

551548
def method_missing(method, *args, &block)
552-
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
553556
all.send(method, *args, &block)
554557
elsif ScopeDescription.find(@target_klass, method)
555558
apply_scope(method, *args)
556-
elsif args.count == 1 && method.start_with?('find_by_')
557-
apply_scope(:___hyperstack_internal_scoped_find_by, method.sub(/^find_by_/, '') => args.first).first
558559
elsif @target_klass.respond_to?(method) && ScopeDescription.find(@target_klass, "_#{method}")
559560
apply_scope("_#{method}", *args).first
560561
else

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

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +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)
143-
expect_promise do
144-
Hyperstack::Model.load do
145-
Todo.completed.find_by_title('todo 3').present?
146-
end
147-
end.to be_falsy
148176
end
149-
150177
end

0 commit comments

Comments
 (0)