Module: Formtastic::Inputs::Base::Timeish

Included in:
DateInput, DatetimeInput, TimeInput
Defined in:
lib/formtastic/inputs/base/timeish.rb

Overview

TODO:

Document i18n

TODO:

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

TODO:

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, :datetime, :time) 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 with display:none

Examples:

:date input with full form context and sample HTMl output


<%= semantic_form_for(@post) do |f| %>
  <%= f.inputs do %>
    ...
    <%= f.input :publish_at, :as => :date %>
  <% end %>
<% end %>

<form...>
  <fieldset class="inputs">
    <ol>
      <li class="date">
        <fieldset class="fragments">
          <ol class="fragments-group">
            <li class="fragment">
              <label for="post_publish_at_1i">Year</label>
              <select id="post_publish_at_1i" name="post[publish_at_1i]">...</select>
            </li>
            <li class="fragment">
              <label for="post_publish_at_2i">Month</label>
              <select id="post_publish_at_2i" name="post[publish_at_2i]">...</select>
            </li>
            <li class="fragment">
              <label for="post_publish_at_3i">Day</label>
              <select id="post_publish_at_3i" name="post[publish_at_3i]">...</select>
            </li>
          </ol>
        </fieldset>
      </li>
    </ol>
  </fieldset>
</form>

:time input

<%= f.input :publish_at, :as => :time %>

:datetime input

<%= f.input :publish_at, :as => :datetime %>

Change the labels for each fragment

<%= f.input :publish_at, :as => :date, :labels => { :year => "Y", :month => "M", :day => "D" }  %>

Skip a fragment (defaults to 1, skips all following fragments)

<%= f.input :publish_at, :as => :datetime, :discard_minute => true  %>
<%= f.input :publish_at, :as => :datetime, :discard_hour => true  %>
<%= f.input :publish_at, :as => :datetime, :discard_day => true  %>
<%= f.input :publish_at, :as => :datetime, :discard_month => true  %>
<%= f.input :publish_at, :as => :datetime, :discard_year => true  %>

Change the order

<%= f.input :publish_at, :as => :date, :order => [:month, :day, :year]  %>

Include seconds with times (excluded by default)

<%= f.input :publish_at, :as => :time, :include_seconds => true %>

Specify if there should be a blank option at the start of each select or not

<%= f.input :publish_at, :as => :time, :include_blank=> true %>
<%= f.input :publish_at, :as => :time, :include_blank=> false %>

Instance Method Summary collapse

Instance Method Details

#date_fragmentsObject



116
117
118
# File 'lib/formtastic/inputs/base/timeish.rb', line 116

def date_fragments
  options[:order] || i18n_date_fragments || default_date_fragments
end

#default_date_fragmentsObject



120
121
122
# File 'lib/formtastic/inputs/base/timeish.rb', line 120

def default_date_fragments
  [:year, :month, :day]
end

#fragment_id(fragment) ⇒ Object



141
142
143
# File 'lib/formtastic/inputs/base/timeish.rb', line 141

def fragment_id(fragment)
  "#{input_html_options[:id]}_#{position(fragment)}i"
end

#fragment_input_html(fragment) ⇒ Object



158
159
160
161
# File 'lib/formtastic/inputs/base/timeish.rb', line 158

def fragment_input_html(fragment)
  opts = input_options.merge(:prefix => object_name, :field_name => fragment_name(fragment), :default => value, :include_blank => include_blank?)
  template.send(:"select_#{fragment}", value, opts, input_html_options.merge(:id => fragment_id(fragment)))
end

#fragment_label(fragment) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/formtastic/inputs/base/timeish.rb', line 132

def fragment_label(fragment)
  labels_from_options = options[:labels] || {}
  if labels_from_options.key?(fragment)
    labels_from_options[fragment]
  else
    ::I18n.t(fragment.to_s, :default => fragment.to_s.humanize, :scope => [:datetime, :prompts])
  end
end

#fragment_label_html(fragment) ⇒ Object



149
150
151
152
# File 'lib/formtastic/inputs/base/timeish.rb', line 149

def fragment_label_html(fragment)
  text = fragment_label(fragment)
  text.blank? ? "".html_safe : template.(:label, text, :for => fragment_id(fragment))
end

#fragment_name(fragment) ⇒ Object



145
146
147
# File 'lib/formtastic/inputs/base/timeish.rb', line 145

def fragment_name(fragment)
  "#{method}(#{position(fragment)}i)"
end

#fragment_wrapping(&block) ⇒ Object



124
125
126
# File 'lib/formtastic/inputs/base/timeish.rb', line 124

def fragment_wrapping(&block)
  template.(:li, template.capture(&block), fragment_wrapping_html_options)
end

#fragment_wrapping_html_optionsObject



128
129
130
# File 'lib/formtastic/inputs/base/timeish.rb', line 128

def fragment_wrapping_html_options
  { :class => 'fragment' }
end

#fragmentsObject



108
109
110
# File 'lib/formtastic/inputs/base/timeish.rb', line 108

def fragments
  date_fragments + time_fragments
end

#fragments_inner_wrapping(&block) ⇒ Object



204
205
206
207
208
# File 'lib/formtastic/inputs/base/timeish.rb', line 204

def fragments_inner_wrapping(&block)
  template.(:ol,
    template.capture(&block)
  )
end

#fragments_labelObject



193
194
195
196
197
198
199
200
201
202
# File 'lib/formtastic/inputs/base/timeish.rb', line 193

def fragments_label
  if render_label?
    template.(:legend, 
      builder.label(method, label_text, :for => "#{input_html_options[:id]}_1i"), 
      :class => "label"
    )
  else
    "".html_safe
  end
end

#fragments_wrapping(&block) ⇒ Object



182
183
184
185
186
187
# File 'lib/formtastic/inputs/base/timeish.rb', line 182

def fragments_wrapping(&block)
  template.(:fieldset,
    template.capture(&block).html_safe, 
    fragments_wrapping_html_options
  )
end

#fragments_wrapping_html_optionsObject



189
190
191
# File 'lib/formtastic/inputs/base/timeish.rb', line 189

def fragments_wrapping_html_options
  { :class => "fragments" }
end

#hidden_fragmentsObject



210
211
212
# File 'lib/formtastic/inputs/base/timeish.rb', line 210

def hidden_fragments
  "".html_safe
end

#i18n_date_fragmentsObject



176
177
178
179
180
# File 'lib/formtastic/inputs/base/timeish.rb', line 176

def i18n_date_fragments
  order = ::I18n.t(:order, :scope => [:date])
  order = nil unless order.is_a?(Array)
  order
end

#include_blank?Boolean

TODO extract to BlankOptions or similar -- Select uses similar code

Returns:

  • (Boolean)


164
165
166
# File 'lib/formtastic/inputs/base/timeish.rb', line 164

def include_blank?
  options.key?(:include_blank) ? options[:include_blank] : builder.include_blank_for_select_by_default
end

#position(fragment) ⇒ Object



172
173
174
# File 'lib/formtastic/inputs/base/timeish.rb', line 172

def position(fragment)
  positions[fragment]
end

#positionsObject



168
169
170
# File 'lib/formtastic/inputs/base/timeish.rb', line 168

def positions
  { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
end

#time_fragmentsObject



112
113
114
# File 'lib/formtastic/inputs/base/timeish.rb', line 112

def time_fragments
  options[:include_seconds] ? [:hour, :minute, :second] : [:hour, :minute]
end

#to_htmlObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/formtastic/inputs/base/timeish.rb', line 90

def to_html
  input_wrapping do
    fragments_wrapping do
      hidden_fragments <<
      fragments_label <<
      template.(: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

#valueObject



154
155
156
# File 'lib/formtastic/inputs/base/timeish.rb', line 154

def value
  object.send(method) if object && object.respond_to?(method)
end