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
-
#calendar_date_select(object, method, options = {}) ⇒ Object
Similar to the difference between
text_field_tag
andtext_field
, this method behaves liketext_field
. -
#calendar_date_select_tag(name, value = nil, options = {}) ⇒ Object
Similar to text_field_tag, but adds a calendar picker, naturally.
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
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 150 151 152 153 154 155 156 |
# File 'lib/calendar_date_select/form_helpers.rb', line 123 def calendar_date_select(object, method, ={}) obj = [:object] || instance_variable_get("@#{object}") if !.include?(:time) && obj.class.respond_to?("columns_hash") column_type = (obj.class.columns_hash[method.to_s].type rescue nil) [:time] = true if column_type == :datetime end use_time = [:time] if [:time].to_s=="mixed" use_time = false if Date===(obj.respond_to?(method) && obj.send(method)) end , = () [: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, .delete(:object)) calendar_date_select_output( tag.to_input_field_tag( ([:hidden] || [:embedded]) ? "hidden" : "text", ), , ) 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 %>
:on_modal
If the calendar is being placed in a widget which is already a modal itself, it needs to be aware that it needs to appear on top of that modal. Not compatible with :embedded => true.
<%= calendar_date_select_tag “name”, “2007-01-01”, :on_modal => 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] %>
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());" ,
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/calendar_date_select/form_helpers.rb', line 106 def calendar_date_select_tag( name, value = nil, = {}) , = () value = CalendarDateSelect.format_time(value, ) .delete(:format) [:id] ||= name tag = [:hidden] || [:embedded] ? hidden_field_tag(name, value, ) : text_field_tag(name, value, ) calendar_date_select_output(tag, , ) end |