Merge pull request #1528 from plataformatec/plataformatec/fix-wrong-number-of-arguments-from-scope

Fix error when the scope from association has parameter
This commit is contained in:
Felipe Renan 2017-11-28 21:42:28 -02:00 committed by GitHub
commit ec31349846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -462,7 +462,11 @@ module SimpleForm
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]

View File

@ -122,6 +122,15 @@ class AssociationTest < ActionView::TestCase
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

View File

@ -8,7 +8,7 @@ Relation = Struct.new(:records) do
delegate :each, to: :records
def where(conditions = nil)
self.class.new conditions ? records.first : records
self.class.new conditions ? [records.first] : records
end
def order(conditions = nil)
@ -84,7 +84,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 = {
@ -205,6 +205,8 @@ class User
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