2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-10-17 12:03:28 -04:00
|
|
|
class ActiveModelHelperTest < ActionView::TestCase
|
2009-07-19 12:28:15 -04:00
|
|
|
tests ActionView::Helpers::ActiveModelHelper
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-07-04 19:09:58 -04:00
|
|
|
silence_warnings do
|
2011-12-19 13:55:37 -05:00
|
|
|
class Post < Struct.new(:author_name, :body, :updated_at)
|
2009-07-21 01:51:57 -04:00
|
|
|
include ActiveModel::Conversion
|
2010-04-10 04:53:05 -04:00
|
|
|
include ActiveModel::Validations
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
def persisted?
|
2009-03-07 13:55:12 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-04-26 19:09:08 -04:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2005-06-12 01:33:23 -04:00
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
@post = Post.new
|
|
|
|
@post.errors[:author_name] << "can't be empty"
|
|
|
|
@post.errors[:body] << "foo"
|
2011-12-19 13:55:37 -05:00
|
|
|
@post.errors[:updated_at] << "bar"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
@post.author_name = ""
|
|
|
|
@post.body = "Back to the hill and over it again!"
|
2011-12-19 13:55:37 -05:00
|
|
|
@post.updated_at = Date.new(2004, 6, 15)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-06-12 21:34:16 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_text_area_with_errors
|
2005-09-20 03:54:55 -04:00
|
|
|
assert_dom_equal(
|
2012-03-10 05:30:39 -05:00
|
|
|
%(<div class="field_with_errors"><textarea id="post_body" name="post[body]">\nBack to the hill and over it again!</textarea></div>),
|
2004-11-23 20:04:44 -05:00
|
|
|
text_area("post", "body")
|
2008-07-27 17:34:20 -04:00
|
|
|
)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_text_field_with_errors
|
2005-09-20 03:54:55 -04:00
|
|
|
assert_dom_equal(
|
2012-03-10 05:30:39 -05:00
|
|
|
%(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" type="text" value="" /></div>),
|
2004-11-23 20:04:44 -05:00
|
|
|
text_field("post", "author_name")
|
|
|
|
)
|
|
|
|
end
|
2007-06-12 21:34:16 -04:00
|
|
|
|
2012-07-10 15:03:37 -04:00
|
|
|
def test_select_with_errors
|
|
|
|
assert_dom_equal(
|
|
|
|
%(<div class="field_with_errors"><select name="post[author_name]" id="post_author_name"><option value="a">a</option>\n<option value="b">b</option></select></div>),
|
|
|
|
select("post", "author_name", [:a, :b])
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_select_with_errors_and_blank_option
|
|
|
|
expected_dom = %(<div class="field_with_errors"><select name="post[author_name]" id="post_author_name"><option value="">Choose one...</option>\n<option value="a">a</option>\n<option value="b">b</option></select></div>)
|
|
|
|
assert_dom_equal(expected_dom, select("post", "author_name", [:a, :b], :include_blank => 'Choose one...'))
|
|
|
|
assert_dom_equal(expected_dom, select("post", "author_name", [:a, :b], :prompt => 'Choose one...'))
|
|
|
|
end
|
|
|
|
|
2011-12-19 13:55:37 -05:00
|
|
|
def test_date_select_with_errors
|
|
|
|
assert_dom_equal(
|
2012-03-18 23:44:46 -04:00
|
|
|
%(<div class="field_with_errors"><select id="post_updated_at_1i" name="post[updated_at(1i)]">\n<option selected="selected" value="2004">2004</option>\n<option value="2005">2005</option>\n</select>\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="1" />\n</div>),
|
2011-12-19 13:55:37 -05:00
|
|
|
date_select("post", "updated_at", :discard_month => true, :discard_day => true, :start_year => 2004, :end_year => 2005)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_datetime_select_with_errors
|
|
|
|
assert_dom_equal(
|
2012-03-18 23:44:46 -04:00
|
|
|
%(<div class="field_with_errors"><input id="post_updated_at_1i" name="post[updated_at(1i)]" type="hidden" value="2004" />\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="1" />\n<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n<option selected="selected" value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n</select>\n : <select id="post_updated_at_5i" name="post[updated_at(5i)]">\n<option selected="selected" value="00">00</option>\n</select>\n</div>),
|
2011-12-19 13:55:37 -05:00
|
|
|
datetime_select("post", "updated_at", :discard_year => true, :discard_month => true, :discard_day => true, :minute_step => 60)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_select_with_errors
|
|
|
|
assert_dom_equal(
|
|
|
|
%(<div class="field_with_errors"><input id="post_updated_at_1i" name="post[updated_at(1i)]" type="hidden" value="2004" />\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="15" />\n<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n<option selected="selected" value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n</select>\n : <select id="post_updated_at_5i" name="post[updated_at(5i)]">\n<option selected="selected" value="00">00</option>\n</select>\n</div>),
|
|
|
|
time_select("post", "updated_at", :minute_step => 60)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-06-24 16:36:54 -04:00
|
|
|
def test_hidden_field_does_not_render_errors
|
|
|
|
assert_dom_equal(
|
|
|
|
%(<input id="post_author_name" name="post[author_name]" type="hidden" value="" />),
|
|
|
|
hidden_field("post", "author_name")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-03-15 14:11:46 -04:00
|
|
|
def test_field_error_proc
|
|
|
|
old_proc = ActionView::Base.field_error_proc
|
|
|
|
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
|
2010-04-24 17:57:49 -04:00
|
|
|
%(<div class=\"field_with_errors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
|
2010-03-15 14:11:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal(
|
2012-03-10 05:30:39 -05:00
|
|
|
%(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" type="text" value="" /> <span class="error">can't be empty</span></div>),
|
2010-03-15 14:11:46 -04:00
|
|
|
text_field("post", "author_name")
|
|
|
|
)
|
|
|
|
ensure
|
|
|
|
ActionView::Base.field_error_proc = old_proc if old_proc
|
|
|
|
end
|
2012-07-10 15:03:37 -04:00
|
|
|
|
2004-12-12 06:17:25 -05:00
|
|
|
end
|