formtastic 1.2.5 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.yardopts +1 -0
- data/CHANGELOG +279 -0
- data/Gemfile +3 -0
- data/README.textile +155 -172
- data/RELEASE_PROCESS +7 -0
- data/Rakefile +52 -0
- data/app/assets/stylesheets/formtastic.css +275 -0
- data/app/assets/stylesheets/formtastic_ie6.css +27 -0
- data/app/assets/stylesheets/formtastic_ie7.css +17 -0
- data/formtastic.gemspec +51 -0
- data/lib/formtastic.rb +21 -1960
- data/lib/formtastic/engine.rb +7 -0
- data/lib/formtastic/form_builder.rb +83 -0
- data/lib/formtastic/helpers.rb +16 -0
- data/lib/formtastic/helpers/buttons_helper.rb +277 -0
- data/lib/formtastic/helpers/errors_helper.rb +113 -0
- data/lib/formtastic/helpers/fieldset_wrapper.rb +75 -0
- data/lib/formtastic/helpers/file_column_detection.rb +16 -0
- data/lib/formtastic/helpers/form_helper.rb +198 -0
- data/lib/formtastic/helpers/input_helper.rb +366 -0
- data/lib/formtastic/helpers/inputs_helper.rb +392 -0
- data/lib/formtastic/helpers/reflection.rb +33 -0
- data/lib/formtastic/helpers/semantic_form_helper.rb +11 -0
- data/lib/formtastic/html_attributes.rb +21 -0
- data/lib/formtastic/i18n.rb +1 -0
- data/lib/formtastic/inputs.rb +31 -0
- data/lib/formtastic/inputs/base.rb +61 -0
- data/lib/formtastic/inputs/base/associations.rb +31 -0
- data/lib/formtastic/inputs/base/choices.rb +103 -0
- data/lib/formtastic/inputs/base/collections.rb +94 -0
- data/lib/formtastic/inputs/base/database.rb +17 -0
- data/lib/formtastic/inputs/base/errors.rb +58 -0
- data/lib/formtastic/inputs/base/fileish.rb +23 -0
- data/lib/formtastic/inputs/base/grouped_collections.rb +77 -0
- data/lib/formtastic/inputs/base/hints.rb +31 -0
- data/lib/formtastic/inputs/base/html.rb +52 -0
- data/lib/formtastic/inputs/base/labelling.rb +55 -0
- data/lib/formtastic/inputs/base/naming.rb +42 -0
- data/lib/formtastic/inputs/base/options.rb +18 -0
- data/lib/formtastic/inputs/base/stringish.rb +35 -0
- data/lib/formtastic/inputs/base/timeish.rb +128 -0
- data/lib/formtastic/inputs/base/validations.rb +166 -0
- data/lib/formtastic/inputs/base/wrapping.rb +40 -0
- data/lib/formtastic/inputs/boolean_input.rb +96 -0
- data/lib/formtastic/inputs/check_boxes_input.rb +179 -0
- data/lib/formtastic/inputs/country_input.rb +66 -0
- data/lib/formtastic/inputs/date_input.rb +14 -0
- data/lib/formtastic/inputs/datetime_input.rb +9 -0
- data/lib/formtastic/inputs/email_input.rb +40 -0
- data/lib/formtastic/inputs/file_input.rb +42 -0
- data/lib/formtastic/inputs/hidden_input.rb +66 -0
- data/lib/formtastic/inputs/number_input.rb +118 -0
- data/lib/formtastic/inputs/numeric_input.rb +21 -0
- data/lib/formtastic/inputs/password_input.rb +40 -0
- data/lib/formtastic/inputs/phone_input.rb +41 -0
- data/lib/formtastic/inputs/radio_input.rb +157 -0
- data/lib/formtastic/inputs/range_input.rb +119 -0
- data/lib/formtastic/inputs/search_input.rb +40 -0
- data/lib/formtastic/inputs/select_input.rb +210 -0
- data/lib/formtastic/inputs/string_input.rb +34 -0
- data/lib/formtastic/inputs/text_input.rb +47 -0
- data/lib/formtastic/inputs/time_input.rb +14 -0
- data/lib/formtastic/inputs/time_zone_input.rb +48 -0
- data/lib/formtastic/inputs/url_input.rb +40 -0
- data/lib/formtastic/localized_string.rb +105 -0
- data/lib/formtastic/railtie.rb +5 -7
- data/lib/formtastic/semantic_form_builder.rb +11 -0
- data/lib/formtastic/util.rb +6 -19
- data/lib/formtastic/version.rb +3 -0
- data/lib/generators/formtastic/install/install_generator.rb +28 -6
- data/lib/generators/templates/_form.html.erb +10 -4
- data/lib/generators/templates/_form.html.haml +8 -4
- data/lib/generators/templates/formtastic.rb +25 -32
- data/lib/locale/en.yml +1 -0
- data/lib/tasks/verify_rcov.rb +44 -0
- data/sample/basic_inputs.html +182 -0
- data/sample/config.ru +69 -0
- data/sample/index.html +14 -0
- data/spec/builder/custom_builder_spec.rb +109 -0
- data/spec/builder/error_proc_spec.rb +27 -0
- data/spec/builder/errors_spec.rb +193 -0
- data/spec/builder/semantic_fields_for_spec.rb +88 -0
- data/spec/helpers/buttons_helper_spec.rb +150 -0
- data/spec/helpers/commit_button_helper_spec.rb +470 -0
- data/spec/helpers/form_helper_spec.rb +135 -0
- data/spec/helpers/input_helper_spec.rb +837 -0
- data/spec/helpers/inputs_helper_spec.rb +562 -0
- data/spec/helpers/semantic_errors_helper_spec.rb +112 -0
- data/spec/i18n_spec.rb +199 -0
- data/spec/inputs/boolean_input_spec.rb +184 -0
- data/spec/inputs/check_boxes_input_spec.rb +375 -0
- data/spec/inputs/country_input_spec.rb +133 -0
- data/spec/inputs/custom_input_spec.rb +52 -0
- data/spec/inputs/date_input_spec.rb +110 -0
- data/spec/inputs/datetime_input_spec.rb +115 -0
- data/spec/inputs/email_input_spec.rb +55 -0
- data/spec/inputs/file_input_spec.rb +59 -0
- data/spec/inputs/hidden_input_spec.rb +120 -0
- data/spec/inputs/include_blank_spec.rb +70 -0
- data/spec/inputs/label_spec.rb +104 -0
- data/spec/inputs/number_input_spec.rb +487 -0
- data/spec/inputs/numeric_input_spec.rb +41 -0
- data/spec/inputs/password_input_spec.rb +69 -0
- data/spec/inputs/phone_input_spec.rb +55 -0
- data/spec/inputs/placeholder_spec.rb +71 -0
- data/spec/inputs/radio_input_spec.rb +234 -0
- data/spec/inputs/range_input_spec.rb +477 -0
- data/spec/inputs/search_input_spec.rb +55 -0
- data/spec/inputs/select_input_spec.rb +545 -0
- data/spec/inputs/string_input_spec.rb +163 -0
- data/spec/inputs/text_input_spec.rb +158 -0
- data/spec/inputs/time_input_spec.rb +155 -0
- data/spec/inputs/time_zone_input_spec.rb +87 -0
- data/spec/inputs/url_input_spec.rb +55 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +361 -0
- data/spec/support/custom_macros.rb +656 -0
- data/spec/support/deferred_garbage_collection.rb +21 -0
- data/spec/support/deprecation.rb +6 -0
- data/spec/support/test_environment.rb +30 -0
- metadata +306 -88
- data/generators/form/USAGE +0 -16
- data/generators/form/form_generator.rb +0 -111
- data/generators/formtastic/formtastic_generator.rb +0 -26
- data/init.rb +0 -5
- data/lib/formtastic/layout_helper.rb +0 -12
- data/lib/generators/formtastic/form/form_generator.rb +0 -84
- data/lib/generators/templates/formtastic.css +0 -145
- data/lib/generators/templates/formtastic_changes.css +0 -14
- data/lib/generators/templates/rails2/_form.html.erb +0 -5
- data/lib/generators/templates/rails2/_form.html.haml +0 -4
- data/rails/init.rb +0 -2
@@ -0,0 +1,42 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a `<input type="file">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for objects with attributes that appear
|
6
|
+
# to be for file uploads, by detecting some common method names used by popular file upload
|
7
|
+
# libraries such as Paperclip and CarrierWave. You can add to or alter these method names
|
8
|
+
# through the `file_methods` config, but can be applied to any input with `:as => :file`.
|
9
|
+
#
|
10
|
+
# Don't forget to set the multipart attribute in your `<form>` tag!
|
11
|
+
#
|
12
|
+
# @example Full form context and output
|
13
|
+
#
|
14
|
+
# <%= semantic_form_for(@user, :html => { :multipart => true }) do |f| %>
|
15
|
+
# <%= f.inputs do %>
|
16
|
+
# <%= f.input :email_address, :as => :email %>
|
17
|
+
# <% end %>
|
18
|
+
# <% end %>
|
19
|
+
#
|
20
|
+
# <form...>
|
21
|
+
# <fieldset>
|
22
|
+
# <ol>
|
23
|
+
# <li class="email">
|
24
|
+
# <label for="user_email_address">Email address</label>
|
25
|
+
# <input type="email" id="user_email_address" name="user[email_address]">
|
26
|
+
# </li>
|
27
|
+
# </ol>
|
28
|
+
# </fieldset>
|
29
|
+
# </form>
|
30
|
+
#
|
31
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
32
|
+
class FileInput
|
33
|
+
include Base
|
34
|
+
def to_html
|
35
|
+
input_wrapping do
|
36
|
+
label_html <<
|
37
|
+
builder.file_field(method, input_html_options)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<input type="hidden">` wrapped in the standard `<li>` wrapper. This is
|
5
|
+
# provided for situations where a hidden field needs to be rendered in the flow of a form with
|
6
|
+
# many inputs that form an `<ol>`. Wrapping the hidden input inside the `<li>` maintains the
|
7
|
+
# HTML validity. The `<li>` is marked with a `class` of `hidden` so that stylesheet authors can
|
8
|
+
# hide these list items with CSS (formtastic.css does this out of the box).
|
9
|
+
#
|
10
|
+
# @example Full form context, output and CSS
|
11
|
+
#
|
12
|
+
# <%= semantic_form_for(@something) do |f| %>
|
13
|
+
# <%= f.inputs do %>
|
14
|
+
# <%= f.input :secret, :as => :hidden %>
|
15
|
+
# <% end %>
|
16
|
+
# <% end %>
|
17
|
+
#
|
18
|
+
# <form...>
|
19
|
+
# <fieldset>
|
20
|
+
# <ol>
|
21
|
+
# <li class="hidden">
|
22
|
+
# <input type="hidden" id="something_secret" name="something[secret]">
|
23
|
+
# </li>
|
24
|
+
# </ol>
|
25
|
+
# </fieldset>
|
26
|
+
# </form>
|
27
|
+
#
|
28
|
+
# form.formtastic li.hidden { display:none; }
|
29
|
+
#
|
30
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
31
|
+
class HiddenInput
|
32
|
+
include Base
|
33
|
+
|
34
|
+
# Override to include :value set directly from options hash. The :value set in :input_html
|
35
|
+
# hash will be preferred over :value set directly in the options.
|
36
|
+
#
|
37
|
+
# @todo this is inconsistent with all other inputs, deprecate and remove
|
38
|
+
def input_html_options
|
39
|
+
options.slice(:value).merge(super).merge(:required => nil).merge(:autofocus => nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_html
|
43
|
+
input_wrapping do
|
44
|
+
builder.hidden_field(method, input_html_options)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def error_html
|
49
|
+
""
|
50
|
+
end
|
51
|
+
|
52
|
+
def errors?
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def hint_html
|
57
|
+
""
|
58
|
+
end
|
59
|
+
|
60
|
+
def hint?
|
61
|
+
false
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a HTML5 `<input type="number">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for all database columns of the type `:float`
|
6
|
+
# and `:decimal`, as well as `:integer` columns that aren't used for `belongs_to` associations,
|
7
|
+
# but can be applied to any text-like input with `:as => :number`.
|
8
|
+
#
|
9
|
+
# Sensible default values for the `min`, `max` and `step` attributes are found by reflecting on
|
10
|
+
# the model's validations (when provided). An `IndeterminableMinimumAttributeError` exception
|
11
|
+
# will be raised when the following conditions are all true:
|
12
|
+
#
|
13
|
+
# * you haven't specified a `:min` or `:max` for the input
|
14
|
+
# * the model's database column type is a `:float` or `:decimal`
|
15
|
+
# * the validation uses `:less_than` or `:greater_than`
|
16
|
+
#
|
17
|
+
# The solution is to either:
|
18
|
+
#
|
19
|
+
# * manually specify the `:min` or `:max` for the input
|
20
|
+
# * change the database column type to an `:integer` (if appropriate)
|
21
|
+
# * change the validations to use `:less_than_or_equal_to` or `:greater_than_or_equal_to`
|
22
|
+
#
|
23
|
+
# @example Full form context and output
|
24
|
+
#
|
25
|
+
# <%= semantic_form_for(@user) do |f| %>
|
26
|
+
# <%= f.inputs do %>
|
27
|
+
# <%= f.input :shoe_size, :as => :number %>
|
28
|
+
# <% end %>
|
29
|
+
# <% end %>
|
30
|
+
#
|
31
|
+
# <form...>
|
32
|
+
# <fieldset>
|
33
|
+
# <ol>
|
34
|
+
# <li class="numeric">
|
35
|
+
# <label for="user_shoe_size">Shoe size</label>
|
36
|
+
# <input type="number" id="user_shoe_size" name="user[shoe_size]">
|
37
|
+
# </li>
|
38
|
+
# </ol>
|
39
|
+
# </fieldset>
|
40
|
+
# </form>
|
41
|
+
#
|
42
|
+
# @example Default HTML5 min/max/step attributes are detected from the numericality validations
|
43
|
+
#
|
44
|
+
# class Person < ActiveRecord::Base
|
45
|
+
# validates_numericality_of :age,
|
46
|
+
# :less_than_or_equal_to => 100,
|
47
|
+
# :greater_than_or_equal_to => 18,
|
48
|
+
# :only_integer => true
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# <%= f.input :age, :as => :number %>
|
52
|
+
#
|
53
|
+
# <li class="numeric">
|
54
|
+
# <label for="persom_age">Age</label>
|
55
|
+
# <input type="number" id="person_age" name="person[age]" min="18" max="100" step="1">
|
56
|
+
# </li>
|
57
|
+
#
|
58
|
+
# @example Pass attributes down to the `<input>` tag with :input_html
|
59
|
+
# <%= f.input :shoe_size, :as => :number, :input_html => { :min => 3, :max => 15, :step => 1, :class => "special" } %>
|
60
|
+
#
|
61
|
+
# @example Min/max/step also work as options
|
62
|
+
# <%= f.input :shoe_size, :as => :number, :min => 3, :max => 15, :step => 1, :input_html => { :class => "special" } %>
|
63
|
+
#
|
64
|
+
# @example Use :in with a Range as a shortcut for :min/:max
|
65
|
+
# <%= f.input :shoe_size, :as => :number, :in => 3..15, :step => 1 %>
|
66
|
+
# <%= f.input :shoe_size, :as => :number, :input_html => { :in => 3..15, :step => 1 } %>
|
67
|
+
#
|
68
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
69
|
+
# @see http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of Rails' Numericality validation documentation
|
70
|
+
class NumberInput
|
71
|
+
include Base
|
72
|
+
include Base::Stringish
|
73
|
+
|
74
|
+
def to_html
|
75
|
+
input_wrapping do
|
76
|
+
label_html <<
|
77
|
+
builder.number_field(method, input_html_options)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def input_html_options
|
82
|
+
defaults = super
|
83
|
+
|
84
|
+
if in_option
|
85
|
+
defaults[:min] = in_option.to_a.min
|
86
|
+
defaults[:max] = in_option.to_a.max
|
87
|
+
else
|
88
|
+
defaults[:min] ||= min_option
|
89
|
+
defaults[:max] ||= max_option
|
90
|
+
end
|
91
|
+
defaults[:step] ||= step_option
|
92
|
+
defaults
|
93
|
+
end
|
94
|
+
|
95
|
+
def step_option
|
96
|
+
return options[:step] if options.key?(:step)
|
97
|
+
return validation_step if validation_step
|
98
|
+
return 1 if validation_integer_only?
|
99
|
+
1
|
100
|
+
end
|
101
|
+
|
102
|
+
def min_option
|
103
|
+
return options[:min] if options.key?(:min)
|
104
|
+
validation_min
|
105
|
+
end
|
106
|
+
|
107
|
+
def max_option
|
108
|
+
return options[:max] if options.key?(:max)
|
109
|
+
validation_max
|
110
|
+
end
|
111
|
+
|
112
|
+
def in_option
|
113
|
+
options[:in]
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
# Alias for NumberInput for backwards compatibility with 1.x.
|
4
|
+
#
|
5
|
+
# @example:
|
6
|
+
# f.input :age, :as => :numeric
|
7
|
+
#
|
8
|
+
# @deprecated Use :as => :number instead
|
9
|
+
#
|
10
|
+
# @todo Remove on or after 2.1
|
11
|
+
class NumericInput < NumberInput
|
12
|
+
|
13
|
+
def initialize(builder, template, object, object_name, method, options)
|
14
|
+
ActiveSupport::Deprecation.warn(':as => :numeric has been deprecated in favor of :as => :number and will be removed on or after version 2.1', caller)
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a `<input type="password">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for all attributes matching `/password/`, but
|
6
|
+
# can be applied to any text-like input with `:as => :password`.
|
7
|
+
#
|
8
|
+
# @example Full form context and output
|
9
|
+
#
|
10
|
+
# <%= semantic_form_for(@user) do |f| %>
|
11
|
+
# <%= f.inputs do %>
|
12
|
+
# <%= f.input :password, :as => :password %>
|
13
|
+
# <% end %>
|
14
|
+
# <% end %>
|
15
|
+
#
|
16
|
+
# <form...>
|
17
|
+
# <fieldset>
|
18
|
+
# <ol>
|
19
|
+
# <li class="password">
|
20
|
+
# <label for="user_password">Password</label>
|
21
|
+
# <input type="password" id="user_password" name="user[password]">
|
22
|
+
# </li>
|
23
|
+
# </ol>
|
24
|
+
# </fieldset>
|
25
|
+
# </form>
|
26
|
+
#
|
27
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
28
|
+
class PasswordInput
|
29
|
+
include Base
|
30
|
+
include Base::Stringish
|
31
|
+
|
32
|
+
def to_html
|
33
|
+
input_wrapping do
|
34
|
+
label_html <<
|
35
|
+
builder.password_field(method, input_html_options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a HTML5 `<input type="phone">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for attributes with a name matching
|
6
|
+
# `/(phone|fax)/`, but can be applied to any text-like input with `:as => :phone`.
|
7
|
+
#
|
8
|
+
# @example Full form context and output
|
9
|
+
#
|
10
|
+
# <%= semantic_form_for(@user) do |f| %>
|
11
|
+
# <%= f.inputs do %>
|
12
|
+
# <%= f.input :mobile, :as => :phone %>
|
13
|
+
# <% end %>
|
14
|
+
# <% end %>
|
15
|
+
#
|
16
|
+
# <form...>
|
17
|
+
# <fieldset>
|
18
|
+
# <ol>
|
19
|
+
# <li class="phone">
|
20
|
+
# <label for="user_mobile">Mobile</label>
|
21
|
+
# <input type="phone" id="user_mobile" name="user[mobile]">
|
22
|
+
# </li>
|
23
|
+
# </ol>
|
24
|
+
# </fieldset>
|
25
|
+
# </form>
|
26
|
+
#
|
27
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
28
|
+
class PhoneInput
|
29
|
+
include Base
|
30
|
+
include Base::Stringish
|
31
|
+
|
32
|
+
def to_html
|
33
|
+
input_wrapping do
|
34
|
+
label_html <<
|
35
|
+
builder.phone_field(method, input_html_options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# A radio input is used to render a series of radio inputs. This is an alternative input choice
|
5
|
+
# for `belongs_to` associations like a `Post` belonging to a `Section` or an `Author`, or any
|
6
|
+
# case where the user needs to make a single selection from a pre-defined collectioon of choices.
|
7
|
+
#
|
8
|
+
# Within the standard `<li>` wrapper, the output is a `<fieldset>` with a `<legend>` to
|
9
|
+
# represent the "label" for the input, and an `<ol>` containing `<li>`s for each choice in
|
10
|
+
# the association. Each `<li>` choice has a `<label>` containing an `<input type="radio">` and
|
11
|
+
# the label text to describe each choice.
|
12
|
+
#
|
13
|
+
# Radio inputs can be considered as an alternative where a (non-multi) select input is used,
|
14
|
+
# especially in cases where there are only a few choices, however they are not used by default
|
15
|
+
# for any type of association or model attribute. You can choose to use a radio input instead of
|
16
|
+
# a select with `:as => :radio`.
|
17
|
+
#
|
18
|
+
# Like a select input, the flexibility of the `:collection` option (see examples) makes the
|
19
|
+
# :radio input viable as an alternative for many other input types. For example, instead of...
|
20
|
+
#
|
21
|
+
# * a `:string` input (where you want to force the user to choose from a few specific strings rather than entering anything)
|
22
|
+
# * a `:boolean` checkbox input (where the user could choose yes or no, rather than checking a box)
|
23
|
+
# * a `:date`, `:time` or `:datetime` input (where the user could choose from a small set of pre-determined dates)
|
24
|
+
# * a `:number` input (where the user could choose from a small set of pre-defined numbers)
|
25
|
+
# * a `:time_zone` input (where you want to provide your own small set of choices instead of relying on Rails)
|
26
|
+
# * a `:country` input (where you want to provide a small set of choices, no need for a plugin really)
|
27
|
+
#
|
28
|
+
# For radio inputs that map to associations on the object model, Formtastic will automatically
|
29
|
+
# load in a collection of objects on the association as options to choose from. This might be an
|
30
|
+
# `Author.all` on a `Post` form with an input for a `belongs_to :user` association, or a
|
31
|
+
# `Section.all` for a `Post` form with an input for a `belongs_to :section` association.
|
32
|
+
# You can override or customise this collection through the `:collection` option (see examples).
|
33
|
+
#
|
34
|
+
# The way on which Formtastic renders the `value` attribute and label for each choice is
|
35
|
+
# customisable through the `:member_label` and `:member_value` options (see examples below).
|
36
|
+
# When not provided, we fall back to a list of methods to try on each object such as
|
37
|
+
# `:to_label`, `:name` and `:to_s`, which are defined in the configurations
|
38
|
+
# `collection_label_methods` and `collection_value_methods`.
|
39
|
+
#
|
40
|
+
# @example Basic `belongs_to` example with full form context
|
41
|
+
#
|
42
|
+
# <%= semantic_form_for @post do |f| %>
|
43
|
+
# <%= f.inputs do %>
|
44
|
+
# <%= f.input :author, :as => :radio %>
|
45
|
+
# <% end %>
|
46
|
+
# <% end %>
|
47
|
+
#
|
48
|
+
# <form...>
|
49
|
+
# <fieldset>
|
50
|
+
# <ol>
|
51
|
+
# <li class='radio'>
|
52
|
+
# <fieldset>
|
53
|
+
# <legend class="label"><label>Categories</label></legend>
|
54
|
+
# <ol>
|
55
|
+
# <li>
|
56
|
+
# <label for="post_author_id_1">
|
57
|
+
# <input type="radio" id="post_author_id_1" value="1"> Justin
|
58
|
+
# </label>
|
59
|
+
# </li>
|
60
|
+
# <li>
|
61
|
+
# <label for="post_author_id_3">
|
62
|
+
# <input type="radio" id="post_author_id_3" value="3"> Kate
|
63
|
+
# </label>
|
64
|
+
# </li>
|
65
|
+
# <li>
|
66
|
+
# <label for="post_author_id_2">
|
67
|
+
# <input type="radio" id="post_author_id_2" value="2"> Amelia
|
68
|
+
# </label>
|
69
|
+
# </li>
|
70
|
+
# </ol>
|
71
|
+
# </fieldset>
|
72
|
+
# </li>
|
73
|
+
# </ol>
|
74
|
+
# </fieldset>
|
75
|
+
# </form>
|
76
|
+
#
|
77
|
+
# @example The `:collection` option can be used to customize the choices
|
78
|
+
# <%= f.input :author, :as => :radio, :collection => @authors %>
|
79
|
+
# <%= f.input :author, :as => :radio, :collection => Author.all %>
|
80
|
+
# <%= f.input :author, :as => :radio, :collection => Author.some_named_scope %>
|
81
|
+
# <%= f.input :author, :as => :radio, :collection => [Author.find_by_login("justin"), Category.find_by_name("kate")] %>
|
82
|
+
# <%= f.input :author, :as => :radio, :collection => ["Justin", "Kate"] %>
|
83
|
+
# <%= f.input :author, :as => :radio, :collection => [["Justin", "justin"], ["Kate", "kate"]] %>
|
84
|
+
# <%= f.input :author, :as => :radio, :collection => [["Justin", "1"], ["Kate", "3"]] %>
|
85
|
+
# <%= f.input :author, :as => :radio, :collection => [["Justin", 1], ["Kate", 3]] %>
|
86
|
+
# <%= f.input :author, :as => :radio, :collection => 1..5 %>
|
87
|
+
#
|
88
|
+
# @example The `:member_label` can be used to call a different method (or a Proc) on each object in the collection for rendering the label text (it'll try the methods like `to_s` in `collection_label_methods` config by default)
|
89
|
+
# <%= f.input :author, :as => :radio, :member_label => :name %>
|
90
|
+
# <%= f.input :author, :as => :radio, :member_label => :name_with_post_count
|
91
|
+
# <%= f.input :author, :as => :radio, :member_label => Proc.new { |a| "#{c.name} (#{pluralize("post", a.posts.count)})" }
|
92
|
+
#
|
93
|
+
# @example `:member_label` can be used with a helper method (both examples have the same result)
|
94
|
+
# <%= f.input :author, :as => :radio, :member_label => method(:fancy_label)
|
95
|
+
# <%= f.input :author, :as => :radio, :member_label => Proc.new { |author| fancy_label(author) }
|
96
|
+
#
|
97
|
+
# @example The `:member_value` can be used to call a different method (or a Proc) on each object in the collection for rendering the value for each checkbox (it'll try the methods like `id` in `collection_value_methods` config by default)
|
98
|
+
# <%= f.input :author, :as => :radio, :member_value => :login %>
|
99
|
+
# <%= f.input :author, :as => :radio, :member_value => Proc.new { |c| c.full_name.downcase.underscore }
|
100
|
+
#
|
101
|
+
# @example `:member_value` can be used with a helper method (both examples have the same result)
|
102
|
+
# <%= f.input :author, :as => :radio, :member_value => method(:some_helper)
|
103
|
+
# <%= f.input :author, :as => :radio, :member_value => Proc.new { |author| some_helper(author) }
|
104
|
+
#
|
105
|
+
# @example Set HTML attributes on each `<input type="radio">` tag with `:input_html`
|
106
|
+
# <%= f.input :author, :as => :radio, :input_html => { :size => 20, :multiple => true, :class => "special" } %>
|
107
|
+
#
|
108
|
+
# @example Set HTML attributes on the `<li>` wrapper with `:wrapper_html`
|
109
|
+
# <%= f.input :author, :as => :radio, :wrapper_html => { :class => "special" } %>
|
110
|
+
#
|
111
|
+
# @example `:value_as_class` can be used to add a class to the `<li>` wrapped around each choice using the radio value for custom styling of each choice
|
112
|
+
# <%= f.input :author, :as => :radio, :value_as_class => true %>
|
113
|
+
#
|
114
|
+
# @example Set HTML options on a specific radio input option with a 3rd element in the array for a collection member
|
115
|
+
# <%= f.input :author, :as => :radio, :collection => [["Test", 'test'], ["Try", "try", {:disabled => true}]]
|
116
|
+
#
|
117
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
118
|
+
# @see Formtastic::Inputs::RadioInput as an alternative for `belongs_to` associations
|
119
|
+
#
|
120
|
+
# @todo :disabled like CheckBoxes?
|
121
|
+
class RadioInput
|
122
|
+
include Base
|
123
|
+
include Base::Collections
|
124
|
+
include Base::Choices
|
125
|
+
|
126
|
+
def to_html
|
127
|
+
input_wrapping do
|
128
|
+
choices_wrapping do
|
129
|
+
legend_html <<
|
130
|
+
choices_group_wrapping do
|
131
|
+
collection.map { |choice|
|
132
|
+
choice_wrapping(choice_wrapping_html_options(choice)) do
|
133
|
+
choice_html(choice)
|
134
|
+
end
|
135
|
+
}.join("\n").html_safe
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def choice_html(choice)
|
142
|
+
template.content_tag(:label,
|
143
|
+
builder.radio_button(input_name, choice_value(choice), input_html_options.merge(choice_html_options(choice))) <<
|
144
|
+
choice_label(choice),
|
145
|
+
label_html_options.merge(:for => choice_input_dom_id(choice))
|
146
|
+
)
|
147
|
+
end
|
148
|
+
|
149
|
+
# Override to remove the for attribute since this isn't associated with any element, as it's
|
150
|
+
# nested inside the legend.
|
151
|
+
def label_html_options
|
152
|
+
super.merge(:for => nil)
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|