Try convert @object to model in case we got decorated object

This commit is contained in:
Timur Vafin 2015-05-20 20:41:28 +03:00
parent 7b9d560fb3
commit f62371525d
8 changed files with 35 additions and 0 deletions

View File

@ -39,6 +39,7 @@ module SimpleForm
def initialize(*) #:nodoc:
super
@object = convert_to_model(@object)
@defaults = options[:defaults]
@wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper)
end

View File

@ -15,6 +15,12 @@ class AssociationTest < ActionView::TestCase
end
end
test 'builder association works with decorated object responsive to #to_model' do
assert_nothing_raised do
with_association_for @decorated_user, :company
end
end
test 'builder association with a block calls simple_fields_for' do
simple_form_for @user do |f|
f.association :posts do |posts_form|

View File

@ -36,6 +36,11 @@ class ErrorTest < ActionView::TestCase
assert_select 'span.error', "cannot be blank"
end
test 'error generates messages with decorated object responsive to #to_model' do
with_error_for @decorated_user, :name
assert_select 'span.error', "cannot be blank"
end
test 'error generates messages for attribute with one error when using first' do
swap SimpleForm, error_method: :first do
with_error_for @user, :age

View File

@ -31,6 +31,12 @@ class FormBuilderTest < ActionView::TestCase
end
end
test 'builder works with decorated object responsive to #to_model' do
assert_nothing_raised do
with_form_for @decorated_user, :name
end
end
test 'builder input allows a block to configure input' do
with_form_for @user, :name do
text_field_tag :foo, :bar, id: :cool

View File

@ -19,6 +19,11 @@ class HintTest < ActionView::TestCase
assert_select 'span.hint', 'Use with care...'
end
test 'hint is generated with decorated object responsive to #to_model' do
with_hint_for @decorated_user, :name, hint: 'Use with care...'
assert_select 'span.hint', 'Use with care...'
end
test 'hint does not modify the options hash' do
options = { hint: 'Use with care...' }
with_hint_for @user, :name, options

View File

@ -14,6 +14,11 @@ class LabelTest < ActionView::TestCase
assert_select 'label.string[for=user_name]', /Name/
end
test 'builder generates a label for the attribute with decorated object responsive to #to_model' do
with_label_for @decorated_user, :name
assert_select 'label.string[for=user_name]', /Name/
end
test 'builder generates a label for the boolean attrbiute' do
with_label_for @user, :name, as: :boolean
assert_select 'label.boolean[for=user_name]', /Name/

View File

@ -19,6 +19,12 @@ Relation = Struct.new(:records) do
alias_method :to_ary, :records
end
Decorator = Struct.new(:object) do
def to_model
object
end
end
Picture = Struct.new(:id, :name) do
extend ActiveModel::Naming
include ActiveModel::Conversion

View File

@ -54,6 +54,7 @@ class ActionView::TestCase
def setup_users(extra_attributes = {})
@user = User.build(extra_attributes)
@decorated_user = Decorator.new(@user)
@validating_user = ValidatingUser.build({
name: 'Tester McTesterson',