Module: Formtastic::Inputs::Base::Timeish
- Included in:
- DateSelectInput, DatetimeSelectInput, TimeSelectInput
- Defined in:
- lib/formtastic/inputs/base/timeish.rb
Overview
Document i18n
Check what other Rails options are supported (start_year
, end_year
, use_month_numbers
, use_short_month
, add_month_numbers
, prompt
), write tests for them, and otherwise support them
Could we take the rendering from Rails' helpers and inject better HTML in and around it rather than re-inventing the whee?
Timeish inputs (:date_select
, :datetime_select
, :time_select
) are similar to the Rails date and time
helpers (date_select
, datetime_select
, time_select
), rendering a series of <select>
tags for each fragment (year, month, day, hour, minute, seconds). The fragments are then
re-combined to a date by ActiveRecord through multi-parameter assignment.
The mark-up produced by Rails is simple but far from ideal, with no way to label the
individual fragments for accessibility, no fieldset to group the related fields, and no
legend describing the group. Formtastic addresses this within the standard <li>
wrapper
with a <fieldset>
with a <legend>
as a label, followed by an ordered list (<ol>
) of
list items (<li>
), one for each fragment (year, month, ...). Each <li>
fragment contains
a <label>
(eg "Year") for the fragment, and a <select>
containing <option>
s (eg a
range of years).
In the supplied formtastic.css file, the resulting mark-up is styled to appear a lot like a standard Rails date time select by:
- styling the legend to look like the other labels (to the left hand side of the selects)
- floating the
<li>
fragments against each other as a single line - hiding the
<label>
of each fragment withdisplay:none
Instance Method Summary collapse
- #date_fragments ⇒ Object
- #default_date_fragments ⇒ Object
- #fragment_id(fragment) ⇒ Object
- #fragment_input_html(fragment) ⇒ Object
- #fragment_label(fragment) ⇒ Object
- #fragment_label_html(fragment) ⇒ Object
- #fragment_name(fragment) ⇒ Object
- #fragment_prefix ⇒ Object
- #fragment_wrapping(&block) ⇒ Object
- #fragment_wrapping_html_options ⇒ Object
- #fragments ⇒ Object
- #fragments_inner_wrapping(&block) ⇒ Object
- #fragments_label ⇒ Object
- #fragments_wrapping(&block) ⇒ Object
- #fragments_wrapping_html_options ⇒ Object
- #hidden_field_name(fragment) ⇒ Object
- #hidden_fragments ⇒ Object
- #i18n_date_fragments ⇒ Object
-
#include_blank? ⇒ Boolean
TODO extract to BlankOptions or similar -- Select uses similar code.
- #position(fragment) ⇒ Object
- #positions ⇒ Object
- #time_fragments ⇒ Object
- #to_html ⇒ Object
- #value ⇒ Object
Instance Method Details
#date_fragments ⇒ Object
122 123 124 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 122 def date_fragments [:order] || i18n_date_fragments || default_date_fragments end |
#default_date_fragments ⇒ Object
126 127 128 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 126 def default_date_fragments [:year, :month, :day] end |
#fragment_id(fragment) ⇒ Object
149 150 151 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 149 def fragment_id(fragment) "#{[:id]}_#{position(fragment)}i" end |
#fragment_input_html(fragment) ⇒ Object
167 168 169 170 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 167 def fragment_input_html(fragment) opts = .merge(:prefix => fragment_prefix, :field_name => fragment_name(fragment), :default => value, :include_blank => include_blank?) template.send(:"select_#{fragment}", value, opts, .merge(:id => fragment_id(fragment))) end |
#fragment_label(fragment) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 138 def fragment_label(fragment) = .key?(:labels) ? [:labels] : {} if ! '' elsif .key?(fragment) [fragment] else ::I18n.t(fragment.to_s, :default => fragment.to_s.humanize, :scope => [:datetime, :prompts]) end end |
#fragment_label_html(fragment) ⇒ Object
157 158 159 160 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 157 def fragment_label_html(fragment) text = fragment_label(fragment) text.blank? ? "".html_safe : template.content_tag(:label, text, :for => fragment_id(fragment)) end |
#fragment_name(fragment) ⇒ Object
153 154 155 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 153 def fragment_name(fragment) "#{method}(#{position(fragment)}i)" end |
#fragment_prefix ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 172 def fragment_prefix if builder..key?(:index) object_name + "[#{builder.[:index]}]" else object_name end end |
#fragment_wrapping(&block) ⇒ Object
130 131 132 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 130 def fragment_wrapping(&block) template.content_tag(:li, template.capture(&block), ) end |
#fragment_wrapping_html_options ⇒ Object
134 135 136 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 134 def { :class => 'fragment' } end |
#fragments ⇒ Object
114 115 116 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 114 def fragments date_fragments + time_fragments end |
#fragments_inner_wrapping(&block) ⇒ Object
224 225 226 227 228 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 224 def fragments_inner_wrapping(&block) template.content_tag(:ol, template.capture(&block) ) end |
#fragments_label ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 213 def fragments_label if render_label? template.content_tag(:legend, builder.label(method, label_text, :for => fragment_id(fragments.first)), :class => "label" ) else "".html_safe end end |
#fragments_wrapping(&block) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 202 def fragments_wrapping(&block) template.content_tag(:fieldset, template.capture(&block).html_safe, ) end |
#fragments_wrapping_html_options ⇒ Object
209 210 211 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 209 def { :class => "fragments" } end |
#hidden_field_name(fragment) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 234 def hidden_field_name(fragment) if builder..key?(:index) "#{object_name}[#{builder.[:index]}][#{fragment_name(fragment)}]" else "#{object_name}[#{fragment_name(fragment)}]" end end |
#hidden_fragments ⇒ Object
230 231 232 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 230 def hidden_fragments "".html_safe end |
#i18n_date_fragments ⇒ Object
193 194 195 196 197 198 199 200 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 193 def i18n_date_fragments order = ::I18n.t(:order, :scope => [:date]) if order.is_a?(Array) order.map &:to_sym else nil end end |
#include_blank? ⇒ Boolean
TODO extract to BlankOptions or similar -- Select uses similar code
181 182 183 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 181 def include_blank? .key?(:include_blank) ? [:include_blank] : builder.include_blank_for_select_by_default end |
#position(fragment) ⇒ Object
189 190 191 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 189 def position(fragment) positions[fragment] end |
#positions ⇒ Object
185 186 187 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 185 def positions { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 } end |
#time_fragments ⇒ Object
118 119 120 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 118 def time_fragments [:include_seconds] ? [:hour, :minute, :second] : [:hour, :minute] end |
#to_html ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 96 def to_html input_wrapping do fragments_wrapping do hidden_fragments << fragments_label << template.content_tag(:ol, fragments.map do |fragment| fragment_wrapping do fragment_label_html(fragment) << fragment_input_html(fragment) end end.join.html_safe, # TODO is this safe? { :class => 'fragments-group' } # TODO refactor to fragments_group_wrapping ) end end end |
#value ⇒ Object
162 163 164 165 |
# File 'lib/formtastic/inputs/base/timeish.rb', line 162 def value return [:selected] if .key?(:selected) object.send(method) if object && object.respond_to?(method) end |