12_hour_time 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/lib/12_hour_time.rb +2 -0
- data/lib/12_hour_time/action_view_helpers.rb +62 -0
- data/lib/12_hour_time/active_record_modifications.rb +28 -0
- data/lib/12_hour_time/awesome.rb +3 -0
- data/lib/12_hour_time/version.rb +3 -0
- metadata +74 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Nick Muerdter
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/12_hour_time.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module ActionView::Helpers
|
2
|
+
class DateTimeSelector
|
3
|
+
POSITION = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6, :ampm => 7 }
|
4
|
+
# XXX would like to do this, but it's frozen
|
5
|
+
# POSITION[:ampm] = 7
|
6
|
+
|
7
|
+
# We give them negative values so can differentiate between normal
|
8
|
+
# date/time values. The way the multi param stuff works, from what I
|
9
|
+
# can see, results in a variable number of fields (if you tell it to
|
10
|
+
# include seconds, for example). So we expect the AM/PM field, if
|
11
|
+
# present, to be last and have a negative value.
|
12
|
+
AM = -1
|
13
|
+
PM = -2
|
14
|
+
|
15
|
+
def select_hour_with_ampm
|
16
|
+
unless @options[:twelve_hour]
|
17
|
+
return select_hour_without_ampm
|
18
|
+
end
|
19
|
+
|
20
|
+
if @options[:use_hidden] || @options[:discard_hour]
|
21
|
+
build_hidden(:hour, hour12)
|
22
|
+
else
|
23
|
+
build_options_and_select(:hour, hour12, :start => 1, :end => 12)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
alias_method_chain :select_hour, :ampm
|
28
|
+
|
29
|
+
def select_ampm
|
30
|
+
selected = hour < 12 ? AM : PM unless hour.nil?
|
31
|
+
|
32
|
+
# XXX i18n?
|
33
|
+
label = { AM => 'AM', PM => 'PM' }
|
34
|
+
ampm_options = []
|
35
|
+
[AM, PM].each do |meridiem|
|
36
|
+
option = { :value => meridiem }
|
37
|
+
option[:selected] = "selected" if selected == meridiem
|
38
|
+
ampm_options << content_tag(:option, label[meridiem], option) + "\n"
|
39
|
+
end
|
40
|
+
build_select(:ampm, ampm_options.join)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def build_selects_from_types_with_ampm(order)
|
46
|
+
if @options[:twelve_hour] and order.include?(:hour)
|
47
|
+
order += [:ampm] unless order.include?(:ampm)
|
48
|
+
end
|
49
|
+
build_selects_from_types_without_ampm(order)
|
50
|
+
end
|
51
|
+
|
52
|
+
alias_method_chain :build_selects_from_types, :ampm
|
53
|
+
|
54
|
+
def hour12
|
55
|
+
return nil if hour.nil?
|
56
|
+
|
57
|
+
h12 = hour % 12
|
58
|
+
h12 = 12 if h12 == 0
|
59
|
+
return h12
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module TwelveHourTime
|
2
|
+
module AR
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def instantiate_time_object_with_ampm(name, values)
|
9
|
+
if values.last < 0
|
10
|
+
hour_idx = ActionView::Helpers::DateTimeSelector::POSITION[:hour] - 1
|
11
|
+
ampm = values.pop
|
12
|
+
if ampm == ActionView::Helpers::DateTimeSelector::AM and values[hour_idx] == 12
|
13
|
+
values[hour_idx] = 0
|
14
|
+
elsif ampm == ActionView::Helpers::DateTimeSelector::PM and values[hour_idx] != 12
|
15
|
+
values[hour_idx] += 12
|
16
|
+
end
|
17
|
+
end
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if defined? ActiveRecord
|
25
|
+
ActiveRecord::Base.class_eval do
|
26
|
+
include TwelveHourTime::AR
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: 12_hour_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Carl Lerche
|
13
|
+
- Yehuda Katz
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-23 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: You're definitely going to want to replace a lot of this
|
23
|
+
email:
|
24
|
+
- carlhuda@engineyard.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/12_hour_time.rb
|
33
|
+
- lib/12_hour_time/active_record_modifications.rb
|
34
|
+
- lib/12_hour_time/awesome.rb
|
35
|
+
- lib/12_hour_time/version.rb
|
36
|
+
- lib/12_hour_time/action_view_helpers.rb
|
37
|
+
- LICENSE
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/carlhuda/12_hour_time
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: -2156112900287113205
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 3
|
64
|
+
- 7
|
65
|
+
version: 1.3.7
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: 12_hour_time
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: A new gem templates
|
73
|
+
test_files: []
|
74
|
+
|