Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/app/.DS_Store
Binary file not shown.
38 changes: 31 additions & 7 deletions lib/app/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ module FormHelper
# Mehtod that generates datepicker input field inside a form
def datepicker(object_name, method, options = {}, timepicker = false)
input_tag = JqueryDatepicker::InstanceTag.new(object_name, method, self, options.delete(:object))
dp_options, tf_options = input_tag.split_options(options)
tf_options[:value] = input_tag.format_date(tf_options[:value], String.new(dp_options[:dateFormat])) if tf_options[:value] && !tf_options[:value].empty? && dp_options.has_key?(:dateFormat)
html = input_tag.to_input_field_tag("text", tf_options)
html, dp_options = input_tag.render
method = timepicker ? "datetimepicker" : "datepicker"
html += javascript_tag("jQuery(document).ready(function(){jQuery('##{input_tag.get_name_and_id(tf_options.stringify_keys)["id"]}').#{method}(#{dp_options.to_json})});")
html += javascript_tag("jQuery(document).ready(function(){jQuery('##{input_tag.get_name_and_id["id"]}').#{method}(#{dp_options.to_json})});")
html.html_safe
end
end
Expand All @@ -28,7 +26,13 @@ def datetime_picker(method, options = {})
end
end

class JqueryDatepicker::InstanceTag < ActionView::Helpers::InstanceTag
begin
JqueryDatepicker::InstanceTag = Class.new(ActionView::Helpers::Tags::Base)
rescue
JqueryDatepicker::InstanceTag = Class.new(ActionView::Helpers::InstanceTag)
end

class JqueryDatepicker::InstanceTag

FORMAT_REPLACEMENTES = { "yy" => "%Y", "mm" => "%m", "dd" => "%d", "d" => "%-d", "m" => "%-m", "y" => "%y", "M" => "%b"}

Expand All @@ -39,9 +43,9 @@ def get_name_and_id(options = {})
add_default_name_and_id(options)
options
end

def available_datepicker_options
[:disabled, :altField, :altFormat, :appendText, :autoSize, :buttonImage, :buttonImageOnly, :buttonText, :calculateWeek, :changeMonth, :changeYear, :closeText, :constrainInput, :currentText, :dateFormat, :dayNames, :dayNamesMin, :dayNamesShort, :defaultDate, :duration, :firstDay, :gotoCurrent, :hideIfNoPrevNext, :isRTL, :maxDate, :minDate, :monthNames, :monthNamesShort, :navigationAsDateFormat, :nextText, :numberOfMonths, :prevText, :selectOtherMonths, :shortYearCutoff, :showAnim, :showButtonPanel, :showCurrentAtPos, :showMonthAfterYear, :showOn, :showOptions, :showOtherMonths, :showWeek, :stepMonths, :weekHeader, :yearRange, :yearSuffix]
['disabled', 'altField', 'altFormat', 'appendText', 'autoSize', 'buttonImage', 'buttonImageOnly', 'buttonText', 'calculateWeek', 'changeMonth', 'changeYear', 'closeText', 'constrainInput', 'currentText', 'dateFormat', 'dayNames', 'dayNamesMin', 'dayNamesShort', 'defaultDate', 'duration', 'firstDay', 'gotoCurrent', 'hideIfNoPrevNext', 'isRTL', 'maxDate', 'minDate', 'monthNames', 'monthNamesShort', 'navigationAsDateFormat', 'nextText', 'numberOfMonths', 'prevText', 'selectOtherMonths', 'shortYearCutoff', 'showAnim', 'showButtonPanel', 'showCurrentAtPos', 'showMonthAfterYear', 'showOn', 'showOptions', 'showOtherMonths', 'showWeek', 'stepMonths', 'weekHeader', 'yearRange', 'yearSuffix']
end

def split_options(options)
Expand All @@ -62,4 +66,24 @@ def translate_format(format)
format.gsub!(/#{FORMAT_REPLACEMENTES.keys.join("|")}/) { |match| FORMAT_REPLACEMENTES[match] }
end

def render
options = @options.stringify_keys
dp_options, tf_options = split_options(options)
tf_options['value'] = format_date(tf_options['value'], String.new(dp_options['dateFormat'])) if tf_options['value'] && !tf_options['value'].empty? && dp_options.has_key?('dateFormat')
add_default_name_and_id(options)
return tag("text", tf_options), dp_options
end

class << self
def field_type
@field_type ||= "text"
end
end

private

def field_type
self.class.field_type
end

end