Skip to content

Commit fab03b7

Browse files
committed
closes #113
1 parent 97c018a commit fab03b7

File tree

6 files changed

+122
-100
lines changed

6 files changed

+122
-100
lines changed

ruby/hyper-model/lib/active_record_base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ def __hyperstack_secure_attributes(acting_user)
329329
%i[limit offset].each do |scope|
330330
regulate_scope(scope) {}
331331
end
332+
333+
finder_method :__hyperstack_internal_scoped_find_by do |hash|
334+
find_by(hash)
335+
end
332336
end
333337
end
334338

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ class Base
66

77
scope :limit, ->() {}
88
scope :offset, ->() {}
9+
10+
finder_method :__hyperstack_internal_scoped_find_by
911
end
1012
end

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ def scope(name, *args)
213213
singleton_class.send(:define_method, name) do |*vargs|
214214
all.build_child_scope(scope_description, *name, *vargs)
215215
end
216-
# singleton_class.send(:define_method, "#{name}=") do |_collection|
217-
# raise 'NO LONGER IMPLEMENTED - DOESNT PLAY WELL WITH SYNCHROMESH'
218-
# end
219216
end
220217

221218
def default_scope(*args, &block)
@@ -261,10 +258,10 @@ def unscoped
261258
end
262259

263260
def finder_method(name)
264-
ReactiveRecord::ScopeDescription.new(self, "_#{name}", {}) # was adding _ to front
261+
ReactiveRecord::ScopeDescription.new(self, "_#{name}", {})
265262
[name, "#{name}!"].each do |method|
266263
singleton_class.send(:define_method, method) do |*vargs|
267-
all.apply_scope("_#{method}", *vargs).first # was adding _ to front
264+
all.apply_scope("_#{method}", *vargs).first
268265
end
269266
end
270267
end

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ 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
255258
end
256259

257260
def child_scopes
@@ -551,7 +554,7 @@ def method_missing(method, *args, &block)
551554
elsif ScopeDescription.find(@target_klass, method)
552555
apply_scope(method, *args)
553556
elsif args.count == 1 && method.start_with?('find_by_')
554-
apply_scope(:find_by, method.sub(/^find_by_/, '') => args.first)
557+
apply_scope(:___hyperstack_internal_scoped_find_by, method.sub(/^find_by_/, '') => args.first).first
555558
elsif @target_klass.respond_to?(method) && ScopeDescription.find(@target_klass, "_#{method}")
556559
apply_scope("_#{method}", *args).first
557560
else

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,23 @@ class TestComponent77 < HyperComponent
123123
page.should have_content(todo.title)
124124
end
125125
end
126+
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)
131+
end
132+
Todo.scope :completed, -> () { where(completed: true) }
133+
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+
end
144+
126145
end
Lines changed: 91 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,123 @@
11
require 'spec_helper'
2-
#Opal::RSpec::Runner.autorun
32

4-
class BaseClass < ActiveRecord::Base
5-
end
6-
7-
class SubClass < BaseClass
8-
end
9-
10-
class Funky < ActiveRecord::Base
11-
self.primary_key = :funky_id
12-
self.inheritance_column = :funky_type
13-
end
14-
15-
class BelongsTo < ActiveRecord::Base
16-
belongs_to :has_many
17-
belongs_to :has_one
18-
belongs_to :best_friend, class_name: "HasMany", foreign_key: :bf_id
19-
end
20-
21-
class HasMany < ActiveRecord::Base
22-
has_many :belongs_to
23-
has_many :best_friends, class_name: "BelongsTo", foreign_key: :bf_id
24-
end
25-
26-
class HasOne < ActiveRecord::Base
27-
has_one :belongs_to
28-
end
29-
30-
class Scoped < ActiveRecord::Base
31-
scope :only_those_guys, -> () {}
32-
end
3+
# this never completed conversion from old react.rb days...
4+
# all this content is tested elsewhere, but it would be nice
5+
# to have these basic unit tests executing as well.
336

34-
describe "ActiveRecord" do
7+
describe 'ActiveRecord client side basics', js: true do
358

36-
before(:all) { Hyperstack::Component::IsomorphicHelpers.load_context }
37-
38-
after(:each) { React::API.clear_component_class_cache }
39-
40-
# uncomment if you are having trouble with tests failing. One non-async test must pass for things to work
41-
42-
# describe "a passing dummy test" do
43-
# it "passes" do
44-
# expect(true).to be(true)
45-
# end
46-
# end
47-
48-
describe "reactive_record active_record base methods" do
49-
50-
it "will find the base class" do
51-
expect(SubClass.base_class).to eq(BaseClass)
9+
before(:all) do
10+
class ActiveRecord::Base
11+
class << self
12+
def public_columns_hash
13+
@public_columns_hash ||= {}
14+
end
15+
end
5216
end
5317

54-
it "knows the primary key" do
55-
expect(BaseClass.primary_key).to eq(:id)
18+
class BaseClass < ActiveRecord::Base
5619
end
5720

58-
it "can override the primary key" do
59-
expect(Funky.primary_key).to eq(:funky_id)
21+
class SubClass < BaseClass
6022
end
6123

62-
it "knows the inheritance column" do
63-
expect(BaseClass.inheritance_column).to eq(:type)
24+
class Funky < ActiveRecord::Base
25+
self.primary_key = :funky_id
26+
self.inheritance_column = :funky_type
6427
end
6528

66-
it "can override the inheritance column" do
67-
expect(Funky.inheritance_column).to eq(:funky_type)
29+
class BelongsTo < ActiveRecord::Base
30+
belongs_to :has_many
31+
belongs_to :has_one
32+
belongs_to :best_friend, class_name: "HasMany", foreign_key: :bf_id
6833
end
6934

70-
it "knows the model name" do
71-
expect(BaseClass.model_name).to eq("BaseClass")
35+
class HasMany < ActiveRecord::Base
36+
has_many :belongs_to
37+
has_many :best_friends, class_name: "BelongsTo", foreign_key: :bf_id
7238
end
7339

74-
it "can find a record by id" do
75-
expect(BaseClass.find(12).id).to eq(12)
40+
class HasOne < ActiveRecord::Base
41+
has_one :belongs_to
7642
end
7743

78-
it "has a find_by_xxx method" do
79-
expect(BaseClass.find_by_xxx("beer").xxx).to eq("beer")
44+
class Scoped < ActiveRecord::Base
45+
scope :only_those_guys, -> () {}
8046
end
8147

82-
it "will correctly infer the model type from the inheritance column" do
83-
expect(BaseClass.find_by_type("SubClass").class).to eq(SubClass)
84-
expect(BaseClass.find_by_type(nil).class).to eq(BaseClass)
85-
end
48+
end
8649

87-
it "can have a has_many association" do
88-
expect(HasMany.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_many).klass).to eq(HasMany)
89-
end
50+
it "will find the base class" do
51+
expect(SubClass.base_class).to eq(BaseClass)
52+
end
9053

91-
it "can have a has_one association" do
92-
expect(HasOne.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_one).klass).to eq(HasOne)
93-
end
54+
it "knows the primary key" do
55+
expect(BaseClass.primary_key).to eq(:id)
56+
end
9457

95-
it "can override the class and foreign_key values when creating an association" do
96-
reflection = HasMany.reflect_on_association(:best_friends)
97-
expect(reflection.klass).to eq(BelongsTo)
98-
expect(reflection.association_foreign_key).to eq(:bf_id)
99-
end
58+
it "can override the primary key" do
59+
expect(Funky.primary_key).to eq(:funky_id)
60+
end
10061

101-
it "can have a scoping method" do
102-
expect(Scoped.only_those_guys.respond_to? :all).to be_truthy
103-
end
62+
it "knows the inheritance column" do
63+
expect(BaseClass.inheritance_column).to eq(:type)
64+
end
10465

105-
it "can type check parameters" do
106-
expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n, :validate_only)).to be(true)
107-
end
66+
it "can override the inheritance column" do
67+
expect(Funky.inheritance_column).to eq(:funky_type)
68+
end
10869

109-
it "can type check parameters with native wrappers" do
110-
expect(SubClass._react_param_conversion(Native({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n), :validate_only)).to be(true)
111-
end
70+
it "knows the model name" do
71+
expect(BaseClass.model_name).to eq("BaseClass")
72+
end
11273

113-
it "will fail type checking if type does not match" do
114-
expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: nil, id: 123}.to_n, :validate_only)).to be_falsy
115-
end
74+
it "can find a record by id" do
75+
expect(BaseClass.find(12).id).to eq(12)
76+
end
11677

117-
it "will convert a hash to an instance" do
118-
ar = SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n)
119-
expect(ar.attr1).to eq(1)
120-
expect(ar.attr2).to eq(2)
121-
expect(ar.id).to eq(123)
122-
end
78+
it "has a find_by_xxx method" do
79+
expect(BaseClass.find_by_xxx("beer").xxx).to eq("beer")
80+
end
12381

82+
it "will correctly infer the model type from the inheritance column" do
83+
expect(BaseClass.find_by_type("SubClass").class).to eq(SubClass)
84+
expect(BaseClass.find_by_type(nil).class).to eq(BaseClass)
12485
end
12586

87+
it "can have a has_many association" do
88+
expect(HasMany.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_many).klass).to eq(HasMany)
89+
end
90+
91+
it "can have a has_one association" do
92+
expect(HasOne.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_one).klass).to eq(HasOne)
93+
end
94+
95+
it "can override the class and foreign_key values when creating an association" do
96+
reflection = HasMany.reflect_on_association(:best_friends)
97+
expect(reflection.klass).to eq(BelongsTo)
98+
expect(reflection.association_foreign_key).to eq(:bf_id)
99+
end
100+
101+
it "can have a scoping method" do
102+
expect(Scoped.only_those_guys.respond_to? :all).to be_truthy
103+
end
104+
105+
it "can type check parameters" do
106+
expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n, :validate_only)).to be(true)
107+
end
108+
109+
it "can type check parameters with native wrappers" do
110+
expect(SubClass._react_param_conversion(Native({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n), :validate_only)).to be(true)
111+
end
112+
113+
it "will fail type checking if type does not match" do
114+
expect(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: nil, id: 123}.to_n, :validate_only)).to be_falsy
115+
end
116+
117+
it "will convert a hash to an instance" do
118+
ar = SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n)
119+
expect(ar.attr1).to eq(1)
120+
expect(ar.attr2).to eq(2)
121+
expect(ar.id).to eq(123)
122+
end
126123
end

0 commit comments

Comments
 (0)