Module: Jquids

Extended by:
ActionView::Helpers::AssetTagHelper
Defined in:
lib/jquids/jquids.rb,
lib/jquids/form_helpers.rb,
lib/jquids/includes_helper.rb,
lib/jquids/constants/styles.rb,
lib/jquids/constants/formats.rb,
lib/jquids/constants/version.rb,
lib/jquids/constants/jq_versions.rb,
lib/jquids/constants/ui_versions.rb,
lib/jquids/constants/timepicker_tags.rb

Defined Under Namespace

Modules: FormHelpers, IncludesHelper Classes: NotAKnownFormat, NotAKnownStyle

Constant Summary collapse

STYLES =
{
  :base => "base",
  :black_tie => "black-tie",
  :blitzer => "blitzer",
  :cupertino => "cupertino",
  :dark_hive => "dark-hive",
  :dot_luv => "dot-luv",
  :eggplant => "eggplant",
  :excite_bike => "excite-bike",
  :flick => "flick",
  :hot_sneaks => "hot-sneaks",
  :humanity => "humanity",
  :le_frog => "le-frog",
  :mint_choc => "mint-choc",
  :overcast => "overcast",
  :pepper_grinder => "pepper-grinder",
  :redmond => "redmond",
  :smoothness => "smoothness",
  :south_street => "south-street",
  :start => "start",
  :sunny => "sunny",
  :swanky_purse => "swanky-purse",
  :trontastic => "trontastic",
  :ui_darkness => "ui-darkness",
  :ui_lightness => "ui-lightness",
  :vader => "vader"
}
FORMATS =
{
  :natural => {
    :date => "%B %d, %Y",
    :time => " %I:%M %p",
    :js_date => "MM dd, yy",
    :tr_js_time => "hh:mm TT",
    :ampm => true
  },
  :hyphen_ampm => {
    :date => "%Y-%m-%d",
    :time => " %I:%M %p",
    :js_date => "yy-mm-dd",
    :tr_js_time => "hh:mm TT",
    :ampm => true
  },
  :iso_date => {
    :date => "%Y-%m-%d",
    :time => " %H:%M",
    :js_date => "yy-mm-dd",
    :tr_js_time => "hh:mm",
    :ampm => false
  },
  :finnish => {
    :date => "%d.%m.%Y",
    :time => " %H:%M",
    :js_date => "dd.mm.yy",
    :tr_js_time => "hh:mm",
    :ampm => false
  },
  :danish => {
    :date => "%d/%m/%Y",
    :time => " %H:%M",
    :js_date => "dd/mm/yy",
    :tr_js_time => "hh:mm",
    :ampm => false
  },
  :american => {
    :date => "%m/%d/%Y",
    :time => " %I:%M %p",
    :js_date => "mm/dd/yy",
    :tr_js_time => "hh:mm TT",
    :ampm => true
  },
  :euro_24hr => {
    :date => "%d %B %Y",
    :time => " %H:%M",
    :js_date => "dd MM yy",
    :tr_js_time => "hh:mm",
    :ampm => false
  },
  :euro_24hr_ymd => {
    :date => "%Y.%m.%d",
    :time => " %H:%M",
    :js_date => "yy.mm.dd",
    :tr_js_time => "hh:mm",
    :ampm => false
  },
  :italian => {
    :date => "%d/%m/%Y",
    :time => " %H:%M",
    :js_date => "dd/mm/yy",
    :tr_js_time => "hh:mm",
    :ampm => false
  },
  :db => {
    :date => "%Y-%m-%d",
    :time => " %H:%M",
    :js_date => "yy-mm-dd",
    :tr_js_time => "hh:mm",
    :ampm => false
  }
}
VERSION =
"0.2.1"
JQVersions =
%w{1.2.3 1.2.6 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1}
UIVersions =
%w{1.5.2 1.5.3 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.8.10 1.8.11 1.8.12 1.8.13}
TimepickerTags =
%w{v0.8 v0.9 v0.9.1 v0.9.2 v0.9.3 v0.9.4 v0.9.5}

Class Method Summary collapse

Class Method Details

.date_format_string(time = false) ⇒ Object

Gets a format string based off of the current format

The format string will always return the date portion of the format

If a true is passed for time, the time string will be tacked on to the string
Else it will return just the date string


26
27
28
# File 'lib/jquids/jquids.rb', line 26

def self.date_format_string(time = false)
  format[:date] + (time ? format[:time] : "")
end

.formatObject

Gets the format variable

If a format is set, it grabs it
Else it will set it to the default format (:natural)


17
18
19
# File 'lib/jquids/jquids.rb', line 17

def self.format
  @jquids_format ||= Jquids::FORMATS[:natural]
end

.format=(key) ⇒ Object

Sets the format for the jquids class

If a format doesn't exist, and error is through
Else the format variable is set


9
10
11
12
# File 'lib/jquids/jquids.rb', line 9

def self.format=(key)
  raise Jquids::NotAKnownFormat, "Jquids: Unrecognized format specification: #{key}" unless Jquids::FORMATS.has_key?(key)
  @jquids_format = Jquids::FORMATS[key]
end

.format_date(date) ⇒ Object

Formats a date or datetime/time object to the default format

This method is assumed that you send it a Date, Datetime, or Time object

If sent a Date, the format will NOT use the time in the format string
Else, it WILL use the time in the format string


35
36
37
38
39
40
41
# File 'lib/jquids/jquids.rb', line 35

def self.format_date(date)
  if date.is_a?(Date) && !date.is_a?(DateTime)
    date.strftime(date_format_string(false))
  else
    date.strftime(date_format_string(true))
  end
end

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

Returns the value that will go in the form builder’s input field when the field is initialized.

This will detect if it is a date or not, and format it if it is

If the value sent to the method is a Date
  If the options hash has :time, format the result with the time included
  Else format the result without the time
Else return the given value as the result (with no changes)


51
52
53
54
55
56
57
58
59
# File 'lib/jquids/jquids.rb', line 51

def self.format_time(value, options = {})
  return value unless value.respond_to?("strftime")
  if options[:time]
    format_date(value)
  else
    format_date(value.is_a?(Date) ? value : value.to_date)
  end

end

.jquids_process_options(options = {}) ⇒ Object

Processes old CalendarDateSelect options and converts them to the coresponding jQuery UI options values for the timepicker and datepicker.

If no keys exist for :timepicker_options or :datepicker options, they are removed to avoid the client side processing.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jquids/jquids.rb', line 66

def self.jquids_process_options(options = {})
  options[:datepicker_options] ||= {}
  options[:timepicker_options] ||= {}

  if options.has_key?(:year_range)
    if options[:year_range].respond_to?(:first)
      options[:datepicker_options][:yearRange] = options[:year_range].first.respond_to?(:strftime) ? 
        "#{options[:year_range].first.strftime("%Y")}:#{options[:year_range].last.strftime("%Y")}" : "#{options[:year_range].first}:#{options[:year_range].last}"
    else
      options[:datepicker_options][:yearRange] = options[:year_range].respond_to?(:strftime) ? 
        "#{options[:year_range].strftime("%Y")}:#{options[:year_range].strftime("%Y")}" : "#{options[:year_range]}:#{options[:year_range]}"
    end
    options.delete(:year_range)
  end

  if options[:month_year] == "dropdowns"
    options[:datepicker_options][:changeMonth] = true
    options[:datepicker_options][:changeYear] = true
  elsif options[:month_year] == "labels"
    options[:datepicker_options][:changeMonth] = false
    options[:datepicker_options][:changeYear] = false
  end
  options.delete(:month_year)

  if options.has_key?(:minute_interval) and options[:minute_interval].is_a?(Numeric)
    options[:timepicker_options][:stepMinute] = options[:minute_interval] 
  end
  options.delete(:minute_interval)

  if options.delete(:popup).to_s == 'force'
    options[:readonly] = true
  end

  if default_time = options.delete(:default_time)
    options[:datepicker_options][:defaultDate] = 
      if default_time.respond_to? :strftime
        default_time.strftime(Jquids.date_format_string(false))
      else
        default_time
      end
    if default_time.respond_to?(:hour)
      options[:timepicker_options][:hour] = default_time.hour
      options[:timepicker_options][:minute] = default_time.min
      options[:timepicker_options][:second] = default_time.sec
    end
  end

  if options.has_key?(:image)
    options[:datepicker_options][:showOn] ||= 'button'
    options[:datepicker_options][:buttonImageOnly] = true
    options[:datepicker_options][:buttonImage] = image_path(options[:image])
    options.delete(:image)
  end

  # For slightly trimming unneeded html, and for less client side processing
  options.delete(:datepicker_options) if options[:datepicker_options].keys.count <= 0
  options.delete(:timepicker_options) if options[:timepicker_options].keys.count <= 0
  options
end