Class: ActiveForm::Element::SelectTimebased

Inherits:
Section show all
Defined in:
lib/active_form/facets/select_timebased.rb

Direct Known Subclasses

SelectDate, SelectDatetime, SelectTime

Constant Summary collapse

TIME_FORMAT =
[:year, :month, :day, :hour, :minute, :second].freeze
DEFAULT_VALUES =
{ :year => 2000, :month => 1, :day => 1, :hour => 12, :minute => 0, :second => 0 }.freeze
VALUE_CALL_MAP =
{ :year => :year, :month => :month, :day => :day, :hour => :hour, :minute => :min, :second => :sec }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Section

#label_attributes, #render_elements, #render_frozen, #render_label

Methods included from Mixins::ContainerMethods

#bound_value?, #define_localizer, #description, #get_bound_value, included, #label, #localize, #localized?, #localizer, #set_bound_value, #submitted?, #title, #update_value, #validated?

Methods included from Mixins::ElementMethods

#accept_block, #contained?, #default_value, #define_formatting_filter, #define_freeze_filter, #description, #disabled?, #element_attributes, #element_binding, #element_binding_key, #element_name, #element_type, #element_value, #element_value=, #fallback_value, #fallback_value=, #format_value, #formatted_value, #freeze_element, #freeze_value, #frozen?, #frozen_value, #frozen_value=, #hidden?, #hide_element, #identifier, included, #initialize_element, #label, #labelled?, #localize, #localized?, #readonly?, #register_container, #render_frozen, #required=, #required?, #reset_formatting_filter, #reset_freeze_filter, #revert_value, #title, #update_options_and_attributes

Instance Attribute Details

#day_incrementObject

Returns the value of attribute day_increment.



25
26
27
# File 'lib/active_form/facets/select_timebased.rb', line 25

def day_increment
  @day_increment
end

#day_labelObject

Returns the value of attribute day_label.



24
25
26
# File 'lib/active_form/facets/select_timebased.rb', line 24

def day_label
  @day_label
end

#end_yearObject

Returns the value of attribute end_year.



22
23
24
# File 'lib/active_form/facets/select_timebased.rb', line 22

def end_year
  @end_year
end

#formatObject

Returns the value of attribute format.



21
22
23
# File 'lib/active_form/facets/select_timebased.rb', line 21

def format
  @format
end

#hour_incrementObject

Returns the value of attribute hour_increment.



28
29
30
# File 'lib/active_form/facets/select_timebased.rb', line 28

def hour_increment
  @hour_increment
end

#hour_labelObject

Returns the value of attribute hour_label.



27
28
29
# File 'lib/active_form/facets/select_timebased.rb', line 27

def hour_label
  @hour_label
end

#minute_incrementObject

Returns the value of attribute minute_increment.



28
29
30
# File 'lib/active_form/facets/select_timebased.rb', line 28

def minute_increment
  @minute_increment
end

#minute_labelObject

Returns the value of attribute minute_label.



27
28
29
# File 'lib/active_form/facets/select_timebased.rb', line 27

def minute_label
  @minute_label
end

#month_incrementObject

Returns the value of attribute month_increment.



25
26
27
# File 'lib/active_form/facets/select_timebased.rb', line 25

def month_increment
  @month_increment
end

#month_labelObject

Returns the value of attribute month_label.



24
25
26
# File 'lib/active_form/facets/select_timebased.rb', line 24

def month_label
  @month_label
end

#nowObject

Returns the value of attribute now.



20
21
22
# File 'lib/active_form/facets/select_timebased.rb', line 20

def now
  @now
end

#rangeObject

Returns the value of attribute range.



22
23
24
# File 'lib/active_form/facets/select_timebased.rb', line 22

def range
  @range
end

#second_incrementObject

Returns the value of attribute second_increment.



28
29
30
# File 'lib/active_form/facets/select_timebased.rb', line 28

def second_increment
  @second_increment
end

#second_labelObject

Returns the value of attribute second_label.



27
28
29
# File 'lib/active_form/facets/select_timebased.rb', line 27

def second_label
  @second_label
end

#start_yearObject

Returns the value of attribute start_year.



22
23
24
# File 'lib/active_form/facets/select_timebased.rb', line 22

def start_year
  @start_year
end

#year_incrementObject

Returns the value of attribute year_increment.



25
26
27
# File 'lib/active_form/facets/select_timebased.rb', line 25

def year_increment
  @year_increment
end

#year_labelObject

Returns the value of attribute year_label.



24
25
26
# File 'lib/active_form/facets/select_timebased.rb', line 24

def year_label
  @year_label
end

Class Method Details

.element_typeObject



30
31
32
# File 'lib/active_form/facets/select_timebased.rb', line 30

def self.element_type
  :select_timebased
end

.inherited(derivative) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/active_form/facets/select_timebased.rb', line 7

def self.inherited(derivative)
  super
  derivative.class_inheritable_accessor :default_format
  derivative.class_inheritable_accessor :allowed_parts
  derivative.default_format = TIME_FORMAT
  derivative.allowed_parts = TIME_FORMAT
end

Instance Method Details

#after_initializeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_form/facets/select_timebased.rb', line 100

def after_initialize
  elements_format.each do |part|
    if self.class.allowed_parts.include?(part)
      defaults = {}
      defaults[:label] = self.send("#{part}_label") rescue part.to_s
      defaults[:increment] = self.send("#{part}_increment") || 1 rescue 1 
      if part == :year
        defaults[:range] = self.range if self.range.kind_of?(Range) 
        defaults[:start_year] = self.start_year if self.start_year.kind_of?(Integer) 
        defaults[:end_year] = self.end_year if self.end_year.kind_of?(Integer)  
      end
      self.define_element("select_#{part}", "#{part}", defaults) do |e|
        e.define_element_wrapper do |builder, elem, render|
          builder.span(:id => "elem_#{elem.identifier}", :class => elem.css, :style => elem.style) { render.call(builder) }
        end
      end
    elsif !part.kind_of?(Symbol)
      self << part
    end
  end
  update_values(default_element_values)
end

#cast_value_to(value, type = :string) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_form/facets/select_timebased.rb', line 67

def cast_value_to(value, type = :string)
  if [:date, :time, :hash, :array, :string, :yaml].include?(type)
    time_hash = default_element_values.merge(time_to_values(self.now, true)).merge(value)
    (self.class.allowed_parts - elements_format).each { |part| time_hash[part] = 0 }
    return time_hash if type == :hash
    time_array = TIME_FORMAT.collect { |part| time_hash[part] || 0 }
    return time_array if type == :array
    time = Time.send(ActiveForm::Element::Base.default_timezone, *time_array) rescue nil
    return time.to_date if type == :date && time.respond_to?(:to_date) 
    return time.to_formatted_s(:db) if type == :string && time.respond_to?(:to_formatted_s)
    return time.to_yaml if type == :yaml
    time
  else
    value
  end
end

#casting_filter(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_form/facets/select_timebased.rb', line 88

def casting_filter(value)
  time = case value
    when Date then value.to_time
    when Time then value
    when String then self.class.string_to_time(value)
    when Hash then return ActiveForm::Values.new(value)
    else nil 
  end
  return default_element_values if time.nil?
  time_to_values(time)
end

#export_value(values = ActiveForm::Values.new) ⇒ Object



59
60
61
# File 'lib/active_form/facets/select_timebased.rb', line 59

def export_value(values = ActiveForm::Values.new)
  export_value_as(self.type_cast)
end

#export_value_as(type) ⇒ Object



63
64
65
# File 'lib/active_form/facets/select_timebased.rb', line 63

def export_value_as(type)
  cast_value_to(original_export_value, type)
end

#include_empty=(bool = true) ⇒ Object Also known as: include_empty



48
49
50
# File 'lib/active_form/facets/select_timebased.rb', line 48

def include_empty=(bool = true)
  self.update_elements(:include_empty => bool)
end

#original_export_valueObject



57
# File 'lib/active_form/facets/select_timebased.rb', line 57

alias :original_export_value :export_value

#render_element(builder = create_builder) ⇒ Object



44
45
46
# File 'lib/active_form/facets/select_timebased.rb', line 44

def render_element(builder = create_builder)
  builder.div(element_attributes) { render_elements(builder) }
end

#type_cast=(type = :string) ⇒ Object



84
85
86
# File 'lib/active_form/facets/select_timebased.rb', line 84

def type_cast=(type = :string) 
  @type_cast = type.to_sym
end

#update_from_params(params, force = false) ⇒ Object



53
54
55
# File 'lib/active_form/facets/select_timebased.rb', line 53

def update_from_params(params, force = false)
  super(casting_filter(params), force)
end