Skip to content

Commit

Permalink
Fix error when the scope from association has parameter
Browse files Browse the repository at this point in the history
As we are using the instance_exec to execute the scope from the
association, when the scope has a parameter, it's breaking.

The scope's parameter always will be the instance of the own class, so
we can fix it using the object instance that simple form uses to build
the form.
  • Loading branch information
feliperenan committed Nov 28, 2017
1 parent 6da385b commit a6b8702
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,11 @@ def fetch_association_collection(reflection, options)
relation = reflection.klass.all

if reflection.respond_to?(:scope) && reflection.scope
relation = reflection.klass.instance_exec(&reflection.scope)
if reflection.scope.parameters.any?
relation = reflection.klass.instance_exec(object, &reflection.scope)
else
relation = reflection.klass.instance_exec(&reflection.scope)
end
else
order = reflection.options[:order]
conditions = reflection.options[:conditions]
Expand Down
9 changes: 9 additions & 0 deletions test/form_builder/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ def with_association_for(object, *args)
assert_no_select 'form select option[value="2"]'
end

test 'builder allows collection to have a scope with parameter' do
with_association_for @user, :special_tags
assert_select 'form select.select#user_special_tag_ids'
assert_select 'form select[multiple=multiple]'
assert_select 'form select option[value="1"]', 'Tag 1'
assert_no_select 'form select option[value="2"]'
assert_no_select 'form select option[value="3"]'
end

test 'builder marks the record which already belongs to the user' do
@user.company_id = 2
with_association_for @user, :company, as: :radio_buttons
Expand Down
4 changes: 3 additions & 1 deletion test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class User
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
:extra_special_company_id, :pictures, :picture_ids, :special_pictures,
:special_picture_ids, :uuid, :friends, :friend_ids
:special_picture_ids, :uuid, :friends, :friend_ids, :special_tags, :special_tag_ids

def self.build(extra_attributes = {})
attributes = {
Expand Down Expand Up @@ -204,6 +204,8 @@ def self.reflect_on_association(association)
Association.new(Company, association, :belongs_to, nil, {})
when :tags
Association.new(Tag, association, :has_many, nil, {})
when :special_tags
Association.new(Tag, association, :has_many, ->(user) { where(id: user.id) }, {})
when :first_company
Association.new(Company, association, :has_one, nil, {})
when :special_company
Expand Down

0 comments on commit a6b8702

Please sign in to comment.