Adding test case for when association condition is a proc.
This commit is contained in:
parent
06d5e5fc97
commit
5df74178a6
|
@ -105,6 +105,14 @@ class AssociationTest < ActionView::TestCase
|
|||
assert_select 'form input.radio_buttons#user_company_id_3'
|
||||
end
|
||||
|
||||
test 'builder allows collection to have a proc as a condition' do
|
||||
with_association_for @user, :extra_special_company
|
||||
assert_select 'form select.select#user_extra_special_company_id'
|
||||
assert_select 'form select option[value=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
|
||||
|
|
|
@ -9,6 +9,7 @@ end
|
|||
|
||||
Relation = Struct.new(:all) do
|
||||
def where(conditions = nil)
|
||||
conditions = conditions.call if conditions.is_a? Proc
|
||||
self.class.new conditions ? all.first : all
|
||||
end
|
||||
|
||||
|
@ -52,7 +53,8 @@ class User
|
|||
:description, :created_at, :updated_at, :credit_limit, :password, :url,
|
||||
:delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
|
||||
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
|
||||
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender
|
||||
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
|
||||
:extra_special_company_id
|
||||
|
||||
def initialize(options={})
|
||||
@new_record = false
|
||||
|
@ -119,6 +121,8 @@ class User
|
|||
Association.new(Company, association, :has_one, {})
|
||||
when :special_company
|
||||
Association.new(Company, association, :belongs_to, { conditions: { id: 1 } })
|
||||
when :extra_special_company
|
||||
Association.new(Company, association, :belongs_to, { conditions: proc { { id: 1 } } })
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue