combined_time_select 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/combined_time_select.gemspec +26 -0
- data/lib/combined_time_select.rb +120 -0
- data/lib/combined_time_select/version.rb +3 -0
- metadata +71 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Anthony Amoyal
|
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/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Combined Time Select
|
2
|
+
====================
|
3
|
+
|
4
|
+
This is a small gem for creating Google Calendar style 12 hour AM/PM
|
5
|
+
time_select fields for Rails 3. It's based off of [simple_time_select](https://github.com/tamoyal/simple_time_select) by tamoyal.
|
6
|
+
|
7
|
+
Usage
|
8
|
+
-----
|
9
|
+
|
10
|
+
Here we have a model called Event with the start_time attribute that we
|
11
|
+
will be using with combined_time_select.
|
12
|
+
|
13
|
+
Because Rails time_select fields submit separate values, there is some
|
14
|
+
overhead on the controller side when we combine the fields into one. We
|
15
|
+
can't rely on Rails to parse the input into a valid time anymore.
|
16
|
+
|
17
|
+
In the view you can do the following:
|
18
|
+
|
19
|
+
<%= f.time_select :start_time,
|
20
|
+
:combined => true,
|
21
|
+
:default => Time.now.change(:hour => 11, :min => 30),
|
22
|
+
:minute_interval => 15,
|
23
|
+
:time_separator => "",
|
24
|
+
:start_hour => 10,
|
25
|
+
:end_hour => 14 } %>
|
26
|
+
|
27
|
+
This will create a combined time select starting at 10 AM and going till
|
28
|
+
2 PM with 15 minute intervals with a default of 11:30 AM. This will set the
|
29
|
+
value for the start_time attribute on the object this form was created
|
30
|
+
for.
|
31
|
+
|
32
|
+
On the controller side, we need to parse this attribute before we create
|
33
|
+
a new object. combined_time_select provides a nice method for this
|
34
|
+
called parse_time_select!. You can use this in your create action just
|
35
|
+
before you initialize the new model:
|
36
|
+
|
37
|
+
def create
|
38
|
+
params[:event].parse_time_select! :start_time
|
39
|
+
@event = Event.new params[:event]
|
40
|
+
end
|
41
|
+
|
42
|
+
And voila! You're all set.
|
43
|
+
|
44
|
+
Behind The Scenes
|
45
|
+
-----------------
|
46
|
+
|
47
|
+
When the form gets submitted we will recieve a params hash like so:
|
48
|
+
|
49
|
+
{"utf8"=>"✓", "event"=>{"start_time(5i)"=>"10:00:00"}, "commit"=>"Save changes"}
|
50
|
+
|
51
|
+
A normal time_select wil use start_time(4i) for the hours and
|
52
|
+
start_time(5i) for the minutes. The parse_time_select! will take all the
|
53
|
+
start_time(Xi) fields, parse them into a Time object and set the
|
54
|
+
params[:start_time] attribute to this object. The result after a
|
55
|
+
parse_time_select!(attribute) looks like this:
|
56
|
+
|
57
|
+
{"utf8"=>"✓", "event"=>{"start_time"=>Fri, 23 Dec 2011 10:00:00 UTC +00:00}, "commit"=>"Save changes"}
|
58
|
+
|
59
|
+
This allows you to also seamlessly use a date_select field with
|
60
|
+
combined_time_select.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "combined_time_select/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "combined_time_select"
|
7
|
+
s.version = CombinedTimeSelect::VERSION
|
8
|
+
s.authors = ["Chris Oliver"]
|
9
|
+
s.email = ["excid3@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/excid3/combined_time_select"
|
11
|
+
s.summary = %q{A Rails time_select like Google Calendar with combined hour and minute time_select}
|
12
|
+
s.description = %q{Generates a time_select field like Google calendar.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "combined_time_select"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require "combined_time_select/version"
|
2
|
+
|
3
|
+
module CombinedTimeSelect
|
4
|
+
# Your code goes here...
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
module ActiveSupport
|
9
|
+
class HashWithIndifferentAccess < Hash
|
10
|
+
def parse_time_select!(attribute)
|
11
|
+
self[attribute] = Time.zone.parse("#{self["#{attribute}(1i)"]}-#{self["#{attribute}(2i)"]}-#{self["#{attribute}(3i)"]} #{self["#{attribute}(5i)"]}")
|
12
|
+
(1..5).each { |i| self.delete "#{attribute}(#{i}i)" }
|
13
|
+
self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
module ActionView::Helpers
|
20
|
+
class DateTimeSelector
|
21
|
+
def select_minute_with_simple_time_select
|
22
|
+
return select_minute_without_simple_time_select unless @options[:combined].eql? true
|
23
|
+
|
24
|
+
# Although this is a datetime select, we only care about the time. Assume that the date will
|
25
|
+
# be set by some other control, and the date represented here will be overriden
|
26
|
+
|
27
|
+
val_minutes = @datetime.kind_of?(Time) ? @datetime.min + @datetime.hour*60 : @datetime
|
28
|
+
|
29
|
+
@options[:time_separator] = ""
|
30
|
+
|
31
|
+
# Default is 15 minute intervals
|
32
|
+
minute_interval = 15
|
33
|
+
if @options[:minute_interval]
|
34
|
+
minute_interval = @options[:minute_interval]
|
35
|
+
end
|
36
|
+
|
37
|
+
start_minute = 0
|
38
|
+
# @options[:start_hour] should be specified in military
|
39
|
+
# i.e. 0-23
|
40
|
+
if @options[:start_hour]
|
41
|
+
start_minute = @options[:start_hour] * 60
|
42
|
+
end
|
43
|
+
|
44
|
+
end_minute = 1439
|
45
|
+
# @options[:end_hour] should be specified in military
|
46
|
+
# i.e. 0-23
|
47
|
+
if @options[:end_hour]
|
48
|
+
end_minute = ( @options[:end_hour] + 1 ) * 60 - 1
|
49
|
+
end
|
50
|
+
|
51
|
+
if @options[:use_hidden] || @options[:discard_minute]
|
52
|
+
build_hidden(:minute, val)
|
53
|
+
else
|
54
|
+
minute_options = []
|
55
|
+
start_minute.upto(end_minute) do |minute|
|
56
|
+
if minute%minute_interval == 0
|
57
|
+
ampm = minute < 720 ? ' AM' : ' PM'
|
58
|
+
hour = minute/60
|
59
|
+
minute_padded = zero_pad_num(minute%60)
|
60
|
+
hour_padded = zero_pad_num(hour)
|
61
|
+
ampm_hour = ampm_hour(hour)
|
62
|
+
|
63
|
+
val = "#{hour_padded}:#{minute_padded}:00"
|
64
|
+
minute_options << ((val_minutes == minute) ?
|
65
|
+
%(<option value="#{val}" selected="selected">#{ampm_hour}:#{minute_padded}#{ampm}</option>\n) :
|
66
|
+
%(<option value="#{val}">#{ampm_hour}:#{minute_padded}#{ampm}</option>\n)
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
build_select(:minute, minute_options.join(' '))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
alias_method_chain :select_minute, :simple_time_select
|
74
|
+
|
75
|
+
|
76
|
+
def select_hour_with_simple_time_select
|
77
|
+
return select_hour_without_simple_time_select unless @options[:combined].eql? true
|
78
|
+
# Don't build the hour select
|
79
|
+
#build_hidden(:hour, val)
|
80
|
+
end
|
81
|
+
alias_method_chain :select_hour, :simple_time_select
|
82
|
+
|
83
|
+
def select_second_with_simple_time_select
|
84
|
+
return select_second_without_simple_time_select unless @options[:combined].eql? true
|
85
|
+
# Don't build the seconds select
|
86
|
+
#build_hidden(:second, val)
|
87
|
+
end
|
88
|
+
alias_method_chain :select_second, :simple_time_select
|
89
|
+
|
90
|
+
def select_year_with_simple_time_select
|
91
|
+
return select_year_without_simple_time_select unless @options[:combined].eql? true
|
92
|
+
# Don't build the year select
|
93
|
+
#build_hidden(:year, val)
|
94
|
+
end
|
95
|
+
alias_method_chain :select_year, :simple_time_select
|
96
|
+
|
97
|
+
def select_month_with_simple_time_select
|
98
|
+
return select_month_without_simple_time_select unless @options[:combined].eql? true
|
99
|
+
# Don't build the month select
|
100
|
+
#build_hidden(:month, val)
|
101
|
+
end
|
102
|
+
alias_method_chain :select_month, :simple_time_select
|
103
|
+
|
104
|
+
def select_day_with_simple_time_select
|
105
|
+
return select_day_without_simple_time_select unless @options[:combined].eql? true
|
106
|
+
# Don't build the day select
|
107
|
+
#build_hidden(:day, val)
|
108
|
+
end
|
109
|
+
alias_method_chain :select_day, :simple_time_select
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def ampm_hour(hour)
|
115
|
+
return hour == 12 ? 12 : (hour == 0 ? 12 : (hour / 12 == 1 ? hour % 12 : hour))
|
116
|
+
end
|
117
|
+
|
118
|
+
def zero_pad_num(num)
|
119
|
+
return num < 10 ? '0' + num.to_s : num.to_s
|
120
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: combined_time_select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Oliver
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-23 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70360704093620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70360704093620
|
25
|
+
description: Generates a time_select field like Google calendar.
|
26
|
+
email:
|
27
|
+
- excid3@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- CHANGELOG.md
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- combined_time_select.gemspec
|
39
|
+
- lib/combined_time_select.rb
|
40
|
+
- lib/combined_time_select/version.rb
|
41
|
+
homepage: https://github.com/excid3/combined_time_select
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
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
|
+
version: '0'
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
hash: 2502057723234992945
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: 2502057723234992945
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project: combined_time_select
|
67
|
+
rubygems_version: 1.8.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A Rails time_select like Google Calendar with combined hour and minute time_select
|
71
|
+
test_files: []
|