Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for AR association accepts_nested_attributes_for and a raise_if proc #196

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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