Skip to content

Commit d4a74fb

Browse files
committed
fixes
1 parent 657cd77 commit d4a74fb

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def initialize(owner_class, macro, name, options = {})
5050
unless options[:polymorphic]
5151
@klass_name = options[:class_name] || (collection? && name.camelize.singularize) || name.camelize
5252
end
53+
54+
if @klass_name < ActiveRecord::Base
55+
@klass = @klass_name
56+
@klass_name = @klass_name.name
57+
end rescue nil
58+
5359
@association_foreign_key =
5460
options[:foreign_key] ||
5561
(macro == :belongs_to && "#{name}_id") ||
@@ -176,8 +182,10 @@ def find_inverse(model) # private
176182

177183
def klass(model = nil)
178184
@klass ||= Object.const_get(@klass_name) if @klass_name
179-
raise "model is not correct class" if @klass && model && model.class != @klass
180-
raise "no model supplied for polymorphic relationship" unless @klass || model
185+
if @klass && model && !(model.class <= @klass)
186+
raise "internal error: provided model #{model} is not subclass of #{@klass}"
187+
end
188+
raise 'no model supplied for polymorphic relationship' unless @klass || model
181189
@klass || model.class
182190
end
183191

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def add_new_inverse_attribute(association, orig, model)
167167
model.backing_record.push_onto_collection(@model, inverse_association, @ar_instance)
168168
else
169169
inverse_attr = inverse_association.attribute
170-
value.attributes[inverse_attr] = @ar_instance
170+
model.attributes[inverse_attr] = @ar_instance
171171
return if data_loading?
172172
Hyperstack::Internal::State::Variable.set(model.backing_record, inverse_attr, @ar_instance)
173173
end
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
require 'spec_helper'
2+
require 'test_components'
3+
require 'rspec-steps'
4+
5+
describe "one to one relationships", js: true do
6+
7+
before(:all) do
8+
require 'pusher'
9+
require 'pusher-fake'
10+
Pusher.app_id = "MY_TEST_ID"
11+
Pusher.key = "MY_TEST_KEY"
12+
Pusher.secret = "MY_TEST_SECRET"
13+
require "pusher-fake/support/base"
14+
15+
Hyperstack.configuration do |config|
16+
config.transport = :pusher
17+
config.channel_prefix = "synchromesh"
18+
config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options)
19+
end
20+
21+
class ActiveRecord::Base
22+
class << self
23+
def public_columns_hash
24+
@public_columns_hash ||= {}
25+
end
26+
end
27+
end
28+
end
29+
30+
before(:each) do
31+
32+
# spec_helper resets the policy system after each test so we have to setup
33+
# before each test
34+
stub_const 'TestApplicationPolicy', Class.new
35+
TestApplicationPolicy.class_eval do
36+
always_allow_connection
37+
regulate_all_broadcasts { |policy| policy.send_all }
38+
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
39+
end
40+
41+
size_window(:small, :large)
42+
43+
isomorphic do
44+
45+
class Parent < ActiveRecord::Base
46+
def self.build_tables
47+
connection.create_table :parents, force: true do |t|
48+
t.string :name
49+
t.timestamps
50+
end
51+
ActiveRecord::Base.public_columns_hash[name] = columns_hash
52+
end
53+
54+
has_one :child
55+
end
56+
57+
class Child < ActiveRecord::Base
58+
def self.build_tables
59+
connection.create_table :children, force: true do |t|
60+
t.string :name
61+
t.integer :parent_id
62+
t.timestamps
63+
end
64+
ActiveRecord::Base.public_columns_hash[name] = columns_hash
65+
end
66+
belongs_to :parent
67+
end
68+
end
69+
70+
[Parent, Child].each { |klass| klass.build_tables }
71+
72+
end
73+
74+
def compare_to_server(model, expression, expected_result, load=true)
75+
server_side = eval("#{model.class}.find(#{model.id}).#{expression}")
76+
expect(server_side).to eq(expected_result)
77+
be_expected_result = expected_result.is_a?(Array) ? contain_exactly(*expected_result) : eq(expected_result)
78+
if load
79+
expect_promise("Hyperstack::Model.load { #{model.class}.find(#{model.id}).#{expression} }")
80+
.to be_expected_result
81+
else
82+
wait_for_ajax
83+
expect_evaluate_ruby("#{model.class}.find(#{model.id}).#{expression}").to be_expected_result
84+
end
85+
end
86+
87+
before(:each) do
88+
@parent1 = Parent.create(name: 'parent1')
89+
@child1 = Child.create(name: 'child1', parent: @parent1)
90+
end
91+
92+
it 'will load the parent' do
93+
compare_to_server @child1, 'parent.name', 'parent1'
94+
end
95+
96+
it 'will load the child' do
97+
compare_to_server @parent1, 'child.name', 'child1'
98+
end
99+
end

ruby/hyper-model/spec/batch7/poly_assoc_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ def self.build_tables
9292
has_one :picture, as: :imageable
9393
end
9494

95-
class ReactiveRecord::Collection
96-
end if RUBY_ENGINE == 'opal'
97-
9895
class Membership < ActiveRecord::Base
9996
def self.build_tables
10097
connection.create_table :memberships, force: true do |t|

0 commit comments

Comments
 (0)