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,119 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a HTML5 `<input type="range">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is an alternative input choice to a number input.
|
6
|
+
#
|
7
|
+
# Sensible default for the `min`, `max` and `step` attributes are found by reflecting on
|
8
|
+
# the model's validations. When validations are not provided, the `min` and `step` default to
|
9
|
+
# `1` and the `max` default to `100`. An `IndeterminableMinimumAttributeError` exception
|
10
|
+
# will be raised when the following conditions are all true:
|
11
|
+
#
|
12
|
+
# * you haven't specified a `:min` or `:max` for the input
|
13
|
+
# * the model's database column type is a `:float` or `:decimal`
|
14
|
+
# * the validation uses `:less_than` or `:greater_than`
|
15
|
+
#
|
16
|
+
# The solution is to either:
|
17
|
+
#
|
18
|
+
# * manually specify the `:min` or `:max` for the input
|
19
|
+
# * change the database column type to an `:integer` (if appropriate)
|
20
|
+
# * change the validations to use `:less_than_or_equal_to` or `:greater_than_or_equal_to`
|
21
|
+
#
|
22
|
+
# @example Full form context and output
|
23
|
+
#
|
24
|
+
# <%= semantic_form_for(@user) do |f| %>
|
25
|
+
# <%= f.inputs do %>
|
26
|
+
# <%= f.input :shoe_size, :as => :range %>
|
27
|
+
# <% end %>
|
28
|
+
# <% end %>
|
29
|
+
#
|
30
|
+
# <form...>
|
31
|
+
# <fieldset>
|
32
|
+
# <ol>
|
33
|
+
# <li class="numeric">
|
34
|
+
# <label for="user_shoe_size">Shoe size</label>
|
35
|
+
# <input type="range" id="user_shoe_size" name="user[shoe_size]" min="1" max="100" step="1">
|
36
|
+
# </li>
|
37
|
+
# </ol>
|
38
|
+
# </fieldset>
|
39
|
+
# </form>
|
40
|
+
#
|
41
|
+
# @example Default HTML5 min/max/step attributes are detected from the numericality validations
|
42
|
+
#
|
43
|
+
# class Person < ActiveRecord::Base
|
44
|
+
# validates_numericality_of :age,
|
45
|
+
# :less_than_or_equal_to => 100,
|
46
|
+
# :greater_than_or_equal_to => 18,
|
47
|
+
# :only_integer => true
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# <%= f.input :age, :as => :number %>
|
51
|
+
#
|
52
|
+
# <li class="numeric">
|
53
|
+
# <label for="persom_age">Age</label>
|
54
|
+
# <input type="range" id="person_age" name="person[age]" min="18" max="100" step="1">
|
55
|
+
# </li>
|
56
|
+
#
|
57
|
+
# @example Pass attributes down to the `<input>` tag with :input_html
|
58
|
+
# <%= f.input :shoe_size, :as => :range, :input_html => { :min => 3, :max => 15, :step => 1, :class => "special" } %>
|
59
|
+
#
|
60
|
+
# @example Min/max/step also work as options
|
61
|
+
# <%= f.input :shoe_size, :as => :range, :min => 3, :max => 15, :step => 1, :input_html => { :class => "special" } %>
|
62
|
+
#
|
63
|
+
# @example Use :in with a Range as a shortcut for :min/:max
|
64
|
+
# <%= f.input :shoe_size, :as => :range, :in => 3..15, :step => 1 %>
|
65
|
+
# <%= f.input :shoe_size, :as => :range, :input_html => { :in => 3..15, :step => 1 } %>
|
66
|
+
#
|
67
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
68
|
+
# @see http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of Rails' Numericality validation documentation
|
69
|
+
#
|
70
|
+
# @todo Is it still correct for this to be Stringish?
|
71
|
+
class RangeInput
|
72
|
+
include Base
|
73
|
+
include Base::Stringish
|
74
|
+
|
75
|
+
def to_html
|
76
|
+
input_wrapping do
|
77
|
+
label_html <<
|
78
|
+
builder.range_field(method, input_html_options)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def input_html_options
|
83
|
+
defaults = super
|
84
|
+
|
85
|
+
if in_option
|
86
|
+
defaults[:min] = in_option.to_a.min
|
87
|
+
defaults[:max] = in_option.to_a.max
|
88
|
+
else
|
89
|
+
defaults[:min] ||= min_option
|
90
|
+
defaults[:max] ||= max_option
|
91
|
+
end
|
92
|
+
defaults[:step] ||= step_option
|
93
|
+
defaults
|
94
|
+
end
|
95
|
+
|
96
|
+
def step_option
|
97
|
+
return options[:step] if options.key?(:step)
|
98
|
+
return validation_step if validation_step
|
99
|
+
return 1 if validation_integer_only?
|
100
|
+
1
|
101
|
+
end
|
102
|
+
|
103
|
+
def min_option
|
104
|
+
return options[:min] if options.key?(:min)
|
105
|
+
validation_min || 1
|
106
|
+
end
|
107
|
+
|
108
|
+
def max_option
|
109
|
+
return options[:max] if options.key?(:max)
|
110
|
+
validation_max || 100
|
111
|
+
end
|
112
|
+
|
113
|
+
def in_option
|
114
|
+
options[:in]
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a HTML5 `<input type="search">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for attributes with a name matching
|
6
|
+
# `/^search$/`, but can be applied to any text-like input with `:as => :search`.
|
7
|
+
#
|
8
|
+
# @example Full form context and output
|
9
|
+
#
|
10
|
+
# <%= semantic_form_for(@search, :html => { :method => :get }) do |f| %>
|
11
|
+
# <%= f.inputs do %>
|
12
|
+
# <%= f.input :q, :as => :search, :label => false, :input_html => { :name => "q" } %>
|
13
|
+
# <% end %>
|
14
|
+
# <% end %>
|
15
|
+
#
|
16
|
+
# <form...>
|
17
|
+
# <fieldset>
|
18
|
+
# <ol>
|
19
|
+
# <li class="search">
|
20
|
+
# <input type="search" id="search_q" name="q">
|
21
|
+
# </li>
|
22
|
+
# </ol>
|
23
|
+
# </fieldset>
|
24
|
+
# </form>
|
25
|
+
#
|
26
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
27
|
+
class SearchInput
|
28
|
+
include Base
|
29
|
+
include Base::Stringish
|
30
|
+
|
31
|
+
def to_html
|
32
|
+
input_wrapping do
|
33
|
+
label_html <<
|
34
|
+
builder.search_field(method, input_html_options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,210 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
# A select input is used to render a `<select>` tag with a series of options to choose from.
|
4
|
+
# It works for both single selections (like a `belongs_to` relationship, or "yes/no" boolean),
|
5
|
+
# as well as multiple selections (like a `has_and_belongs_to_many`/`has_many` relationship,
|
6
|
+
# for assigning many genres to a song, for example).
|
7
|
+
#
|
8
|
+
# This is the default input choice when:
|
9
|
+
#
|
10
|
+
# * the database column type is an `:integer` and there is an association (`belongs_to`)
|
11
|
+
# * the database column type is a `:string` and the `:collection` option is used
|
12
|
+
# * there an object with an association, but no database column on the object (`has_many`, etc)
|
13
|
+
# * there is no object and the `:collection` option is used
|
14
|
+
#
|
15
|
+
# The flexibility of the `:collection` option (see examples) makes the :select input viable as
|
16
|
+
# an alternative for many other input types. For example, instead of...
|
17
|
+
#
|
18
|
+
# * a `:string` input (where you want to force the user to choose from a few specific strings rather than entering anything)
|
19
|
+
# * a `:boolean` checkbox input (where the user could choose yes or no, rather than checking a box)
|
20
|
+
# * a `:date`, `:time` or `:datetime` input (where the user could choose from pre-selected dates)
|
21
|
+
# * a `:number` input (where the user could choose from a set of pre-defined numbers)
|
22
|
+
# * a `:time_zone` input (where you want to provide your own set of choices instead of relying on Rails)
|
23
|
+
# * a `:country` input (no need for a plugin really)
|
24
|
+
#
|
25
|
+
# Within the standard `<li>` wrapper, the output is a `<label>` tag followed by a `<select>`
|
26
|
+
# tag containing `<option>` tags.
|
27
|
+
#
|
28
|
+
# For inputs that map to associations on the object model, Formtastic will automatically load
|
29
|
+
# 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
|
+
# `Tag.all` for a `Post` form with an input for a `has_and_belongs_to_many :tags` association.
|
32
|
+
# You can override or customise this collection and the `<option>` tags it will render through
|
33
|
+
# the `:collection` option (see examples).
|
34
|
+
#
|
35
|
+
# The way on which Formtastic renders the `value` attribute and content of each `<option>` tag
|
36
|
+
# is customisable through the `:member_label` and `:member_value` options. When not provided,
|
37
|
+
# we fall back to a list of methods to try on each object such as `:to_label`, `:name` and
|
38
|
+
# `:to_s`, which are defined in the configurations `collection_label_methods` and
|
39
|
+
# `collection_value_methods` (see examples below).
|
40
|
+
#
|
41
|
+
# @example Basic `belongs_to` example with full form context
|
42
|
+
#
|
43
|
+
# <%= semantic_form_for @post do |f| %>
|
44
|
+
# <%= f.inputs do %>
|
45
|
+
# <%= f.input :author, :as => :select %>
|
46
|
+
# <% end %>
|
47
|
+
# <% end %>
|
48
|
+
#
|
49
|
+
# <form...>
|
50
|
+
# <fieldset>
|
51
|
+
# <ol>
|
52
|
+
# <li class='select'>
|
53
|
+
# <label for="post_author_id">Author</label>
|
54
|
+
# <select id="post_author_id" name="post[post_author_id]">
|
55
|
+
# <option value=""></option>
|
56
|
+
# <option value="1">Justin</option>
|
57
|
+
# <option value="3">Kate</option>
|
58
|
+
# <option value="2">Amelia</option>
|
59
|
+
# </select>
|
60
|
+
# </li>
|
61
|
+
# </ol>
|
62
|
+
# </fieldset>
|
63
|
+
# </form>
|
64
|
+
#
|
65
|
+
# @example Basic `has_many` or `has_and_belongs_to_many` example with full form context
|
66
|
+
#
|
67
|
+
# <%= semantic_form_for @post do |f| %>
|
68
|
+
# <%= f.inputs do %>
|
69
|
+
# <%= f.input :tags, :as => :select %>
|
70
|
+
# <% end %>
|
71
|
+
# <% end %>
|
72
|
+
#
|
73
|
+
# <form...>
|
74
|
+
# <fieldset>
|
75
|
+
# <ol>
|
76
|
+
# <li class='select'>
|
77
|
+
# <label for="post_tag_ids">Author</label>
|
78
|
+
# <select id="post_tag_ids" name="post[tag_ids]" multiple="true">
|
79
|
+
# <option value="1">Ruby</option>
|
80
|
+
# <option value="6">Rails</option>
|
81
|
+
# <option value="3">Forms</option>
|
82
|
+
# <option value="4">Awesome</option>
|
83
|
+
# </select>
|
84
|
+
# </li>
|
85
|
+
# </ol>
|
86
|
+
# </fieldset>
|
87
|
+
# </form>
|
88
|
+
#
|
89
|
+
# @example Override Formtastic's assumption on when you need a multi select
|
90
|
+
# <%= f.input :authors, :as => :select, :input_html => { :multiple => true } %>
|
91
|
+
# <%= f.input :authors, :as => :select, :input_html => { :multiple => false } %>
|
92
|
+
#
|
93
|
+
# @example The `:collection` option can be used to customize the choices
|
94
|
+
# <%= f.input :author, :as => :select, :collection => @authors %>
|
95
|
+
# <%= f.input :author, :as => :select, :collection => Author.all %>
|
96
|
+
# <%= f.input :author, :as => :select, :collection => Author.some_named_scope %>
|
97
|
+
# <%= f.input :author, :as => :select, :collection => [Author.find_by_login("justin"), Category.find_by_name("kate")] %>
|
98
|
+
# <%= f.input :author, :as => :select, :collection => ["Justin", "Kate"] %>
|
99
|
+
# <%= f.input :author, :as => :select, :collection => [["Justin", "justin"], ["Kate", "kate"]] %>
|
100
|
+
# <%= f.input :author, :as => :select, :collection => [["Justin", "1"], ["Kate", "3"]] %>
|
101
|
+
# <%= f.input :author, :as => :select, :collection => [["Justin", 1], ["Kate", 3]] %>
|
102
|
+
# <%= f.input :author, :as => :select, :collection => 1..5 %>
|
103
|
+
# <%= f.input :author, :as => :select, :collection => "<option>your own options HTML string</option>" %>
|
104
|
+
# <%= f.input :author, :as => :select, :collection => options_for_select(...) %>
|
105
|
+
# <%= f.input :author, :as => :select, :collection => options_from_collection_for_select(...) %>
|
106
|
+
# <%= f.input :author, :as => :select, :collection => grouped_options_for_select(...) %>
|
107
|
+
# <%= f.input :author, :as => :select, :collection => time_zone_options_for_select(...) %>
|
108
|
+
#
|
109
|
+
# @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)
|
110
|
+
# <%= f.input :author, :as => :select, :member_label => :name %>
|
111
|
+
# <%= f.input :author, :as => :select, :member_label => :name_with_post_count
|
112
|
+
# <%= f.input :author, :as => :select, :member_label => Proc.new { |a| "#{c.name} (#{pluralize("post", a.posts.count)})" }
|
113
|
+
#
|
114
|
+
# @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)
|
115
|
+
# <%= f.input :author, :as => :select, :member_value => :login %>
|
116
|
+
# <%= f.input :author, :as => :select, :member_value => Proc.new { |c| c.full_name.downcase.underscore }
|
117
|
+
#
|
118
|
+
# @example Set HTML attributes on the `<select>` tag with `:input_html`
|
119
|
+
# <%= f.input :authors, :as => :select, :input_html => { :size => 20, :multiple => true, :class => "special" } %>
|
120
|
+
#
|
121
|
+
# @example Set HTML attributes on the `<li>` wrapper with `:wrapper_html`
|
122
|
+
# <%= f.input :authors, :as => :select, :wrapper_html => { :class => "special" } %>
|
123
|
+
#
|
124
|
+
# @example Exclude or include the blank option at the top of the select, or change the prompt
|
125
|
+
# <%= f.input :author, :as => :select, :input_html => { :include_blank => false } %>
|
126
|
+
# <%= f.input :author, :as => :select, :input_html => { :include_blank => true } %>
|
127
|
+
# <%= f.input :author, :as => :select, :input_html => { :prompt => "Please select an Author..." } %>
|
128
|
+
#
|
129
|
+
# @example Group options an `<optgroup>` with the `:group_by` and `:group_label` options (`belongs_to` associations only)
|
130
|
+
# <%= f.input :author, :as => :select, :group_by => :continent %>
|
131
|
+
#
|
132
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
133
|
+
# @see Formtastic::Inputs::CheckBoxesInput CheckBoxesInput as an alternative for `has_many` and `has_and_belongs_to_many` associations
|
134
|
+
# @see Formtastic::Inputs::RadioInput RadioInput as an alternative for `belongs_to` associations
|
135
|
+
#
|
136
|
+
# @todo Do/can we support the per-item HTML options like RadioInput?
|
137
|
+
class SelectInput
|
138
|
+
include Base
|
139
|
+
include Base::Collections
|
140
|
+
include Base::GroupedCollections
|
141
|
+
|
142
|
+
def to_html
|
143
|
+
input_wrapping do
|
144
|
+
label_html <<
|
145
|
+
(options[:group_by] ? grouped_select_html : select_html)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def select_html
|
150
|
+
builder.select(input_name, collection, input_options, input_html_options)
|
151
|
+
end
|
152
|
+
|
153
|
+
def grouped_select_html
|
154
|
+
builder.grouped_collection_select(
|
155
|
+
input_name,
|
156
|
+
grouped_collection,
|
157
|
+
group_association,
|
158
|
+
group_label_method,
|
159
|
+
value_method,
|
160
|
+
label_method,
|
161
|
+
input_options,
|
162
|
+
input_html_options
|
163
|
+
)
|
164
|
+
end
|
165
|
+
|
166
|
+
def include_blank?
|
167
|
+
return options[:prompt] if options.key?(:prompt)
|
168
|
+
return options[:include_blank] if options.key?(:include_blank)
|
169
|
+
return true if (single? && builder.include_blank_for_select_by_default)
|
170
|
+
false
|
171
|
+
end
|
172
|
+
|
173
|
+
def label_html_options
|
174
|
+
super.merge(:for => input_html_options[:id])
|
175
|
+
end
|
176
|
+
|
177
|
+
def input_options
|
178
|
+
{:include_blank => include_blank?}.merge(super)
|
179
|
+
end
|
180
|
+
|
181
|
+
def input_html_options
|
182
|
+
extra_input_html_options.merge(super)
|
183
|
+
end
|
184
|
+
|
185
|
+
def extra_input_html_options
|
186
|
+
{
|
187
|
+
:multiple => multiple_by_association?,
|
188
|
+
:name => "#{object_name}[#{association_primary_key}]#{'[]' if multiple?}"
|
189
|
+
}
|
190
|
+
end
|
191
|
+
|
192
|
+
def multiple_by_association?
|
193
|
+
reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
|
194
|
+
end
|
195
|
+
|
196
|
+
def multiple_by_options?
|
197
|
+
options[:multiple]
|
198
|
+
end
|
199
|
+
|
200
|
+
def multiple?
|
201
|
+
multiple_by_options? || multiple_by_association?
|
202
|
+
end
|
203
|
+
|
204
|
+
def single?
|
205
|
+
!multiple?
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a `<input type="text">` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for database columns of the `:string` type,
|
6
|
+
# and is the default choice for all inputs when no other logical input type can be inferred.
|
7
|
+
# You can force any input to be a string input with `:as => :string`.
|
8
|
+
#
|
9
|
+
# @example Full form context and output
|
10
|
+
#
|
11
|
+
# <%= semantic_form_for(@user) do |f| %>
|
12
|
+
# <%= f.inputs do %>
|
13
|
+
# <%= f.input :first_name, :as => :string %>
|
14
|
+
# <% end %>
|
15
|
+
# <% end %>
|
16
|
+
#
|
17
|
+
# <form...>
|
18
|
+
# <fieldset>
|
19
|
+
# <ol>
|
20
|
+
# <li class="string">
|
21
|
+
# <label for="user_first_name">First name</label>
|
22
|
+
# <input type="text" id="user_first_name" name="user[first_name]">
|
23
|
+
# </li>
|
24
|
+
# </ol>
|
25
|
+
# </fieldset>
|
26
|
+
# </form>
|
27
|
+
#
|
28
|
+
# @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documetation of all possible options.
|
29
|
+
class StringInput
|
30
|
+
include Base
|
31
|
+
include Base::Stringish
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Formtastic
|
2
|
+
module Inputs
|
3
|
+
|
4
|
+
# Outputs a simple `<label>` with a `<textarea>` wrapped in the standard
|
5
|
+
# `<li>` wrapper. This is the default input choice for database columns of the `:text` type,
|
6
|
+
# but can forced on any text-like input with `:as => :text`.
|
7
|
+
#
|
8
|
+
# @example Full form context and output
|
9
|
+
#
|
10
|
+
# <%= semantic_form_for(@user) do |f| %>
|
11
|
+
# <%= f.inputs do %>
|
12
|
+
# <%= f.input :first_name, :as => :string %>
|
13
|
+
# <% end %>
|
14
|
+
# <% end %>
|
15
|
+
#
|
16
|
+
# <form...>
|
17
|
+
# <fieldset>
|
18
|
+
# <ol>
|
19
|
+
# <li class="string">
|
20
|
+
# <label for="user_first_name">First name</label>
|
21
|
+
# <input type="text" id="user_first_name" name="user[first_name]">
|
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 TextInput
|
29
|
+
include Base
|
30
|
+
|
31
|
+
def input_html_options
|
32
|
+
{
|
33
|
+
:cols => builder.default_text_area_width,
|
34
|
+
:rows => builder.default_text_area_height
|
35
|
+
}.merge(super)
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_html
|
39
|
+
input_wrapping do
|
40
|
+
label_html <<
|
41
|
+
builder.text_area(method, input_html_options)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|