simple_form 0.5 → 1.0.0

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

Potentially problematic release.


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

Files changed (48) hide show
  1. data/README.rdoc +339 -6
  2. data/generators/simple_form_install/USAGE +3 -0
  3. data/generators/simple_form_install/simple_form_install_generator.rb +19 -0
  4. data/generators/simple_form_install/templates/simple_form.rb +38 -0
  5. data/init.rb +1 -0
  6. data/lib/simple_form.rb +57 -1
  7. data/lib/simple_form/action_view_extensions/builder.rb +122 -0
  8. data/lib/simple_form/action_view_extensions/form_helper.rb +33 -0
  9. data/lib/simple_form/action_view_extensions/instance_tag.rb +37 -0
  10. data/lib/simple_form/components.rb +8 -0
  11. data/lib/simple_form/components/errors.rb +35 -0
  12. data/lib/simple_form/components/hints.rb +21 -0
  13. data/lib/simple_form/components/labels.rb +68 -0
  14. data/lib/simple_form/components/wrapper.rb +21 -0
  15. data/lib/simple_form/form_builder.rb +332 -0
  16. data/lib/simple_form/i18n_cache.rb +22 -0
  17. data/lib/simple_form/inputs.rb +12 -0
  18. data/lib/simple_form/inputs/base.rb +107 -0
  19. data/lib/simple_form/inputs/block_input.rb +13 -0
  20. data/lib/simple_form/inputs/collection_input.rb +58 -0
  21. data/lib/simple_form/inputs/date_time_input.rb +18 -0
  22. data/lib/simple_form/inputs/hidden_input.rb +11 -0
  23. data/lib/simple_form/inputs/mapping_input.rb +23 -0
  24. data/lib/simple_form/inputs/priority_input.rb +20 -0
  25. data/lib/simple_form/inputs/text_field_input.rb +16 -0
  26. data/lib/simple_form/locale/en.yml +14 -0
  27. data/lib/simple_form/map_type.rb +13 -0
  28. data/lib/simple_form/version.rb +3 -0
  29. data/test/action_view_extensions/builder_test.rb +172 -0
  30. data/test/action_view_extensions/form_helper_test.rb +50 -0
  31. data/test/components/error_test.rb +45 -0
  32. data/test/components/hint_test.rb +78 -0
  33. data/test/components/label_test.rb +170 -0
  34. data/test/form_builder_test.rb +550 -0
  35. data/test/inputs_test.rb +337 -0
  36. data/test/simple_form_test.rb +9 -0
  37. data/test/support/country_select/init.rb +1 -0
  38. data/test/support/country_select/install.rb +2 -0
  39. data/test/support/country_select/lib/country_select.rb +84 -0
  40. data/test/support/country_select/uninstall.rb +1 -0
  41. data/test/support/misc_helpers.rb +29 -0
  42. data/test/support/mock_controller.rb +11 -0
  43. data/test/support/mock_response.rb +14 -0
  44. data/test/support/models.rb +100 -0
  45. data/test/test_helper.rb +60 -0
  46. metadata +50 -10
  47. data/CHANGELOG +0 -27
  48. data/Rakefile +0 -17
@@ -1,9 +1,342 @@
1
- == Deprecation
1
+ == SimpleForm
2
2
 
3
- SimpleForm is deprecated in favor of MailForm:
3
+ Forms made easy (for Rails)!
4
4
 
5
- http://github.com/plataformatec/mail_form/tree/v1.0
6
-
7
- For a Rails 2.3 version, please install:
5
+ SimpleForm aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of simple form is to not touch your way of defining the layout, this way letting you find how you find the better design for your eyes. Good part of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.
8
6
 
9
- gem install mail_form --version=1.0
7
+ == Installation
8
+
9
+ Install the gem:
10
+
11
+ sudo gem install simple_form --version=1.0
12
+
13
+ Configure simple_form gem inside your app:
14
+
15
+ config.gem 'simple_form'
16
+
17
+ Run the generator:
18
+
19
+ ruby script/generate simple_form_install
20
+
21
+ And you're ready to go.
22
+
23
+ == Usage
24
+
25
+ SimpleForm was designed to be customized as you need to. Basically it's a stack of components that are invoked to create a complete html input for you, which by default contains label, hints, errors and the input itself. It does not aim to create a lot of different logic from the default Rails form helpers, as they do a great work by themselves. Instead, SimpleForm acts as a DSL and just maps your input type (retrieved from the column definition in the database) to an specific helper method.
26
+
27
+ To start using SimpleForm you just have to use the helper it provides:
28
+
29
+ <% simple_form_for @user do |f| -%>
30
+ <p><%= f.input :username %></p>
31
+ <p><%= f.input :password %></p>
32
+ <p><%= f.button :submit %></p>
33
+ <% end -%>
34
+
35
+ This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).
36
+
37
+ You can overwrite the default label by passing it to the input method, or even add a hint:
38
+
39
+ <% simple_form_for @user do |f| -%>
40
+ <p><%= f.input :username, :label => 'Your username please' %></p>
41
+ <p><%= f.input :password, :hint => 'No special characters.' %></p>
42
+ <p><%= f.button :submit %></p>
43
+ <% end -%>
44
+
45
+ You can also disable labels, hints or error or configure the html of any of them:
46
+
47
+ <% simple_form_for @user do |f| -%>
48
+ <p><%= f.input :username, :label_html => { :class => 'my_class' } %></p>
49
+ <p><%= f.input :password, :hint => false, :error_html => { :id => "password_error"} %></p>
50
+ <p><%= f.input :password_confirmation, :label => false %></p>
51
+ <p><%= f.button :submit %></p>
52
+ <% end -%>
53
+
54
+ By default all inputs are required, which means an * is prepended to the label, but you can disable it in any input you want:
55
+
56
+ <% simple_form_for @user do |f| -%>
57
+ <p><%= f.input :name, :required => false %></p>
58
+ <p><%= f.input :username %></p>
59
+ <p><%= f.input :password %></p>
60
+ <p><%= f.button :submit %></p>
61
+ <% end -%>
62
+
63
+ SimpleForm also lets you overwrite the default input type it creates:
64
+
65
+ <% simple_form_for @user do |f| -%>
66
+ <p><%= f.input :username %></p>
67
+ <p><%= f.input :password %></p>
68
+ <p><%= f.input :description, :as => :text %></p>
69
+ <p><%= f.input :accepts, :as => :radio %></p>
70
+ <p><%= f.button :submit %></p>
71
+ <% end -%>
72
+
73
+ So instead of a checkbox for the :accepts attribute, you'll have a pair of radio buttons with yes/no labels and a text area instead of a text field for the description. You can also render boolean attributes using :as => :select to show a dropdown.
74
+
75
+ SimpleForm also allows you using label, hint and error helpers it provides:
76
+
77
+ <% simple_form_for @user do |f| -%>
78
+ <p><%= f.label :username %></p>
79
+ <p><%= f.text_field :username %></p>
80
+ <p><%= f.error :username, :id => 'user_name_error' %></p>
81
+ <p><%= f.hint 'No special characters, please!' %></p>
82
+ <p><%= f.submit 'Save' %></p>
83
+ <% end -%>
84
+
85
+ Any extra option passed to label, hint or error will be rendered as html option.
86
+
87
+ === Collections
88
+
89
+ And what if you want to create a select containing the age from 18 to 60 in your form? You can do it overriding the :collection option:
90
+
91
+ <% simple_form_for @user do |f| -%>
92
+ <p><%= f.input :user %></p>
93
+ <p><%= f.input :age, :collection => 18..60 %></p>
94
+ <p><%= f.button :submit %></p>
95
+ <% end -%>
96
+
97
+ Collections can be arrays or ranges, and when a :collection is given the :select input will be rendered by default, so we don't need to pass the :as => :select option. Other types of collection are :radio and :check_boxes. Those are added by SimpleForm to Rails set of form helpers (read Extra Helpers session below for more information).
98
+
99
+ Collection inputs accepts two other options beside collections:
100
+
101
+ * label_method => the label method to be applied to the collection to retrieve the label
102
+
103
+ * value_method => the value method to be applied to the collection to retrieve the value
104
+
105
+ Those methods are useful to manipulate the given collection. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
106
+
107
+ f.input :age, :collection => 18..60, :prompt => "Select your age"
108
+
109
+ === Priority
110
+
111
+ SimpleForm also supports :time_zone and :country. When using such helpers, you can give :priority as option to select which time zones and/or countries should be given higher priority:
112
+
113
+ f.input :residence_country, :priority => [ "Brazil" ]
114
+ f.input :time_zone, :priority => /US/
115
+
116
+ Those values can also be configured with a default value to be used site use through the SimpleForm.country_priority and SimpleForm.time_zone_priority helpers.
117
+
118
+ === Wrapper
119
+
120
+ SimpleForm allows you to add a wrapper which contains the label, error, hint and input.
121
+ The first step is to configure a wrapper tag:
122
+
123
+ SimpleForm.wrapper_tag = :p
124
+
125
+ And now, you don't need to wrap your f.input calls with <p> anymore:
126
+
127
+ <% simple_form_for @user do |f| -%>
128
+ <%= f.input :username %>
129
+ <%= f.input :password %>
130
+ <p><%= f.button :submit %></p>
131
+ <% end -%>
132
+
133
+ == Associations
134
+
135
+ To deal with associations, SimpleForm can generate select inputs or a series of radios or check boxes. Lets see how it works: imagine you have the user model that belongs to a company and has_and_belongs_to_many roles. The structure should be something like:
136
+
137
+ class User < ActiveRecord::Base
138
+ belongs_to :company
139
+ has_and_belongs_to_many :roles
140
+ end
141
+
142
+ class Company < ActiveRecord::Base
143
+ has_many :users
144
+ end
145
+
146
+ class Role < ActiveRecord::Base
147
+ has_and_belongs_to_many :users
148
+ end
149
+
150
+ Now we have the user form:
151
+
152
+ <% simple_form_for @user do |f| -%>
153
+ <p><%= f.input :name %></p>
154
+ <p><%= f.association :company %></p>
155
+ <p><%= f.association :roles %></p>
156
+ <p><%= f.button :submit %></p>
157
+ <% end -%>
158
+
159
+ Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well:
160
+
161
+ f.association :company, :as => :radio
162
+ f.association :roles, :as => :check_boxes
163
+
164
+ And you will get a set of radios to select the company and another set of check boxes for the roles. Some options are available for refining the collection for associations: :conditions, :include, :joins, :order. These options are given straight to the find method. Here is an example of how to use these options:
165
+
166
+ f.association :company, :order => 'name'
167
+ f.association :roles, :conditions => { :active => true }
168
+
169
+ You also have the ability to call named scopes using the association:
170
+
171
+ f.association :company, :scope => [:active, :available]
172
+
173
+ The association helper just invokes input under the hood, so all options available to :select, :radio and :check_boxes are also available to association. For example, you can specify the collection by hand, all together with the prompt:
174
+
175
+ f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"
176
+
177
+ == Buttons
178
+
179
+ All web forms need buttons, right? To help you with this, SimpleForm has a default button helper that acts as a wrapper for Rails helpers, creating submit texts using I18n. It's pretty straightforward:
180
+
181
+ <% simple_form_for @user do |f| -%>
182
+ <p><%= f.input :name %></p>
183
+ <p><%= f.button :submit %></p>
184
+ <% end -%>
185
+
186
+ This will create a input submit tag for you. If the resource @user is a new record, the button will have text 'Create User', otherwise 'Update User' is shown. If you are working with forms without objects, 'Submit User' would be shown. All those buttons labels can be configured using I18n through the keys :simple_form.buttons.create, :simple_form.buttons.update and :simple_form.buttons.submit.
187
+
188
+ You can also pass the button text directly:
189
+
190
+ f.button :submit, 'Save User'
191
+ f.button :submit, :label => 'Save User'
192
+
193
+ As button is just a wrapper, it is actually calling Rails helper :submit_tag with the provided text. That said, any other option you pass will be given to :submit_tag call:
194
+
195
+ f.button :submit, :confirm => 'Are you sure?'
196
+
197
+ And if you want to use any other button tag, you just need to create a helper that ends with "_tag":
198
+
199
+ # inside ApplicationHelper for instance
200
+ def custom_submit_tag(text, options={})
201
+ # render submit tag here
202
+ end
203
+
204
+ And you will able to use it with SimpleForm:
205
+
206
+ f.button :custom_submit
207
+
208
+ == Extra helpers
209
+
210
+ SimpleForm also comes with some extra helpers you can use inside rails default forms without relying on simple_form_for helper. They are listed below.
211
+
212
+ === Simple Fields For
213
+
214
+ Wrapper to use simple form inside a default rails form
215
+
216
+ form_for @user do |f|
217
+ f.simple_fields_for :posts do |posts_form|
218
+ # Here you have all simple_form methods available
219
+ posts_form.input :title
220
+ end
221
+ end
222
+
223
+ === Collection Radio
224
+
225
+ Creates a collection of radio inputs with labels associated (same API as collection_select):
226
+
227
+ form_for @user do |f|
228
+ f.collection_radio :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
229
+ end
230
+
231
+ <input id="user_options_true" name="user[options]" type="radio" value="true" />
232
+ <label class="collection_radio" for="user_options_true">Yes</label>
233
+ <input id="user_options_false" name="user[options]" type="radio" value="false" />
234
+ <label class="collection_radio" for="user_options_false">No</label>
235
+
236
+ === Collection Check Box
237
+
238
+ Creates a collection of check boxes with labels associated (same API as collection_select):
239
+
240
+ form_for @user do |f|
241
+ f.collection_check_box :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
242
+ end
243
+
244
+ <input name="user[options][]" type="hidden" value="" />
245
+ <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
246
+ <label class="collection_check_box" for="user_options_true">Yes</label>
247
+ <input name="user[options][]" type="hidden" value="" />
248
+ <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
249
+ <label class="collection_check_box" for="user_options_false">No</label>
250
+
251
+ == Mappings/Inputs available
252
+
253
+ SimpleForm comes with a lot of default mappings:
254
+
255
+ Mapping Input Column Type
256
+
257
+ boolean check box boolean
258
+ string text field string
259
+ password password field string with name matching "password"
260
+ text text area text
261
+ file file field string, responding to file methods
262
+ hidden hidden field -
263
+ integer text field integer
264
+ float text field float
265
+ decimal text field decimal
266
+ datetime datetime select datetime/timestamp
267
+ date date select date
268
+ time time select time
269
+ select collection select belongs_to/has_many/has_and_belongs_to_many associations
270
+ radio collection radio belongs_to
271
+ check_boxes collection check box has_many/has_and_belongs_to_many associations
272
+ country country select string with name matching "country"
273
+ time_zone time zone select string with name matching "time_zone"
274
+
275
+ == I18n
276
+
277
+ SimpleForm uses all power of I18n API to lookup labels and hints for you. To customize your forms you can create a locale file like this:
278
+
279
+ en:
280
+ simple_form:
281
+ labels:
282
+ user:
283
+ username: 'User name'
284
+ password: 'Password'
285
+ hints:
286
+ user:
287
+ username: 'User name to sign in.'
288
+ password: 'No special characters, please.'
289
+
290
+ And your forms will use this information to render labels and hints for you.
291
+
292
+ SimpleForm also lets you be more specific, separating lookups through actions for both hint and label. Let's say you want a different label for new and edit actions, the locale file would be something like:
293
+
294
+ en:
295
+ simple_form:
296
+ labels:
297
+ user:
298
+ username: 'User name'
299
+ password: 'Password'
300
+ edit:
301
+ username: 'Change user name'
302
+ password: 'Change password'
303
+
304
+ This way SimpleForm will figure out the right translation for you, based on the action being rendered. And to be a little bit DRYer with your locale file, you can skip the model information inside it:
305
+
306
+ en:
307
+ simple_form:
308
+ labels:
309
+ username: 'User name'
310
+ password: 'Password'
311
+ hints:
312
+ username: 'User name to sign in.'
313
+ password: 'No special characters, please.'
314
+
315
+ SimpleForm will always look for a default attribute translation if no specific is found inside the model key. In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other translation is found.
316
+
317
+ Finally, you can also overwrite labels and hints inside your view, just by passing the label/hint manually. This way the I18n lookup will be skipped.
318
+
319
+ There are other options that can be configured through I18n API, such as required text, boolean and button texts. Be sure to check our locale file or the one copied to your application after you run "script/generate simple_form_install".
320
+
321
+ == Configuration
322
+
323
+ SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
324
+
325
+ ruby script/generate simple_form_install
326
+
327
+ == TODO
328
+
329
+ Please refer to TODO file.
330
+
331
+ == Maintainers
332
+
333
+ * José Valim (http://github.com/josevalim)
334
+ * Carlos Antonio da Silva (http://github.com/carlosantoniodasilva)
335
+
336
+ == Bugs and Feedback
337
+
338
+ If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
339
+
340
+ http://github.com/plataformatec/simple_form/issues
341
+
342
+ MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
@@ -0,0 +1,3 @@
1
+ To copy a SimpleForm initializer to your Rails App, with some configuration values, just do:
2
+
3
+ script/generate simple_form_install
@@ -0,0 +1,19 @@
1
+ class SimpleFormInstallGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.directory 'config/initializers'
6
+ m.template 'simple_form.rb', 'config/initializers/simple_form.rb'
7
+
8
+ m.directory 'config/locales'
9
+ m.template locale_file, 'config/locales/simple_form.en.yml'
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def locale_file
16
+ @locale_file ||= '../../../lib/simple_form/locale/en.yml'
17
+ end
18
+
19
+ end
@@ -0,0 +1,38 @@
1
+ # Use this setup block to configure all options available in SimpleForm.
2
+ SimpleForm.setup do |config|
3
+
4
+ # Components used by the form builder to generate a complete input. You can
5
+ # remove any of them, change the order, or even add your own components in the
6
+ # stack. By inheriting your component from SimpleForm::Components::Base you'll
7
+ # have some extra helpers for free.
8
+ # config.components = [
9
+ # SimpleForm::Components::Wrapper, SimpleForm::Components::Label,
10
+ # SimpleForm::Components::Input, SimpleForm::Components::Hint,
11
+ # SimpleForm::Components::Error
12
+ #]
13
+
14
+ # Default tag used in components (hints and errors basically). When using one
15
+ # of these components, this tag will be used to render it.
16
+ # config.component_tag = :span
17
+
18
+ # You can wrap all inputs in a pre-defined tag. By default is nil.
19
+ # config.wrapper_tag = nil
20
+
21
+ # How the label text should be generated altogether with the required text.
22
+ # config.label_text = lambda { |label, required| "#{required} #{label}" }
23
+
24
+ # Series of attemps to detect a default label method for collection
25
+ # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
26
+
27
+ # Series of attemps to detect a default value method for collection
28
+ # config.collection_value_methods = [ :id, :to_s ]
29
+
30
+ # Collection of methods to detect if a file type was given.
31
+ # config.file_methods = [ :file?, :public_filename ]
32
+
33
+ # Default priority for time_zone inputs.
34
+ # config.time_zone_priority = nil
35
+
36
+ # Default priority for country inputs.
37
+ # config.country_priority = nil
38
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'simple_form'
@@ -1 +1,57 @@
1
- raise RuntimeError, "\n\n" + File.read(File.expand_path("../README.rdoc", File.dirname(__FILE__)))
1
+ require 'simple_form/action_view_extensions/form_helper'
2
+ require 'simple_form/action_view_extensions/builder'
3
+ require 'simple_form/action_view_extensions/instance_tag'
4
+
5
+ module SimpleForm
6
+ autoload :Components, 'simple_form/components'
7
+ autoload :FormBuilder, 'simple_form/form_builder'
8
+ autoload :I18nCache, 'simple_form/i18n_cache'
9
+ autoload :Inputs, 'simple_form/inputs'
10
+ autoload :MapType, 'simple_form/map_type'
11
+
12
+ # Default tag used in hints.
13
+ mattr_accessor :hint_tag
14
+ @@hint_tag = :span
15
+
16
+ # Default tag used in errors.
17
+ mattr_accessor :error_tag
18
+ @@error_tag = :span
19
+
20
+ # Components used by the form builder.
21
+ mattr_accessor :components
22
+ @@components = [ :label, :input, :hint, :error ]
23
+
24
+ # Series of attemps to detect a default label method for collection.
25
+ mattr_accessor :collection_label_methods
26
+ @@collection_label_methods = [ :to_label, :name, :title, :to_s ]
27
+
28
+ # Series of attemps to detect a default value method for collection.
29
+ mattr_accessor :collection_value_methods
30
+ @@collection_value_methods = [ :id, :to_s ]
31
+
32
+ # You can wrap all inputs in a pre-defined tag. By default is nil.
33
+ mattr_accessor :wrapper_tag
34
+ @@wrapper_tag = nil
35
+
36
+ # How the label text should be generated altogether with the required text.
37
+ mattr_accessor :label_text
38
+ @@label_text = lambda { |label, required| "#{required} #{label}" }
39
+
40
+ # Collection of methods to detect if a file type was given.
41
+ mattr_accessor :file_methods
42
+ @@file_methods = [ :mounted_as, :file?, :public_filename ]
43
+
44
+ # Default priority for time_zone inputs.
45
+ mattr_accessor :time_zone_priority
46
+ @@time_zone_priority = nil
47
+
48
+ # Default priority for country inputs.
49
+ mattr_accessor :country_priority
50
+ @@country_priority = nil
51
+
52
+ # Default way to setup SimpleForm. Run script/generate simple_form_install
53
+ # to create a fresh initializer with all configuration values.
54
+ def self.setup
55
+ yield self
56
+ end
57
+ end