Module: CalendarDateSelect

Defined in:
lib/calendar_date_select/calendar_date_select.rb

Defined Under Namespace

Modules: FormHelpers, IncludesHelper

Constant Summary collapse

VERSION =
'1.13'
FORMATS =
{
  :natural => {
    :date => "%B %d, %Y",
    :time => " %I:%M %p"
  },
  :hyphen_ampm => {
    :date => "%Y-%m-%d",
    :time => " %I:%M %p",
    :javascript_include => "format_hyphen_ampm"
  },
  :iso_date => {
    :date => "%Y-%m-%d",
    :time => " %H:%M",
    :javascript_include => "format_iso_date"
  },
  :finnish => {
    :date => "%d.%m.%Y",
    :time => " %H:%M",
    :javascript_include => "format_finnish"
  },
  :american => {
    :date => "%m/%d/%Y",
    :time => " %I:%M %p",
    :javascript_include => "format_american"
  },
  :euro_24hr => {
    :date => "%d %B %Y",
    :time => " %H:%M",
    :javascript_include => "format_euro_24hr"
  },
  :euro_24hr_ymd => {
    :date => "%Y.%m.%d",
    :time => " %H:%M",
    :javascript_include => "format_euro_24hr_ymd"
  },
  :italian => {
    :date => "%d/%m/%Y",
    :time => " %H:%M",
    :javascript_include => "format_italian"
  },
  :db => {
    :date => "%Y-%m-%d",
    :time => " %H:%M",
    :javascript_include => "format_db"
  }
}

Class Method Summary collapse

Class Method Details

.date_format_string(time = false) ⇒ Object



86
87
88
# File 'lib/calendar_date_select/calendar_date_select.rb', line 86

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

.default_optionsObject

Returns the default_options hash. These options are by default provided to every calendar_date_select control, unless otherwise overrided.

Example:

# At the bottom of config/environment.rb:
CalendarDateSelect.default_options.update(
  :popup => "force",
  :month_year => "label",
  :image => "custom_calendar_picker.png"
)


59
60
61
# File 'lib/calendar_date_select/calendar_date_select.rb', line 59

def self.default_options
  @default_options ||= { :image => "calendar_date_select/calendar.gif" }
end

.formatObject

Returns the options for the given format

Example:

CalendarDateSelect.format = :italian
puts CalendarDateSelect.format[:date]
  => "%d/%m/%Y"


74
75
76
# File 'lib/calendar_date_select/calendar_date_select.rb', line 74

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

.format=(format) ⇒ Object

Set the format. To see a list of available formats, CalendarDateSelect::FORMATS.keys, or open lib/calendar_date_select/calendar_date_select.rb

(e.g. CalendarDateSelect.format = :italian)



81
82
83
84
# File 'lib/calendar_date_select/calendar_date_select.rb', line 81

def self.format=(format)
  raise "CalendarDateSelect: Unrecognized format specification: #{format}" unless FORMATS.has_key?(format)
  @format = FORMATS[format]
end

.format_date(date) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/calendar_date_select/calendar_date_select.rb', line 90

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

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



98
99
100
101
102
103
104
105
# File 'lib/calendar_date_select/calendar_date_select.rb', line 98

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

.has_time?(value) ⇒ Boolean

Detects the presence of time in a date, string

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
# File 'lib/calendar_date_select/calendar_date_select.rb', line 108

def self.has_time?(value)
  case value
  when DateTime, Time then true
  when Date           then false
  else
    /[0-9]:[0-9]{2}/.match(value.to_s) ? true : false
  end
end

.image=(value) ⇒ Object

Set the picker image. Provide the image url the same way you would provide it to image_tag



64
65
66
# File 'lib/calendar_date_select/calendar_date_select.rb', line 64

def self.image=(value)
  default_options[:image] = value
end