Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit d2ce6cc

Browse files
committed
fixed problem with bang method due to refactoring - all specs pass
1 parent 200c711 commit d2ce6cc

File tree

11 files changed

+349
-9
lines changed

11 files changed

+349
-9
lines changed

Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ require "rspec/core/rake_task"
44

55

66
task :spec do
7-
(1..6).each { |batch| Rake::Task["spec:batch#{batch}"].invoke rescue nil }
7+
(1..7).each { |batch| Rake::Task["spec:batch#{batch}"].invoke rescue nil }
88
end
99

1010
namespace :spec do
1111
task :prepare do
1212
sh %{bundle update}
1313
sh %{cd spec/test_app; bundle update; bundle exec rails db:setup} # may need ;bundle exec rails db:setup as well
1414
end
15-
(1..6).each do |batch|
15+
(1..7).each do |batch|
1616
RSpec::Core::RakeTask.new(:"batch#{batch}") do |t|
1717
t.pattern = "spec/batch#{batch}/**/*_spec.rb"
1818
end

lib/reactive_record/active_record/reactive_record/setters.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,27 @@ def set_common(attr, value)
8181
end
8282

8383
def set_attribute_change_status_and_notify(attr, changed, new_value)
84-
@attributes[attr] = new_value
85-
change_status_and_notify_helper(attr, changed) do |had_key, current_value|
86-
if !data_loading? ||
87-
(on_opal_client? && had_key && current_value.loaded? && current_value != new_value)
88-
React::State.set_state(self, attr, new_value, data_loading?)
84+
if @virgin
85+
@attributes[attr] = new_value
86+
else
87+
change_status_and_notify_helper(attr, changed) do |had_key, current_value|
88+
@attributes[attr] = new_value
89+
if !data_loading? ||
90+
(on_opal_client? && had_key && current_value.loaded? && current_value != new_value)
91+
React::State.set_state(self, attr, new_value, data_loading?)
92+
end
8993
end
9094
end
9195
end
9296

9397
def set_change_status_and_notify_only(attr, changed)
98+
return if @virgin
9499
change_status_and_notify_helper(attr, changed) do
95100
React::State.set_state(self, attr, nil) unless data_loading?
96101
end
97102
end
98103

99104
def change_status_and_notify_helper(attr, changed)
100-
return if @virgin
101105
empty_before = changed_attributes.empty?
102106
if !changed
103107
changed_attributes.delete(attr)

spec/batch1/misc/ar_basics_tbdspec.rb

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
require 'spec_helper'
2+
require 'test_components'
3+
require 'rspec-steps'
4+
5+
RSpec::Steps.steps "AR Client Basics", js: true do
6+
7+
before(:all) do
8+
on_client do
9+
10+
class BaseClass < ActiveRecord::Base
11+
self.abstract_class = true
12+
end
13+
14+
class SubClass < BaseClass
15+
end
16+
17+
class SubSubClass < SubClass
18+
end
19+
20+
class Funky < ActiveRecord::Base
21+
self.primary_key = :funky_id
22+
self.inheritance_column = :funky_type
23+
end
24+
25+
class BelongsTo < ActiveRecord::Base
26+
belongs_to :has_many
27+
belongs_to :has_one
28+
belongs_to :best_friend, class_name: "HasMany", foreign_key: :bf_id
29+
end
30+
31+
class HasMany < ActiveRecord::Base
32+
has_many :belongs_to
33+
has_many :best_friends, class_name: "BelongsTo", foreign_key: :bf_id
34+
end
35+
36+
class HasOne < ActiveRecord::Base
37+
has_one :belongs_to
38+
end
39+
40+
class Scoped < ActiveRecord::Base
41+
scope :only_those_guys, -> () {}
42+
end
43+
end
44+
end
45+
46+
it "will find the base class" do
47+
expect_evaluate_ruby("SubSubClass.base_class").to eq("SubClass")
48+
end
49+
50+
it "knows the primary key" do
51+
expect_evaluate_ruby("BaseClass.primary_key").to eq("id")
52+
end
53+
54+
it "can override the primary key" do
55+
expect_evaluate_ruby("Funky.primary_key").to eq("funky_id")
56+
end
57+
58+
it "knows the inheritance column" do
59+
expect_evaluate_ruby('BaseClass.inheritance_column').to eq('type')
60+
end
61+
62+
it "can override the inheritance column" do
63+
expect_evaluate_ruby('Funky.inheritance_column').to eq('funky_type')
64+
end
65+
66+
it "knows the model name" do
67+
expect_evaluate_ruby('BaseClass.model_name').to eq("BaseClass")
68+
end
69+
70+
it "can find a record by id" do
71+
expect_evaluate_ruby('BaseClass.find(12).id').to eq(12)
72+
end
73+
74+
it "has a find_by_xxx method" do
75+
expect_evaluate_ruby('BaseClass.find_by_xxx("beer").xxx').to eq("beer")
76+
end
77+
78+
it "will correctly infer the model type from the inheritance column" do
79+
expect_evaluate_ruby('BaseClass.find_by_type("SubClass").class').to eq('SubClass')
80+
expect_evaluate_ruby('BaseClass.find_by_type(nil).class').to eq('BaseClass')
81+
end
82+
83+
it "can have a has_many association" do
84+
expect_evaluate_ruby(HasMany.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_many).klass).to eq(HasMany)
85+
end
86+
87+
it "can have a has_one association" do
88+
expect_evaluate_ruby(HasOne.reflect_on_association(:belongs_to).klass.reflect_on_association(:has_one).klass).to eq(HasOne)
89+
end
90+
91+
it "can override the class and foreign_key values when creating an association" do
92+
reflection = HasMany.reflect_on_association(:best_friends)
93+
expect_evaluate_ruby(reflection.klass).to eq(BelongsTo)
94+
expect_evaluate_ruby(reflection.association_foreign_key).to eq(:bf_id)
95+
end
96+
97+
it "can have a scoping method" do
98+
expect_evaluate_ruby(Scoped.only_those_guys.respond_to? :all).to be_truthy
99+
end
100+
101+
it "can type check parameters" do
102+
expect_evaluate_ruby(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n, :validate_only)).to be(true)
103+
end
104+
105+
it "can type check parameters with native wrappers" do
106+
expect_evaluate_ruby(SubClass._react_param_conversion(Native({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n), :validate_only)).to be(true)
107+
end
108+
109+
it "will fail type checking if type does not match" do
110+
expect_evaluate_ruby(SubClass._react_param_conversion({attr1: 1, attr2: 2, type: nil, id: 123}.to_n, :validate_only)).to be_falsy
111+
end
112+
113+
it "will convert a hash to an instance" do
114+
ar = SubClass._react_param_conversion({attr1: 1, attr2: 2, type: "SubClass", id: 123}.to_n)
115+
expect_evaluate_ruby(ar.attr1).to eq(1)
116+
expect_evaluate_ruby(ar.attr2).to eq(2)
117+
expect_evaluate_ruby(ar.id).to eq(123)
118+
end
119+
120+
end

spec/batch1/misc/validate_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'rspec-steps'
44

55

6-
RSpec::Steps.steps "access like a hash", js: true do
6+
RSpec::Steps.steps "validate and valid? methods", js: true do
77

88
before(:step) do
99
# spec_helper resets the policy system after each test so we have to setup
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
require 'spec_helper'
2+
require 'rspec-steps'
3+
4+
RSpec::Steps.steps 'using non-ar aggregations', js: true do
5+
6+
before(:each) do
7+
require 'pusher'
8+
require 'pusher-fake'
9+
Pusher.app_id = "MY_TEST_ID"
10+
Pusher.key = "MY_TEST_KEY"
11+
Pusher.secret = "MY_TEST_SECRET"
12+
require "pusher-fake/support/base"
13+
14+
Hyperloop.configuration do |config|
15+
config.transport = :pusher
16+
config.channel_prefix = "synchromesh"
17+
config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options)
18+
end
19+
20+
User.do_not_synchronize
21+
end
22+
23+
before(:step) do
24+
stub_const 'TestApplicationPolicy', Class.new
25+
TestApplicationPolicy.class_eval do
26+
always_allow_connection
27+
regulate_all_broadcasts { |policy| policy.send_all }
28+
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
29+
end
30+
ApplicationController.acting_user = nil
31+
end
32+
33+
it "create an aggregation" do
34+
expect_evaluate_ruby do
35+
User.new(first_name: "Data", data: TestData.new("hello", 3)).data.big_string
36+
end.to eq("hellohellohello")
37+
end
38+
39+
it "save it" do
40+
evaluate_promise do
41+
User.find_by_first_name("Data").save
42+
end
43+
expect(User.find_by_first_name("Data").data.big_string).to eq("hellohellohello")
44+
binding.pry
45+
end
46+
47+
it "read it" do
48+
User.create(first_name: 'User2', data: TestData.new('goodby', 3))
49+
expect_promise do
50+
ReactiveRecord.load { User.find_by_first_name("User2").data.big_string }
51+
end.to eq('goodbygoodbygoodby')
52+
end
53+
54+
# and restored ...
55+
#
56+
# async "is time to change it, and force the save" do
57+
# user = User.find_by_first_name("Data")
58+
# user.data.string = "goodby"
59+
# user.save(force: true).then do
60+
# React::IsomorphicHelpers.load_context
61+
# ReactiveRecord.load do
62+
# User.find_by_first_name("Data").data
63+
# end.then do |data|
64+
# async { expect(data.big_string).to eq("goodbygoodbygoodby") }
65+
# end
66+
# end
67+
# end
68+
#
69+
# async "is time to change the value completely and save it (no force needed)" do
70+
# user = User.find_by_first_name("Data")
71+
# user.data = TestData.new("the end", 1)
72+
# user.save.then do
73+
# React::IsomorphicHelpers.load_context
74+
# ReactiveRecord.load do
75+
# User.find_by_first_name("Data").data
76+
# end.then do |data|
77+
# async { expect(data.big_string).to eq("the end") }
78+
# end
79+
# end
80+
# end
81+
#
82+
# async "is time to delete the value and see if returns nil after saving" do
83+
# user = User.find_by_first_name("Data")
84+
# user.data = nil
85+
# user.save.then do
86+
# React::IsomorphicHelpers.load_context
87+
# ReactiveRecord.load do
88+
# User.find_by_first_name("Data").data
89+
# end.then do |data|
90+
# async { expect(data).to be_nil }
91+
# end
92+
# end
93+
# end
94+
#
95+
# it "is time to delete our user" do
96+
# User.find_by_first_name("Data").destroy.then do
97+
# expect(User.find_by_first_name("Data")).to be_destroyed
98+
# end
99+
# end
100+
#
101+
# it "is time to see to make sure a nil aggregate that has never had a value returns nil" do
102+
# ReactiveRecord.load do
103+
# User.find_by_email("[email protected]").data
104+
# end.then do |data|
105+
# expect(data).to be_nil
106+
# end
107+
# end
108+
109+
end

spec/batch6/server_method_spec.rb

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'spec_helper'
2+
require 'rspec-steps'
3+
4+
RSpec::Steps.steps 'server_method', js: true do
5+
6+
before(:each) do
7+
require 'pusher'
8+
require 'pusher-fake'
9+
Pusher.app_id = "MY_TEST_ID"
10+
Pusher.key = "MY_TEST_KEY"
11+
Pusher.secret = "MY_TEST_SECRET"
12+
require "pusher-fake/support/base"
13+
14+
Hyperloop.configuration do |config|
15+
config.transport = :pusher
16+
config.channel_prefix = "synchromesh"
17+
config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options)
18+
end
19+
20+
#User.do_not_synchronize
21+
end
22+
23+
before(:step) do
24+
stub_const 'TestApplicationPolicy', Class.new
25+
TestApplicationPolicy.class_eval do
26+
always_allow_connection
27+
regulate_all_broadcasts { |policy| policy.send_all }
28+
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
29+
end
30+
ApplicationController.acting_user = nil
31+
end
32+
33+
it "can call a server method" do
34+
isomorphic do
35+
TodoItem.class_eval do
36+
class << self
37+
attr_writer :server_method_count
38+
39+
def server_method_count
40+
@server_method_count ||= 0
41+
end
42+
end
43+
server_method(:test, default: 0) { TodoItem.server_method_count += 1 }
44+
end
45+
end
46+
TodoItem.create
47+
mount 'ServerMethodTester' do
48+
class ServerMethodTester < Hyperloop::Component
49+
render(DIV) do
50+
"test = #{TodoItem.first.test}"
51+
end
52+
end
53+
end
54+
expect(page).to have_content('test = 1')
55+
end
56+
57+
it "can update the server method" do
58+
evaluate_ruby("TodoItem.first.test!")
59+
expect(page).to have_content('test = 2')
60+
end
61+
62+
it "when updating the server method it returns the current value while waiting for the promise" do
63+
expect_evaluate_ruby("TodoItem.first.test!").to eq(2)
64+
end
65+
66+
it "returns the default value on the first call while waiting for the promise" do
67+
expect_evaluate_ruby("TodoItem.new.test").to eq(0)
68+
end
69+
70+
it "works with the load method" do
71+
expect_promise do
72+
new_todo = TodoItem.new
73+
ReactiveRecord.load do
74+
new_todo.test
75+
end
76+
end.to eq(5)
77+
end
78+
end

0 commit comments

Comments
 (0)