Skip to content

Commit

Permalink
Add support for a single text input picker.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjlandau committed Oct 1, 2008
1 parent e33aa68 commit 85be357
Show file tree
Hide file tree
Showing 11 changed files with 739 additions and 505 deletions.
18 changes: 17 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ There are 4 main methods:
- unobtrusive_datetime_picker
- unobtrusive_date_picker_tags
- unobtrusive_datetime_picker_tags
- unobtrusive_date_text_picker
- unobtrusive_date_text_picker_tag

Options (* indicates same functionality as is in Rails helpers):
Options (* indicates same functionality as is in Rails Date helpers):
- *:order => the order the selects should be positioned in
- *:include_blank => include a blank option at the top of every select
- *:start_year => year that the year select should start on (defaults to 5
Expand Down Expand Up @@ -66,6 +68,20 @@ Options (* indicates same functionality as is in Rails helpers):
- :no_transparency => if set to true it disables the fade in/out
visual effect of the datepicker

The `unobtrusive_date_text_picker` and `unobtrusive_date_text_picker_tag` methods
don't except the ":order", ":include_blank", ":start_year", ":end_year", ":minute_step", ":use_short_month",
":use_month_numbers", and ":add_month_numbers" options.
It does use these options though:
- :format => the format the date should be in
- m-d-y
- d-m-y
- y-m-d
- :divider => the divider used between the dates
- "slash" or "/"
- "dash" or "-"
- "dot" or "."
- "space" or " "


==== RJS Method

Expand Down
2 changes: 1 addition & 1 deletion about.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Unobtrusive Date-Picker Widget
author: Brian Landau
version: 1.3
version: 1.4
description: Helper to create a set of Date/Time selects that use the Unobtrusive Date Picker Widget.
url: http://code.google.com/p/rails-unobtrusive-date-picker/
install: http://rails-unobtrusive-date-picker.googlecode.com/svn/trunk/unobtrusive_date_picker
Expand Down
180 changes: 93 additions & 87 deletions lib/12_hour_time.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#
# == Rails Twelve Hour Time Plugin
#
Expand All @@ -15,115 +14,122 @@

# :enddoc:
class ActiveRecord::Base # :nodoc: all
def extract_callstack_for_multiparameter_attributes_with_ampm(pairs)
attributes = extract_callstack_for_multiparameter_attributes_without_ampm(pairs)
attributes.each do |name, values|
klass = (self.class.reflect_on_aggregation(name) ||
column_for_attribute(name)).klass
if klass == Time && values.length == 6
if values[5] == ActionView::Helpers::DateHelper::AM && values[3] == 12
values[3] = 0
elsif values[5] == ActionView::Helpers::DateHelper::PM && values[3] != 12
values[3] += 12
end
end
def extract_callstack_for_multiparameter_attributes_with_ampm(pairs)
attributes = extract_callstack_for_multiparameter_attributes_without_ampm(pairs)
attributes.each do |name, values|
klass = (self.class.reflect_on_aggregation(name) ||
column_for_attribute(name)).klass
if klass == Time && values.length == 6
if values[5] == ActionView::Helpers::DateHelper::AM && values[3] == 12
values[3] = 0
elsif values[5] == ActionView::Helpers::DateHelper::PM && values[3] != 12
values[3] += 12
end
end
end
end
end

alias_method_chain :extract_callstack_for_multiparameter_attributes, :ampm
alias_method_chain :extract_callstack_for_multiparameter_attributes, :ampm
end

module ActionView::Helpers::DateHelper # :nodoc: all
AM = 'AM'
PM = 'PM'
AM = 'AM'
PM = 'PM'

def select_hour_with_ampm(datetime, options = {}, html_options = {})
options[:twelve_hour] or return select_hour_without_ampm(datetime, options)
def select_hour_with_ampm(datetime, options = {}, html_options = {})
options[:twelve_hour] or return select_hour_without_ampm(datetime, options, html_options)

val = _12_hour(datetime)
val = _12_hour(datetime)

if options[:use_hidden]
return hidden_html(options[:field_name] || 'hour', val, options)
end
if options[:use_hidden]
return hidden_html(options[:field_name] || 'hour', val, options)
end

hour_options = []
1.upto(12) do |hour|
selected = (hour == val) ? ' selected="selected"' : ''
hour_options << %(<option value="#{leading_zero_on_single_digits(hour)}"#{selected}>#{leading_zero_on_single_digits(hour)}</option>\n)
end
hour_options = []
1.upto(12) do |hour|
hour_options << ((val == hour) ?
content_tag(:option, leading_zero_on_single_digits(hour), :value => leading_zero_on_single_digits(hour), :selected => "selected") :
content_tag(:option, leading_zero_on_single_digits(hour), :value => leading_zero_on_single_digits(hour))
)
hour_options << "\n"
end

select_html(options[:field_name] || 'hour', hour_options, options, html_options)
end
select_html(options[:field_name] || 'hour', hour_options, options, html_options)
end

alias_method_chain :select_hour, :ampm
alias_method_chain :select_hour, :ampm


def select_ampm(datetime, options = {}, html_options = {})
ampm = [AM, PM]
val = datetime ? (ampm.include?(datetime) ? datetime : datetime.strftime("%p")) : ''
def select_ampm(datetime, options = {}, html_options = {})
ampm = [AM, PM]
val = datetime ? (ampm.include?(datetime) ? datetime : datetime.strftime("%p")) : ''

if options[:use_hidden]
return hidden_html(options[:field_name] || 'ampm', val, options)
end
if options[:use_hidden]
return hidden_html(options[:field_name] || 'ampm', val, options)
end

ampm_options = []
ampm.each do |meridiem|
selected = (meridiem == val) ? ' selected="selected"' : ''
ampm_options << %(<option value="#{meridiem}"#{selected}>#{meridiem}</option>\n)
end
ampm_options = []
ampm.each do |meridiem|
selected = (meridiem == val) ? ' selected="selected"' : ''
ampm_options << ((meridiem == val) ?
content_tag(:option, meridiem, :value => meridiem, :selected => "selected") :
content_tag(:option, meridiem, :value => meridiem)
)
ampm_options << "\n"
end

select_html(options[:field_name] || 'ampm', ampm_options, options, html_options)
end
select_html(options[:field_name] || 'ampm', ampm_options, options, html_options)
end

def select_time_with_ampm(datetime = Time.now, options = {}, html_options = {})
select = select_time_without_ampm(datetime, options, html_options)
select << select_ampm(datetime, options, html_options) if options[:twelve_hour]
select
end
def select_time_with_ampm(datetime = Time.now, options = {}, html_options = {})
select = select_time_without_ampm(datetime, options, html_options)
select << select_ampm(datetime, options, html_options) if options[:twelve_hour]
select
end

alias_method_chain :select_time, :ampm
alias_method_chain :select_time, :ampm

private
private

def _12_hour(datetime)
return '' if datetime.blank?
def _12_hour(datetime)
return '' if datetime.blank?

hour = datetime.kind_of?(Fixnum) ? datetime : datetime.hour
hour = 12 if hour == 0
hour -= 12 if hour > 12
hour = datetime.kind_of?(Fixnum) ? datetime : datetime.hour
hour = 12 if hour == 0
hour -= 12 if hour > 12

return hour
end
return hour
end
end

class ActionView::Helpers::InstanceTag # :nodoc: all
def date_or_time_select_with_ampm(options, html_options)
options[:twelve_hour] and not options[:discard_hour] or
return date_or_time_select_without_ampm(options, html_options)

defaults = { :discard_type => true }
options = defaults.merge(options)

datetime = value(object)
datetime ||= Time.now unless options[:include_blank]

date_or_time_select_without_ampm(options, html_options) +
select_ampm(datetime, options_with_prefix(6, options.merge(:use_hidden => options[:discard_hour], :string => true)))
end

alias_method_chain :date_or_time_select, :ampm

def options_with_prefix_with_ampm(position, options)
prefix = "#{@object_name}"
if options[:index]
prefix << "[#{options[:index]}]"
elsif @auto_index
prefix << "[#{@auto_index}]"
end
if options[:string]
options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}s)]")
else
options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]")
end
end
def date_or_time_select_with_ampm(options, html_options = {})
options[:twelve_hour] and not options[:discard_hour] or
return date_or_time_select_without_ampm(options, html_options)

defaults = { :discard_type => true }
options = defaults.merge(options)

datetime = value(object)
datetime ||= default_time_from_options(options[:default]) unless options[:include_blank]

date_or_time_select_without_ampm(options, html_options) +
select_ampm(datetime, options_with_prefix(6, options.merge(:use_hidden => options[:discard_hour], :string => true)))
end

alias_method_chain :date_or_time_select, :ampm

def options_with_prefix_with_ampm(position, options)
prefix = "#{@object_name}"
if options[:index]
prefix << "[#{options[:index]}]"
elsif @auto_index
prefix << "[#{@auto_index}]"
end
if options[:string]
options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}s)]")
else
options.merge(:prefix => "#{prefix}[#{@method_name}(#{position}i)]")
end
end
end
Loading

0 comments on commit 85be357

Please sign in to comment.