Skip to content

Commit

Permalink
Merge pull request #233 from lionelchauvin/issue-218
Browse files Browse the repository at this point in the history
fix issue #218: before_validation :update_permalink, on: :create is n…
  • Loading branch information
catmando authored Aug 26, 2019
2 parents e83be32 + 966a76f commit 5313d11
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,13 @@ def self.save_records(models, associations, acting_user, validate, save)
#puts ">>>>>>>>>> #{parent.class.name}.send('#{association[:attribute]}') << #{reactive_records[association[:child_id]]})"
dont_save_list.delete(parent)


# if reactive_records[association[:child_id]]&.new_record?
# dont_save_list << reactive_records[association[:child_id]]
# end
#if false and parent.new?
#parent.send("#{association[:attribute]}") << reactive_records[association[:child_id]]
# puts "updated"
#else
#puts "skipped"
#end

if parent.new_record?
parent.send("#{association[:attribute]}") << reactive_records[association[:child_id]]
end
else
#puts ">>>>ASSOCIATION>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
parent.send("#{association[:attribute]}=", reactive_records[association[:child_id]])
Expand All @@ -496,7 +493,8 @@ def self.save_records(models, associations, acting_user, validate, save)
next record.persisted? if record.id && !record.changed?
# if we get to here save the record and return true to keep it
op = new_models.include?(record) ? :create_permitted? : :update_permitted?
record.check_permission_with_acting_user(acting_user, op).save(validate: false) || true

record.check_permission_with_acting_user(acting_user, op).save(validate: validate) || true
end

# if called from ServerDataCache then save and validate are both false, and we just return the
Expand Down
51 changes: 51 additions & 0 deletions ruby/hyper-model/spec/batch1/misc/callbacks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'
require 'test_components'

describe 'callbacks', js: true do
before(:all) do
class ActiveRecord::Base
class << self
def public_columns_hash
@public_columns_hash ||= {}
end
end
end
end

describe 'before_validation :callback_method on create', js: true do
before(:each) do
policy_allows_all

isomorphic do
class ModelWithCallback < ActiveRecord::Base

def self.build_tables
connection.create_table(:model_with_callbacks, force: true) do |t|
t.integer :call_count, default: 0
t.timestamps
end
ActiveRecord::Base.public_columns_hash[name] = columns_hash
end

before_validation :callback_method, on: :create

def callback_method
self.call_count += 1
end
end
end

ModelWithCallback.build_tables
end

it "should be called" do
expect_promise do
model = ModelWithCallback.new
model.save.then do
model.call_count
end
end.to eq 1
end

end
end
29 changes: 28 additions & 1 deletion ruby/hyper-model/spec/batch1/misc/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
User.instance_variable_set :@do_not_synchronize, false
end

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

it "can validate the presence of an association on a saved record" do
expect(TestModel.count).to be_zero
expect(ChildModel.count).to be_zero

expect_promise do
@child = ChildModel.new
@test_model = TestModel.new(child_models: [@child])
@test_model.save.then { |r| @test_model.errors.messages }
end.to be_empty

expect_promise do
@child.destroy.then do |d|
@test_model = TestModel.find_by_id(@test_model.id)
@test_model.validate(force: true).then do |test_model| # why force is needed ?
test_model.errors.messages
end
end
end.to_not be_empty
end

it "can validate only using the validate method" do
expect_promise do
User.new(last_name: 'f**k').validate.then do |new_user|
Expand Down Expand Up @@ -83,6 +103,13 @@ class << self
end.to be_falsy
end

it "save without validate should save invalid record" do
expect_promise do
user = User.new(last_name: 'f**k')
user.save(validate: false).then { |result| user.new_record?}
end.to be_falsy
end

it "the valid? method reacts to the model being saved" do
evaluate_ruby do
Validator.model.last_name = 'f**k'
Expand Down
11 changes: 11 additions & 0 deletions ruby/hyper-model/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ def self.should_immediately_generate(opts={}, &block)
require 'support/component_helpers'
require 'selenium-webdriver'

def policy_allows_all
stub_const 'TestApplication', Class.new
stub_const 'TestApplicationPolicy', Class.new
TestApplicationPolicy.class_eval do
always_allow_connection
regulate_all_broadcasts { |policy| policy.send_all }
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
end
end


module React
module IsomorphicHelpers
def self.xxxload_context(ctx, controller, name = nil)
Expand Down

0 comments on commit 5313d11

Please sign in to comment.