Module: CalendarDateSelect::FormHelpers

Defined in:
lib/calendar_date_select/form_helpers.rb

Overview

Various helpers available for use in your view

Instance Method Summary collapse

Instance Method Details

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

Similar to the difference between text_field_tag and text_field, this method behaves like text_field

It receives the same options as calendar_date_select_tag. Need for time selection is automatically detected by checking the corresponding column meta information of Model#columns_hash



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/calendar_date_select/form_helpers.rb', line 137

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 if obj.class.columns_hash.include?(method.to_s)
    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

  image, options, javascript_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_with_backwards_compatibility(object, method, self, options.delete(:object))
  calendar_date_select_output(
    tag.to_input_field_tag( (javascript_options[:hidden] || javascript_options[:embedded]) ? "hidden" : "text", options),
    image,
    options,
    javascript_options
  )
end

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

Similar to text_field_tag, but adds a calendar picker, naturally.

Arguments

+name+ - the html name of the tag
+value+ - When specified as a string, uses value verbatim.  When Date, DateTime, Time, it converts it to a string basd off the format set by CalendarDateSelect#format=
+options+ - ...

Options

:embedded

Put the calendar straight into the form, rather than using a popup type of form.

<%= calendar_date_select_tag "name", "2007-01-01", :embedded => true %>

:hidden

Use a hidden element instead of a text box for a pop up calendar. Not compatible with :embedded => true. You’ll probably want to use an onchange callback to do something with the value.

<span id='cds_value' />
<%= calendar_date_select_tag "hidden_date_selector", "", :hidden => "true", :onchange => "$('cds_value').update($F(this));" %>

:image

Specify an alternative icon to use for the date picker.

To use /images/groovy.png:

<%= calendar_date_select_tag "altered_image", "", :image => "groovy.png" %>

:minute_interval

Specifies the minute interval used in the hour/minute selector. Default is 5.

<%= calendar_date_select_tag "month_year_selector_label", "", :minute_interval => 15 %>

:month_year

Customize the month and year selectors at the top of the control.

Valid values:

* "dropdowns" (default) - Use a separate dropdown control for both the month and year
* "label" - Use static text to show the month and the year.

  <%= calendar_date_select_tag "month_year_selector_label", "", :month_year => "label" %>

:popup => ‘force’

Forces the user to use the popup calendar by making it’s text-box read-only and causing calendar_date_select to override it’s default behavior of not allowing selection of a date on a target element that is read-only.

<%= calendar_date_select_tag "name", "2007-01-01", :popup => "force" %>

:time

Show time in the controls. There’s three options:

* +true+ - show an hour/minute selector.
* +false+ - don't show an hour/minute selector.
* +"mixed"+ - Show an hour/minute selector, but include a "all day" option - allowing them to choose whether or not to specify a time.

:year_range

Limit the year range. You can pass in an array or range of ruby Date/Time objects or FixNum’s.

<%= calendar_date_select_tag "e_date", nil, :year_range => 10.years.ago..0.years.from_now %>
<%= calendar_date_select_tag "e_date", nil, :year_range => [0.years.ago, 10.years.from_now] %>
<%= calendar_date_select_tag "e_date", nil, :year_range => 2000..2007 %>
<%= calendar_date_select_tag "e_date", nil, :year_range => [2000, 2007] %>

:valid_date_check

Javascript function used to “disable” certain dates on the calendar, so the user can’t pick them.

The date being evaulated is made available through a javascript variable named “date”.

This disables the dates after the begining of today:

<%= calendar_date_select_tag "birth_day", "", :valid_date_check => "date < (new Date()).stripTime()" %>

This disables weekends:

<%= calendar_date_select_tag "work_day", "", :valid_date_check => "date.getDay() != 0 && date.getDay() != 6" %>

:highlighted_date_check

Javascript function used to “highlight” certain dates on the calendar. This is only a visual cue for the user, and doesn’t prohibits the user from selecting other dates. Use :valid_date_check if you want to do that. :valid_date_check and :highlighted_date_check can be used together.

The date being evaulated is made available through a javascript variable named “date”.

This highlights halloween (31st of October):

<%= calendar_date_select_tag "near_halloween_day", "", :highlighted_date_check => "date.getDate() == 31 && date.getMonth() == 10" %>

This highlights fridays:

<%= calendar_date_select_tag "casual_day", "", :highlighted_date_check => "date.getDay() == 5" %>

CALLBACKS

The following callbacks are available:

* before_show / after_show
* before_close / after_close
* after_navigate - Called when navigating to a different month. Passes first parameter as a date object refering to the current month viewed
* onchange - Called when the form input value changes

 <%= calendar_date_select_tag "event_demo", "",
   :before_show => "log('Calendar Showing');" ,
   :after_show => "log('Calendar Shown');" ,
   :before_close => "log('Calendar closing');" ,
   :after_close => "log('Calendar closed');",
   :after_navigate => "log('Current month is ' + (param.getMonth()+1) + '/' + (param.getFullYear()));",
   :onchange => "log('value changed to - ' + $F(this));"

}}}

All callbacks are executed within the context of the target input element. If you’d like to access the CalendarDateSelect object itself, you can access it via “this.calendar_date_select”.

For example:

<%= calendar_date_select_tag "event_demo", "", :after_navigate => "alert('The current selected month is ' + this.calendar_date_select.selected_date.getMonth());" ,


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/calendar_date_select/form_helpers.rb', line 120

def calendar_date_select_tag( name, value = nil, options = {})
  image, options, javascript_options = calendar_date_select_process_options(options)
  value = CalendarDateSelect.format_time(value, javascript_options)

  javascript_options.delete(:format)

  options[:id] ||= name
  tag = javascript_options[:hidden] || javascript_options[:embedded] ?
    hidden_field_tag(name, value, options) :
    text_field_tag(name, value, options)

  calendar_date_select_output(tag, image, options, javascript_options)
end