Skip to content

Commit 5313d11

Browse files
authored
Merge pull request hyperstack-org#233 from lionelchauvin/issue-218
fix issue hyperstack-org#218: before_validation :update_permalink, on: :create is n…
2 parents e83be32 + 966a76f commit 5313d11

4 files changed

Lines changed: 96 additions & 9 deletions

File tree

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,16 +462,13 @@ def self.save_records(models, associations, acting_user, validate, save)
462462
#puts ">>>>>>>>>> #{parent.class.name}.send('#{association[:attribute]}') << #{reactive_records[association[:child_id]]})"
463463
dont_save_list.delete(parent)
464464

465-
466465
# if reactive_records[association[:child_id]]&.new_record?
467466
# dont_save_list << reactive_records[association[:child_id]]
468467
# end
469-
#if false and parent.new?
470-
#parent.send("#{association[:attribute]}") << reactive_records[association[:child_id]]
471-
# puts "updated"
472-
#else
473-
#puts "skipped"
474-
#end
468+
469+
if parent.new_record?
470+
parent.send("#{association[:attribute]}") << reactive_records[association[:child_id]]
471+
end
475472
else
476473
#puts ">>>>ASSOCIATION>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
477474
parent.send("#{association[:attribute]}=", reactive_records[association[:child_id]])
@@ -496,7 +493,8 @@ def self.save_records(models, associations, acting_user, validate, save)
496493
next record.persisted? if record.id && !record.changed?
497494
# if we get to here save the record and return true to keep it
498495
op = new_models.include?(record) ? :create_permitted? : :update_permitted?
499-
record.check_permission_with_acting_user(acting_user, op).save(validate: false) || true
496+
497+
record.check_permission_with_acting_user(acting_user, op).save(validate: validate) || true
500498
end
501499

502500
# if called from ServerDataCache then save and validate are both false, and we just return the
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'spec_helper'
2+
require 'test_components'
3+
4+
describe 'callbacks', js: true do
5+
before(:all) do
6+
class ActiveRecord::Base
7+
class << self
8+
def public_columns_hash
9+
@public_columns_hash ||= {}
10+
end
11+
end
12+
end
13+
end
14+
15+
describe 'before_validation :callback_method on create', js: true do
16+
before(:each) do
17+
policy_allows_all
18+
19+
isomorphic do
20+
class ModelWithCallback < ActiveRecord::Base
21+
22+
def self.build_tables
23+
connection.create_table(:model_with_callbacks, force: true) do |t|
24+
t.integer :call_count, default: 0
25+
t.timestamps
26+
end
27+
ActiveRecord::Base.public_columns_hash[name] = columns_hash
28+
end
29+
30+
before_validation :callback_method, on: :create
31+
32+
def callback_method
33+
self.call_count += 1
34+
end
35+
end
36+
end
37+
38+
ModelWithCallback.build_tables
39+
end
40+
41+
it "should be called" do
42+
expect_promise do
43+
model = ModelWithCallback.new
44+
model.save.then do
45+
model.call_count
46+
end
47+
end.to eq 1
48+
end
49+
50+
end
51+
end

ruby/hyper-model/spec/batch1/misc/validate_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
User.instance_variable_set :@do_not_synchronize, false
3131
end
3232

33-
it "can validate the presence of an association" do
33+
it "can validate the presence of an association on a new record" do
3434
expect_promise do
3535
@test_model = TestModel.new
3636
@test_model.validate.then { |test_model| test_model.errors.messages }
@@ -43,6 +43,26 @@
4343
expect(ChildModel.count).to be_zero
4444
end
4545

46+
it "can validate the presence of an association on a saved record" do
47+
expect(TestModel.count).to be_zero
48+
expect(ChildModel.count).to be_zero
49+
50+
expect_promise do
51+
@child = ChildModel.new
52+
@test_model = TestModel.new(child_models: [@child])
53+
@test_model.save.then { |r| @test_model.errors.messages }
54+
end.to be_empty
55+
56+
expect_promise do
57+
@child.destroy.then do |d|
58+
@test_model = TestModel.find_by_id(@test_model.id)
59+
@test_model.validate(force: true).then do |test_model| # why force is needed ?
60+
test_model.errors.messages
61+
end
62+
end
63+
end.to_not be_empty
64+
end
65+
4666
it "can validate only using the validate method" do
4767
expect_promise do
4868
User.new(last_name: 'f**k').validate.then do |new_user|
@@ -83,6 +103,13 @@ class << self
83103
end.to be_falsy
84104
end
85105

106+
it "save without validate should save invalid record" do
107+
expect_promise do
108+
user = User.new(last_name: 'f**k')
109+
user.save(validate: false).then { |result| user.new_record?}
110+
end.to be_falsy
111+
end
112+
86113
it "the valid? method reacts to the model being saved" do
87114
evaluate_ruby do
88115
Validator.model.last_name = 'f**k'

ruby/hyper-model/spec/spec_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ def self.should_immediately_generate(opts={}, &block)
133133
require 'support/component_helpers'
134134
require 'selenium-webdriver'
135135

136+
def policy_allows_all
137+
stub_const 'TestApplication', Class.new
138+
stub_const 'TestApplicationPolicy', Class.new
139+
TestApplicationPolicy.class_eval do
140+
always_allow_connection
141+
regulate_all_broadcasts { |policy| policy.send_all }
142+
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
143+
end
144+
end
145+
146+
136147
module React
137148
module IsomorphicHelpers
138149
def self.xxxload_context(ctx, controller, name = nil)

0 commit comments

Comments
 (0)