Skip to content

Commit

Permalink
Fix for AR association accepts_nested_attributes_for and a raise_if p…
Browse files Browse the repository at this point in the history
…roc (#196)
  • Loading branch information
evaniainbrooks authored Nov 30, 2024
1 parent 2a35d18 commit 41b26fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/store_model/nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ def assign_nested_attributes_for_collection_association(association, attributes,
end
end

attributes.reject! { |attribute| call_reject_if(attribute, options[:reject_if]) } if options&.dig(:reject_if)
attributes.reject! { call_store_model_reject_if(_1, options[:reject_if]) } if options&.dig(:reject_if)

send("#{association}=", attributes)
end

def call_reject_if(attributes, callback)
def call_store_model_reject_if(attributes, callback)
callback = ActiveRecord::NestedAttributes::ClassMethods::REJECT_ALL_BLANK_PROC if callback == :all_blank

case callback
Expand Down
22 changes: 22 additions & 0 deletions spec/store_model/nested_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,28 @@ def self.model_name
it { is_expected.to respond_to(:bicycles_attributes=) }
end

context "with nested attributes for an AR association and a reject_if proc" do
class Product < ActiveRecord::Base
include StoreModel::NestedAttributes

attribute :suppliers, Supplier.to_array_type

belongs_to :store

accepts_nested_attributes_for(:store, reject_if: ->(_) { true })

accepts_nested_attributes_for(:suppliers, allow_destroy: true)
end

subject { Product.new }

it "rejects blank nested AR attributes" do
subject.update(store_attributes: { bicycles: "" })

expect(subject.reload.store).to be_nil
end
end

context "when db is not connected" do
subject do
model_class.accepts_nested_attributes_for(:suppliers, allow_destroy: true)
Expand Down

0 comments on commit 41b26fc

Please sign in to comment.