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

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

Instance Method Summary collapse

Instance Method Details

#date_fragmentsObject



32
33
34
# File 'lib/formtastic/inputs/base/timeish.rb', line 32

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

#default_date_fragmentsObject



36
37
38
# File 'lib/formtastic/inputs/base/timeish.rb', line 36

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

#fragment_id(fragment) ⇒ Object



53
54
55
# File 'lib/formtastic/inputs/base/timeish.rb', line 53

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

#fragment_input_html(fragment) ⇒ Object



70
71
72
73
# File 'lib/formtastic/inputs/base/timeish.rb', line 70

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



44
45
46
47
48
49
50
51
# File 'lib/formtastic/inputs/base/timeish.rb', line 44

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



61
62
63
64
# File 'lib/formtastic/inputs/base/timeish.rb', line 61

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

#fragment_name(fragment) ⇒ Object



57
58
59
# File 'lib/formtastic/inputs/base/timeish.rb', line 57

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

#fragment_wrapping(&block) ⇒ Object



40
41
42
# File 'lib/formtastic/inputs/base/timeish.rb', line 40

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

#fragmentsObject



24
25
26
# File 'lib/formtastic/inputs/base/timeish.rb', line 24

def fragments
  date_fragments + time_fragments
end

#fragments_inner_wrapping(&block) ⇒ Object



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

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

#fragments_labelObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/formtastic/inputs/base/timeish.rb', line 105

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

#fragments_wrapping(&block) ⇒ Object



94
95
96
97
98
99
# File 'lib/formtastic/inputs/base/timeish.rb', line 94

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

#fragments_wrapping_html_optionsObject



101
102
103
# File 'lib/formtastic/inputs/base/timeish.rb', line 101

def fragments_wrapping_html_options
  {}
end

#i18n_date_fragmentsObject



88
89
90
91
92
# File 'lib/formtastic/inputs/base/timeish.rb', line 88

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)


76
77
78
# File 'lib/formtastic/inputs/base/timeish.rb', line 76

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

#position(fragment) ⇒ Object



84
85
86
# File 'lib/formtastic/inputs/base/timeish.rb', line 84

def position(fragment)
  positions[fragment]
end

#positionsObject



80
81
82
# File 'lib/formtastic/inputs/base/timeish.rb', line 80

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

#time_fragmentsObject



28
29
30
# File 'lib/formtastic/inputs/base/timeish.rb', line 28

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

#to_htmlObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/formtastic/inputs/base/timeish.rb', line 6

def to_html
  input_wrapping do
    fragments_wrapping do
      fragments_label <<
      template.(:fieldset,
        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?
        )
      )
    end
  end
end

#valueObject



66
67
68
# File 'lib/formtastic/inputs/base/timeish.rb', line 66

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