clearance 0.8.8 → 0.9.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of clearance might be problematic. Click here for more details.

Files changed (94) hide show
  1. data/CHANGELOG.md +8 -0
  2. data/README.md +27 -32
  3. data/Rakefile +25 -31
  4. data/app/controllers/clearance/passwords_controller.rb +1 -1
  5. data/app/controllers/clearance/sessions_controller.rb +1 -1
  6. data/app/models/clearance_mailer.rb +4 -4
  7. data/app/views/passwords/edit.html.erb +1 -1
  8. data/app/views/passwords/new.html.erb +2 -2
  9. data/app/views/sessions/new.html.erb +1 -1
  10. data/app/views/users/_form.html.erb +1 -1
  11. data/app/views/users/new.html.erb +1 -1
  12. data/config/routes.rb +30 -0
  13. data/lib/clearance.rb +2 -1
  14. data/lib/clearance/authentication.rb +2 -2
  15. data/lib/clearance/configuration.rb +10 -3
  16. data/lib/clearance/engine.rb +8 -0
  17. data/lib/clearance/user.rb +5 -6
  18. data/lib/rails/generators/clearance_features_generator.rb +20 -0
  19. data/{generators/clearance_features/templates → lib/rails/generators/clearance_features_templates}/features/password_reset.feature +1 -1
  20. data/{generators/clearance_features/templates → lib/rails/generators/clearance_features_templates}/features/sign_in.feature +0 -0
  21. data/{generators/clearance_features/templates → lib/rails/generators/clearance_features_templates}/features/sign_out.feature +0 -0
  22. data/{generators/clearance_features/templates → lib/rails/generators/clearance_features_templates}/features/sign_up.feature +0 -0
  23. data/{generators/clearance_features/templates → lib/rails/generators/clearance_features_templates}/features/step_definitions/clearance_steps.rb +13 -5
  24. data/lib/rails/generators/clearance_generator.rb +68 -0
  25. data/{generators/clearance/templates → lib/rails/generators/clearance_templates}/README +0 -0
  26. data/{generators/clearance/templates → lib/rails/generators/clearance_templates}/clearance.rb +0 -0
  27. data/{generators/clearance/templates → lib/rails/generators/clearance_templates}/factories.rb +0 -0
  28. data/{generators/clearance/templates → lib/rails/generators/clearance_templates}/migrations/create_users.rb +0 -1
  29. data/{generators/clearance/templates → lib/rails/generators/clearance_templates}/migrations/update_users.rb +0 -1
  30. data/{generators/clearance/templates → lib/rails/generators/clearance_templates}/user.rb +0 -0
  31. data/lib/rails/generators/clearance_views_generator.rb +14 -0
  32. data/{generators/clearance_views/templates/formtastic → lib/rails/generators/clearance_views_templates/formtastic/erb}/passwords/edit.html.erb +3 -3
  33. data/{generators/clearance_views/templates/formtastic → lib/rails/generators/clearance_views_templates/formtastic/erb}/passwords/new.html.erb +3 -3
  34. data/{generators/clearance_views/templates/formtastic → lib/rails/generators/clearance_views_templates/formtastic/erb}/sessions/new.html.erb +3 -3
  35. data/{generators/clearance_views/templates/formtastic → lib/rails/generators/clearance_views_templates/formtastic/erb}/users/_inputs.html.erb +1 -1
  36. data/{generators/clearance_views/templates/formtastic → lib/rails/generators/clearance_views_templates/formtastic/erb}/users/new.html.erb +2 -2
  37. data/shoulda_macros/clearance.rb +9 -17
  38. data/test/controllers/confirmations_controller_test.rb +6 -6
  39. data/test/controllers/passwords_controller_test.rb +10 -10
  40. data/test/controllers/sessions_controller_test.rb +57 -16
  41. data/test/controllers/users_controller_test.rb +12 -8
  42. data/test/models/clearance_mailer_test.rb +2 -2
  43. data/test/models/user_test.rb +14 -7
  44. data/test/rails_root/app/helpers/application_helper.rb +0 -3
  45. data/test/rails_root/config/application.rb +46 -0
  46. data/test/rails_root/config/boot.rb +5 -109
  47. data/test/rails_root/config/environment.rb +5 -15
  48. data/test/rails_root/config/environments/development.rb +15 -15
  49. data/test/rails_root/config/environments/production.rb +42 -1
  50. data/test/rails_root/config/environments/test.rb +25 -29
  51. data/test/rails_root/config/initializers/backtrace_silencers.rb +7 -0
  52. data/test/rails_root/config/initializers/inflections.rb +1 -1
  53. data/test/rails_root/config/initializers/secret_token.rb +7 -0
  54. data/test/rails_root/config/initializers/session_store.rb +8 -0
  55. data/test/rails_root/config/routes.rb +57 -2
  56. data/test/rails_root/features/step_definitions/web_steps.rb +125 -165
  57. data/test/rails_root/features/support/env.rb +24 -14
  58. data/test/rails_root/features/support/paths.rb +26 -4
  59. data/test/rails_root/test/functional/accounts_controller_test.rb +0 -1
  60. data/test/rails_root/test/performance/browsing_test.rb +9 -0
  61. data/test/rails_root/test/test_helper.rb +13 -0
  62. data/test/rails_root/vendor/plugins/dynamic_form/init.rb +5 -0
  63. data/test/rails_root/vendor/plugins/dynamic_form/lib/action_view/helpers/dynamic_form.rb +300 -0
  64. data/test/rails_root/vendor/plugins/dynamic_form/test/dynamic_form_i18n_test.rb +42 -0
  65. data/test/rails_root/vendor/plugins/dynamic_form/test/dynamic_form_test.rb +370 -0
  66. data/test/rails_root/vendor/plugins/dynamic_form/test/test_helper.rb +9 -0
  67. data/test/test_helper.rb +30 -1
  68. metadata +104 -49
  69. data/VERSION +0 -1
  70. data/generators/clearance/USAGE +0 -1
  71. data/generators/clearance/clearance_generator.rb +0 -68
  72. data/generators/clearance/lib/insert_commands.rb +0 -33
  73. data/generators/clearance/lib/rake_commands.rb +0 -22
  74. data/generators/clearance_features/USAGE +0 -1
  75. data/generators/clearance_features/clearance_features_generator.rb +0 -19
  76. data/generators/clearance_features/templates/features/support/paths.rb +0 -23
  77. data/generators/clearance_views/USAGE +0 -0
  78. data/generators/clearance_views/clearance_views_generator.rb +0 -27
  79. data/lib/clearance/routes.rb +0 -49
  80. data/test/rails_root/app/helpers/confirmations_helper.rb +0 -2
  81. data/test/rails_root/app/helpers/passwords_helper.rb +0 -2
  82. data/test/rails_root/config/initializers/requires.rb +0 -13
  83. data/test/rails_root/config/initializers/time_formats.rb +0 -4
  84. data/test/rails_root/db/migrate/20100225210436_clearance_create_users.rb +0 -21
  85. data/test/rails_root/features/step_definitions/clearance_steps.rb +0 -122
  86. data/test/rails_root/features/step_definitions/factory_girl_steps.rb +0 -5
  87. data/test/rails_root/public/dispatch.rb +0 -10
  88. data/test/rails_root/script/create_project.rb +0 -52
  89. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +0 -21
  90. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/lib/formtastic.rb +0 -1236
  91. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/lib/justin_french/formtastic.rb +0 -10
  92. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/rails/init.rb +0 -3
  93. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/spec/formtastic_spec.rb +0 -2900
  94. data/test/rails_root/vendor/gems/justinfrench-formtastic-0.2.1/spec/test_helper.rb +0 -14
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class DynamicFormI18nTest < Test::Unit::TestCase
4
+ include ActionView::Context
5
+ include ActionView::Helpers::DynamicForm
6
+
7
+ attr_reader :request
8
+
9
+ def setup
10
+ @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
11
+ @object.stubs :to_model => @object
12
+ @object.stubs :class => stub(:model_name => stub(:human => ""))
13
+
14
+ @object_name = 'book_seller'
15
+ @object_name_without_underscore = 'book seller'
16
+
17
+ stubs(:content_tag).returns 'content_tag'
18
+
19
+ I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
20
+ I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).returns 'There were problems with the following fields:'
21
+ end
22
+
23
+ def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
24
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').never
25
+ error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
26
+ end
27
+
28
+ def test_error_messages_for_given_no_header_option_it_translates_header_message
29
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:errors, :template], :count => 1, :model => '').returns 'header message'
30
+ error_messages_for(:object => @object, :locale => 'en')
31
+ end
32
+
33
+ def test_error_messages_for_given_a_message_option_it_does_not_translate_message
34
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).never
35
+ error_messages_for(:object => @object, :message => 'message', :locale => 'en')
36
+ end
37
+
38
+ def test_error_messages_for_given_no_message_option_it_translates_message
39
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:errors, :template]).returns 'There were problems with the following fields:'
40
+ error_messages_for(:object => @object, :locale => 'en')
41
+ end
42
+ end
@@ -0,0 +1,370 @@
1
+ require 'test_helper'
2
+ require 'action_view/template/handlers/erb'
3
+
4
+ class DynamicFormTest < ActionView::TestCase
5
+ tests ActionView::Helpers::DynamicForm
6
+
7
+ def form_for(*)
8
+ @output_buffer = super
9
+ end
10
+
11
+ silence_warnings do
12
+ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on)
13
+ extend ActiveModel::Naming
14
+ include ActiveModel::Conversion
15
+ end
16
+
17
+ class User < Struct.new(:email)
18
+ extend ActiveModel::Naming
19
+ include ActiveModel::Conversion
20
+ end
21
+
22
+ class Column < Struct.new(:type, :name, :human_name)
23
+ extend ActiveModel::Naming
24
+ include ActiveModel::Conversion
25
+ end
26
+ end
27
+
28
+ class DirtyPost
29
+ class Errors
30
+ def empty?
31
+ false
32
+ end
33
+
34
+ def count
35
+ 1
36
+ end
37
+
38
+ def full_messages
39
+ ["Author name can't be <em>empty</em>"]
40
+ end
41
+
42
+ def [](field)
43
+ ["can't be <em>empty</em>"]
44
+ end
45
+ end
46
+
47
+ def errors
48
+ Errors.new
49
+ end
50
+ end
51
+
52
+ def setup_post
53
+ @post = Post.new
54
+ def @post.errors
55
+ Class.new {
56
+ def [](field)
57
+ case field.to_s
58
+ when "author_name"
59
+ ["can't be empty"]
60
+ when "body"
61
+ ['foo']
62
+ else
63
+ []
64
+ end
65
+ end
66
+ def empty?() false end
67
+ def count() 1 end
68
+ def full_messages() [ "Author name can't be empty" ] end
69
+ }.new
70
+ end
71
+
72
+ def @post.persisted?() false end
73
+ def @post.to_param() nil end
74
+
75
+ def @post.column_for_attribute(attr_name)
76
+ Post.content_columns.select { |column| column.name == attr_name }.first
77
+ end
78
+
79
+ silence_warnings do
80
+ def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end
81
+ end
82
+
83
+ @post.title = "Hello World"
84
+ @post.author_name = ""
85
+ @post.body = "Back to the hill and over it again!"
86
+ @post.secret = 1
87
+ @post.written_on = Date.new(2004, 6, 15)
88
+ end
89
+
90
+ def setup_user
91
+ @user = User.new
92
+ def @user.errors
93
+ Class.new {
94
+ def [](field) field == "email" ? ['nonempty'] : [] end
95
+ def empty?() false end
96
+ def count() 1 end
97
+ def full_messages() [ "User email can't be empty" ] end
98
+ }.new
99
+ end
100
+
101
+ def @user.new_record?() true end
102
+ def @user.to_param() nil end
103
+
104
+ def @user.column_for_attribute(attr_name)
105
+ User.content_columns.select { |column| column.name == attr_name }.first
106
+ end
107
+
108
+ silence_warnings do
109
+ def User.content_columns() [ Column.new(:string, "email", "Email") ] end
110
+ end
111
+
112
+ @user.email = ""
113
+ end
114
+
115
+ def protect_against_forgery?
116
+ @protect_against_forgery ? true : false
117
+ end
118
+ attr_accessor :request_forgery_protection_token, :form_authenticity_token
119
+
120
+ def setup
121
+ super
122
+ setup_post
123
+ setup_user
124
+
125
+ @response = ActionController::TestResponse.new
126
+ end
127
+
128
+ def url_for(options)
129
+ options = options.symbolize_keys
130
+ [options[:action], options[:id].to_param].compact.join('/')
131
+ end
132
+
133
+ def test_generic_input_tag
134
+ assert_dom_equal(
135
+ %(<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />), input("post", "title")
136
+ )
137
+ end
138
+
139
+ def test_text_area_with_errors
140
+ assert_dom_equal(
141
+ %(<div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div>),
142
+ text_area("post", "body")
143
+ )
144
+ end
145
+
146
+ def test_text_field_with_errors
147
+ assert_dom_equal(
148
+ %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /></div>),
149
+ text_field("post", "author_name")
150
+ )
151
+ end
152
+
153
+ def test_field_error_proc
154
+ old_proc = ActionView::Base.field_error_proc
155
+ ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
156
+ %(<div class=\"fieldWithErrors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
157
+ end
158
+
159
+ assert_dom_equal(
160
+ %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>),
161
+ text_field("post", "author_name")
162
+ )
163
+ ensure
164
+ ActionView::Base.field_error_proc = old_proc if old_proc
165
+ end
166
+
167
+ def test_form_with_string
168
+ assert_dom_equal(
169
+ %(<form action="create" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
170
+ form("post")
171
+ )
172
+
173
+ silence_warnings do
174
+ class << @post
175
+ def persisted?() true end
176
+ def to_param() id end
177
+ def id() 1 end
178
+ end
179
+ end
180
+
181
+ assert_dom_equal(
182
+ %(<form action="update/1" method="post"><input id="post_id" name="post[id]" type="hidden" value="1" /><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Update" /></form>),
183
+ form("post")
184
+ )
185
+ end
186
+
187
+ def test_form_with_protect_against_forgery
188
+ @protect_against_forgery = true
189
+ @request_forgery_protection_token = 'authenticity_token'
190
+ @form_authenticity_token = '123'
191
+ assert_dom_equal(
192
+ %(<form action="create" method="post"><div style='margin:0;padding:0;display:inline'><input type='hidden' name='authenticity_token' value='123' /></div><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
193
+ form("post")
194
+ )
195
+ end
196
+
197
+ def test_form_with_method_option
198
+ assert_dom_equal(
199
+ %(<form action="create" method="get"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
200
+ form("post", :method=>'get')
201
+ )
202
+ end
203
+
204
+ def test_form_with_action_option
205
+ output_buffer << form("post", :action => "sign")
206
+ assert_select "form[action=sign]" do |form|
207
+ assert_select "input[type=submit][value=Sign]"
208
+ end
209
+ end
210
+
211
+ def test_form_with_date
212
+ silence_warnings do
213
+ def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
214
+ end
215
+
216
+ assert_dom_equal(
217
+ %(<form action="create" method="post"><p><label for="post_written_on">Written on</label><br /><select id="post_written_on_1i" name="post[written_on(1i)]">\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n</select>\n<select id="post_written_on_2i" name="post[written_on(2i)]">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n</select>\n<select id="post_written_on_3i" name="post[written_on(3i)]">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</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" selected="selected">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<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n</select>\n</p><input name="commit" type="submit" value="Create" /></form>),
218
+ form("post")
219
+ )
220
+ end
221
+
222
+ def test_form_with_datetime
223
+ silence_warnings do
224
+ def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end
225
+ end
226
+ @post.written_on = Time.gm(2004, 6, 15, 16, 30)
227
+
228
+ assert_dom_equal(
229
+ %(<form action="create" method="post"><p><label for="post_written_on">Written on</label><br /><select id="post_written_on_1i" name="post[written_on(1i)]">\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n</select>\n<select id="post_written_on_2i" name="post[written_on(2i)]">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n</select>\n<select id="post_written_on_3i" name="post[written_on(3i)]">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</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" selected="selected">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<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n</select>\n &mdash; <select id="post_written_on_4i" name="post[written_on(4i)]">\n<option 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" selected="selected">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_written_on_5i" name="post[written_on(5i)]">\n<option 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<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30" selected="selected">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n</select>\n</p><input name="commit" type="submit" value="Create" /></form>),
230
+ form("post")
231
+ )
232
+ end
233
+
234
+ def test_error_for_block
235
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post")
236
+ assert_equal %(<div class="errorDeathByClass" id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1")
237
+ assert_equal %(<div id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1")
238
+ assert_equal %(<div class="errorDeathByClass"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1")
239
+ end
240
+
241
+ def test_error_messages_for_escapes_html
242
+ @dirty_post = DirtyPost.new
243
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this dirty post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be &lt;em&gt;empty&lt;/em&gt;</li></ul></div>), error_messages_for("dirty_post")
244
+ end
245
+
246
+ def test_error_messages_for_handles_nil
247
+ assert_equal "", error_messages_for("notthere")
248
+ end
249
+
250
+ def test_error_message_on_escapes_html
251
+ @dirty_post = DirtyPost.new
252
+ assert_dom_equal "<div class=\"formError\">can't be &lt;em&gt;empty&lt;/em&gt;</div>", error_message_on(:dirty_post, :author_name)
253
+ end
254
+
255
+ def test_error_message_on_handles_nil
256
+ assert_equal "", error_message_on("notthere", "notthere")
257
+ end
258
+
259
+ def test_error_message_on
260
+ assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(:post, :author_name)
261
+ end
262
+
263
+ def test_error_message_on_no_instance_variable
264
+ other_post = @post
265
+ assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(other_post, :author_name)
266
+ end
267
+
268
+ def test_error_message_on_with_options_hash
269
+ assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
270
+ end
271
+
272
+ def test_error_message_on_with_tag_option_in_options_hash
273
+ assert_dom_equal "<span class=\"differentError\">beforecan't be emptyafter</span>", error_message_on(:post, :author_name, :html_tag => "span", :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
274
+ end
275
+
276
+ def test_error_message_on_handles_empty_errors
277
+ assert_equal "", error_message_on(@post, :tag)
278
+ end
279
+
280
+ def test_error_messages_for_many_objects
281
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user")
282
+
283
+ # reverse the order, error order changes and so does the title
284
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post")
285
+
286
+ # add the default to put post back in the title
287
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object_name => "post")
288
+
289
+ # symbols work as well
290
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => :post)
291
+
292
+ # any default works too
293
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this monkey from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "monkey")
294
+
295
+ # should space object name
296
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this chunky bacon from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "chunky_bacon")
297
+
298
+ # hide header and explanation messages with nil or empty string
299
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => nil, :message => "")
300
+
301
+ # override header and explanation messages
302
+ header_message = "Yikes! Some errors"
303
+ message = "Please fix the following fields and resubmit:"
304
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>#{header_message}</h2><p>#{message}</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => header_message, :message => message)
305
+ end
306
+
307
+ def test_error_messages_for_non_instance_variable
308
+ actual_user = @user
309
+ actual_post = @post
310
+ @user = nil
311
+ @post = nil
312
+
313
+ #explicitly set object
314
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :object => actual_post)
315
+
316
+ #multiple objects
317
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object => [actual_user, actual_post])
318
+
319
+ #nil object
320
+ assert_equal '', error_messages_for('user', :object => nil)
321
+ end
322
+
323
+ def test_error_messages_for_model_objects
324
+ error = error_messages_for(@post)
325
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>),
326
+ error
327
+
328
+ error = error_messages_for(@user, @post)
329
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>),
330
+ error
331
+ end
332
+
333
+ def test_form_with_string_multipart
334
+ assert_dom_equal(
335
+ %(<form action="create" enctype="multipart/form-data" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
336
+ form("post", :multipart => true)
337
+ )
338
+ end
339
+
340
+ def test_default_form_builder_with_dynamic_form_helpers
341
+ form_for(@post, :as => :post, :url => {}) do |f|
342
+ concat f.error_message_on('author_name')
343
+ concat f.error_messages
344
+ end
345
+
346
+ expected = %(<form class="post_new" method="post" action="" id="post_new">) +
347
+ %(<div class="formError">can't be empty</div>) +
348
+ %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
349
+ %(</form>)
350
+
351
+ assert_dom_equal expected, output_buffer
352
+ end
353
+
354
+ def test_default_form_builder_no_instance_variable
355
+ post = @post
356
+ @post = nil
357
+
358
+ form_for(post, :as => :post, :url => {}) do |f|
359
+ concat f.error_message_on('author_name')
360
+ concat f.error_messages
361
+ end
362
+
363
+ expected = %(<form class="post_new" method="post" action="" id="post_new">) +
364
+ %(<div class="formError">can't be empty</div>) +
365
+ %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
366
+ %(</form>)
367
+
368
+ assert_dom_equal expected, output_buffer
369
+ end
370
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require 'action_view'
6
+ require 'action_controller'
7
+ require 'action_controller/test_case'
8
+ require 'active_model'
9
+ require 'action_view/helpers/dynamic_form'
@@ -1,7 +1,7 @@
1
1
  ENV["RAILS_ENV"] = "test"
2
2
  require File.expand_path(File.dirname(__FILE__) +
3
3
  "/rails_root/config/environment")
4
- require 'test_help'
4
+ require 'rails/test_help'
5
5
 
6
6
  $: << File.expand_path(File.dirname(__FILE__) + '/..')
7
7
  require 'clearance'
@@ -16,4 +16,33 @@ require File.join(File.dirname(__FILE__), '..', 'shoulda_macros', 'clearance')
16
16
  class ActiveSupport::TestCase
17
17
  self.use_transactional_fixtures = true
18
18
  self.use_instantiated_fixtures = false
19
+
20
+ def self.should_set_cookie(name, value, should_expire_at)
21
+ description = "set a '#{name}' cookie to '#{value}'"
22
+ if should_expire_at
23
+ description << " expiring at #{should_expire_at}"
24
+ else
25
+ description << " with no expiration date (session cookie)"
26
+ end
27
+ should description do
28
+ assert_equal value, cookies[name]
29
+ # the following statement may be redundant with the preceding one, but can't hurt
30
+ assert_equal value, @response.cookies[name]
31
+ # cookies and @response[cookies] don't give us the expire time, so we need to fish it out 'manually'
32
+ set_cookie_headers = @response.headers['Set-Cookie']
33
+ assert_not_nil set_cookie_headers, "@response.headers['Set-Cookie'] must not be nil"
34
+ regex = /^#{name}=#{value}(;|$)/
35
+ assert_contains set_cookie_headers, regex
36
+ cookie = set_cookie_headers.find {|h| h =~ regex}
37
+ regex = /; expires=(.*?)(;|$)/
38
+ if should_expire_at
39
+ assert_contains cookie, regex, "cookie does not contain an 'expires=' attribute"
40
+ cookie =~ regex
41
+ expires_at = Time.parse($1)
42
+ assert_in_delta should_expire_at, expires_at, 100 # number of seconds we don't expect the test suite to exceed
43
+ else
44
+ assert_does_not_contain cookie, regex, "cookie contains an 'expires=' attribute but it shouldn't"
45
+ end
46
+ end
47
+ end
19
48
  end