Module: CalendarDateSelect::FormHelper

Defined in:
lib/calendar_date_select.rb

Instance Method Summary collapse

Instance Method Details

#calendar_date_select(object, method, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/calendar_date_select.rb', line 151

def calendar_date_select(object, method, options={})
  obj = options[:object] || instance_variable_get("@#{object}")
  
  if !options.include?(:time) && obj.class.respond_to?("columns_hash")
    column_type = (obj.class.columns_hash[method.to_s].type rescue nil)
    options[:time] = true if column_type == :datetime
  end
  
  use_time = options[:time]
  
  if options[:time].to_s=="mixed"
    use_time = false if Date===(obj.respond_to?(method) && obj.send(method))
  end
  
  calendar_options = calendar_date_select_process_options(options)
  
  options[:value] ||= 
    if(obj.respond_to?(method) && obj.send(method).respond_to?(:strftime))
      obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
    elsif obj.respond_to?("#{method}_before_type_cast") 
      obj.send("#{method}_before_type_cast")
    elsif obj.respond_to?(method)
      obj.send(method).to_s
    else
      nil
    end

  tag = ActionView::Helpers::InstanceTag.new(object, method, self, nil, options.delete(:object))
  calendar_date_select_output(
    tag.to_input_field_tag( (calendar_options[:hidden] || calendar_options[:embedded]) ? "hidden" : "text", options), 
    calendar_options
  )
end

#calendar_date_select_output(input, calendar_options = {}) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/calendar_date_select.rb', line 185

def calendar_date_select_output(input, calendar_options = {})
  out = input
  if calendar_options[:embedded]
    uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
    # we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
    out << (:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
    
    out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(calendar_options)} ); ")
  else
    out << " "
    
    out << image_tag(CalendarDateSelect.image, 
        :onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(calendar_options)} );",
        :style => 'border:0px; cursor:pointer;')
  end
  
  out
end

#calendar_date_select_process_options(options) ⇒ Object

extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/calendar_date_select.rb', line 113

def calendar_date_select_process_options(options)
  calendar_options = {}
  callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
  for key in [:time, :valid_date_check, :embedded, :buttons, :format, :year_range, :month_year, :popup, :hidden, :minute_interval] + callbacks
    calendar_options[key] = options.delete(key) if options.has_key?(key)
  end
  
  # if passing in mixed, pad it with single quotes
  calendar_options[:time] = "'mixed'" if calendar_options[:time].to_s=="mixed"
  calendar_options[:month_year] = "'#{calendar_options[:month_year]}'" if calendar_options[:month_year]
  
  # if we are forcing the popup, automatically set the readonly property on the input control.
  if calendar_options[:popup].to_s == "force"
    calendar_options[:popup] = "'force'"
    options[:readonly] = true 
  end
  
  if (vdc=calendar_options.delete(:valid_date_check))
    if vdc.include?(";") || vdc.include?("function")
      throw ":valid_date_check function is missing a 'return' statement.  Try something like: :valid_date_check => 'if (date > new(Date)) return true; else return false;'" unless vdc.include?("return");
    end
    
    vdc = "return(#{vdc})" unless vdc.include?("return")
    vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
    calendar_options[:valid_date_check] = vdc
  end
  
  calendar_options[:popup_by] ||= "this" if calendar_options[:hidden]
  
  # surround any callbacks with a function, if not already done so
  for key in callbacks
    calendar_options[key] = "function(param) { #{calendar_options[key]} }" unless calendar_options[key].include?("function") if calendar_options[key]
  end
  
  calendar_options[:year_range] = format_year_range(calendar_options[:year_range] || 10)
  calendar_options
end

#calendar_date_select_tag(name, value = nil, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/calendar_date_select.rb', line 83

def calendar_date_select_tag( name, value = nil, options = {})
  calendar_options = calendar_date_select_process_options(options)
  value = format_time(value, calendar_options)
  
  calendar_options.delete(:format)
  
  options[:id] ||= name
  tag = calendar_options[:hidden] || calendar_options[:embedded] ? 
    hidden_field_tag(name, value, options) :
    text_field_tag(name, value, options)
  
  calendar_date_select_output(tag, calendar_options)
end

#format_time(value, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/calendar_date_select.rb', line 97

def format_time(value, options = {})
   if value.respond_to?("strftime")
     if options[:format]
       value = value.strftime(options[:format])
     else
       if options.has_key? :time
         value = value.strftime(CalendarDateSelect.date_format_string(options[:time]))
       else
         value = CalendarDateSelect.format_date(value)
       end
     end
   end
   value
end