nbrew-simple_time_select 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/MIT_LICENSE +20 -0
- data/README +64 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/about.yml +7 -0
- data/init.rb +1 -0
- data/lib/simple_time_select.rb +81 -0
- data/nbrew-simple_time_select.gemspec +52 -0
- data/pkg/simple_time_select-0.0.0.gem +0 -0
- data/test/simple_time_select_test.rb +82 -0
- data/test/test_helper.rb +3 -0
- metadata +79 -0
data/CHANGELOG
ADDED
data/MIT_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
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
SIMPLE TIME SELECT PLUGIN
|
2
|
+
|
3
|
+
Ever wanted a time select component with only one select field? This simple plugin
|
4
|
+
gives you that component and allows you to set minute intervals. If you set your
|
5
|
+
minute interval to 15, you get options such as:
|
6
|
+
"6:00 PM", "6:15 PM", "6:30 PM", etc.
|
7
|
+
|
8
|
+
If no minute interval is specified, the control defaults to a 15 minute interval.
|
9
|
+
As you can see from the sample values above, this control also implements an AM/PM time
|
10
|
+
format.
|
11
|
+
|
12
|
+
Also note that this component is great if you ONLY want the time from a user. As is, the code
|
13
|
+
prevents the hidden date fields associated with the time_select helper from being created. This means
|
14
|
+
that a couple lines of code on the controller side are necessary to make this work. See USAGE for details.
|
15
|
+
|
16
|
+
USAGE:
|
17
|
+
|
18
|
+
To create the time select:
|
19
|
+
|
20
|
+
<%= time_select "event", "time", { :default => Time.now.change(:hour => 21), :simple_time_select => true, :minute_interval => 20, :time_separator => "" } %>
|
21
|
+
|
22
|
+
Don't forget to include the time_separator option. Otherwise you will get an extra colon outside of the select field.
|
23
|
+
|
24
|
+
Simple time select also takes a start_hour and end_hour option to be specified in military format (between 0-23).
|
25
|
+
|
26
|
+
<%= time_select "event", "time", { :default => Time.now.change(:hour => 21), :simple_time_select => true, :minute_interval => 20, :time_separator => "", :start_hour => 10, :end_hour => 14 } %>
|
27
|
+
|
28
|
+
The start hour behaves as you would expect but the end_hour may not. If you specify the end_hour as 10, your time select will include 10:15, 10:30, 10:45. So the end_hour sets the last hour that the time select will include. Email me if you don't like this.
|
29
|
+
|
30
|
+
When the time is submitted, you will have the value in params on the controller side as shown below:
|
31
|
+
|
32
|
+
params[:event][:"time(i)"]
|
33
|
+
|
34
|
+
Simply add these lines of code to your handler to play nicely with ActiveRecord:
|
35
|
+
|
36
|
+
params[:event][:time] = Time.parse(params[:event][:"time(i)"])
|
37
|
+
params[:event].delete(:"time(i)")
|
38
|
+
|
39
|
+
Now the time will be correct, but the date will be the current date. Here are a couple options for changing the date:
|
40
|
+
|
41
|
+
1) Change the date in your controller (replace anything in < > with your own variables):
|
42
|
+
|
43
|
+
params[:event][:time] = params[:event][:time].change(:year => <year_var>, :day => <day_var>, :month => <month_var>)
|
44
|
+
*I do not use this method, you may need to cast params[:event][:time] to a Time object before applying "change" to it.
|
45
|
+
|
46
|
+
2) Change the date in an ActiveRecord callback in your model. This is my preferred method:
|
47
|
+
|
48
|
+
def before_validation
|
49
|
+
# Assuming date is your Date variable
|
50
|
+
self.time = self.time.change(:year => date.year, :day => date.day, :month => date.month)
|
51
|
+
end
|
52
|
+
|
53
|
+
Feel free to send any questions to tonyamoyal@gmail.com
|
54
|
+
|
55
|
+
Author: Tony Amoyal <tonyamoyal@gmail.com>
|
56
|
+
|
57
|
+
Modifications by:
|
58
|
+
|
59
|
+
- Rich Sturim <rich@sturim.org>
|
60
|
+
- Nathan Hyde <nhyde@bigdrift.com>
|
61
|
+
|
62
|
+
|
63
|
+
Homepage:
|
64
|
+
http://github.com/tamoyal/simple_time_select/tree/master
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the simple_time_select plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the simple_time_select plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Simple Time Select'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'jeweler'
|
26
|
+
Jeweler::Tasks.new do |gemspec|
|
27
|
+
gemspec.name = "nbrew-simple_time_select"
|
28
|
+
gemspec.summary = "Twelve hour time select from a single input for hour, minute and second. Does not modify month, day and year inputs."
|
29
|
+
gemspec.description = "A time select component using only ONE select field."
|
30
|
+
gemspec.email = "tonyamoyal@gmail.com"
|
31
|
+
gemspec.homepage = "http://github.com/nbrew/simple_time_select"
|
32
|
+
gemspec.authors = ["Anthony Amoyal"]
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
36
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/about.yml
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "simple_time_select"
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module ActionView::Helpers
|
2
|
+
class DateTimeSelector
|
3
|
+
def select_minute_with_simple_time_select
|
4
|
+
return select_minute_without_simple_time_select unless @options[:simple_time_select].eql? true
|
5
|
+
|
6
|
+
# Although this is a datetime select, we only care about the time. Assume that the date will
|
7
|
+
# be set by some other control, and the date represented here will be overriden
|
8
|
+
|
9
|
+
val_minutes = @datetime.kind_of?(Time) ? @datetime.min + @datetime.hour*60 : @datetime
|
10
|
+
|
11
|
+
@options[:time_separator] = ""
|
12
|
+
|
13
|
+
# Default is 15 minute intervals
|
14
|
+
minute_interval = 15
|
15
|
+
if @options[:minute_interval]
|
16
|
+
minute_interval = @options[:minute_interval]
|
17
|
+
end
|
18
|
+
|
19
|
+
start_minute = 0
|
20
|
+
# @options[:start_hour] should be specified in military
|
21
|
+
# i.e. 0-23
|
22
|
+
if @options[:start_hour]
|
23
|
+
start_minute = @options[:start_hour] * 60
|
24
|
+
end
|
25
|
+
|
26
|
+
end_minute = 1439
|
27
|
+
# @options[:end_hour] should be specified in military
|
28
|
+
# i.e. 0-23
|
29
|
+
if @options[:end_hour]
|
30
|
+
end_minute = ( @options[:end_hour] + 1 ) * 60 - 1
|
31
|
+
end
|
32
|
+
|
33
|
+
if @options[:use_hidden] || @options[:discard_minute]
|
34
|
+
build_hidden(:minute, val)
|
35
|
+
else
|
36
|
+
minute_options = []
|
37
|
+
start_minute.upto(end_minute) do |minute|
|
38
|
+
if minute%minute_interval == 0
|
39
|
+
ampm = minute < 720 ? ' AM' : ' PM'
|
40
|
+
hour = minute/60
|
41
|
+
minute_padded = zero_pad_num(minute%60)
|
42
|
+
hour_padded = zero_pad_num(hour)
|
43
|
+
ampm_hour = ampm_hour(hour)
|
44
|
+
|
45
|
+
val = "#{hour_padded}:#{minute_padded}:00"
|
46
|
+
minute_options << ((val_minutes == minute) ?
|
47
|
+
%(<option value="#{val}" selected="selected">#{ampm_hour}:#{minute_padded}#{ampm}</option>\n) :
|
48
|
+
%(<option value="#{val}">#{ampm_hour}:#{minute_padded}#{ampm}</option>\n)
|
49
|
+
)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
build_select(:minute, minute_options)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
alias_method_chain :select_minute, :simple_time_select
|
56
|
+
|
57
|
+
|
58
|
+
def select_hour_with_simple_time_select
|
59
|
+
return select_hour_without_simple_time_select unless @options[:simple_time_select].eql? true
|
60
|
+
# Don't build the hour select
|
61
|
+
#build_hidden(:hour, val)
|
62
|
+
end
|
63
|
+
alias_method_chain :select_hour, :simple_time_select
|
64
|
+
|
65
|
+
def select_second_with_simple_time_select
|
66
|
+
return select_second_without_simple_time_select unless @options[:simple_time_select].eql? true
|
67
|
+
# Don't build the seconds select
|
68
|
+
#build_hidden(:second, val)
|
69
|
+
end
|
70
|
+
alias_method_chain :select_second, :simple_time_select
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def ampm_hour(hour)
|
76
|
+
return hour == 12 ? 12 : (hour == 0 ? 12 : (hour / 12 == 1 ? hour % 12 : hour))
|
77
|
+
end
|
78
|
+
|
79
|
+
def zero_pad_num(num)
|
80
|
+
return num < 10 ? '0' + num.to_s : num.to_s
|
81
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{nbrew-simple_time_select}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Anthony Amoyal"]
|
12
|
+
s.date = %q{2010-05-19}
|
13
|
+
s.description = %q{A time select component using only ONE select field.}
|
14
|
+
s.email = %q{tonyamoyal@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"CHANGELOG",
|
20
|
+
"MIT_LICENSE",
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"about.yml",
|
25
|
+
"init.rb",
|
26
|
+
"lib/simple_time_select.rb",
|
27
|
+
"nbrew-simple_time_select.gemspec",
|
28
|
+
"pkg/simple_time_select-0.0.0.gem",
|
29
|
+
"test/simple_time_select_test.rb",
|
30
|
+
"test/test_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/nbrew/simple_time_select}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
36
|
+
s.summary = %q{Twelve hour time select from a single input for hour, minute and second. Does not modify month, day and year inputs.}
|
37
|
+
s.test_files = [
|
38
|
+
"test/simple_time_select_test.rb",
|
39
|
+
"test/test_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
Binary file
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class String
|
4
|
+
def nstrip
|
5
|
+
self.gsub(/\n+/, '')
|
6
|
+
end
|
7
|
+
|
8
|
+
def nstrip!
|
9
|
+
self.gsub!(/\n+/, '')
|
10
|
+
end
|
11
|
+
|
12
|
+
def strip_selected
|
13
|
+
self.gsub(" selected=\"selected\"", '')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class SimpleTimeSelectTest < Test::Unit::TestCase
|
18
|
+
include ActionView::Helpers::TagHelper
|
19
|
+
include ActionView::Helpers::FormTagHelper
|
20
|
+
include ActionView::Helpers::FormOptionsHelper
|
21
|
+
include ActionView::Helpers::DateHelper
|
22
|
+
|
23
|
+
def test_ampm_hour
|
24
|
+
assert_equal(12, ampm_hour(0), "ampm_hour test 12 AM")
|
25
|
+
assert_equal(5, ampm_hour(5), "ampm_hour test 5 AM")
|
26
|
+
assert_equal(12, ampm_hour(12), "ampm_hour test 12 PM")
|
27
|
+
assert_equal(5, ampm_hour(17), "ampm_hour test 5 PM")
|
28
|
+
assert_equal(11, ampm_hour(23), "ampm_hour test 11 PM")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_zero_pad_num
|
32
|
+
assert_equal('00', zero_pad_num(0))
|
33
|
+
assert_equal('01', zero_pad_num(1))
|
34
|
+
assert_equal('10', zero_pad_num(10))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_simple_time_select_default
|
38
|
+
# Default time is 9:00pm
|
39
|
+
time = Time.now.change(:hour => 21)
|
40
|
+
|
41
|
+
options = []
|
42
|
+
0.upto(23) do |hour|
|
43
|
+
meridiem = hour < 12 ? 'AM' : 'PM'
|
44
|
+
0.upto(3) do |minute|
|
45
|
+
options.push [ ampm_hour(hour).to_s+":"+zero_pad_num(minute*15)+" "+meridiem, zero_pad_num(hour)+":"+zero_pad_num(minute*15)+":00" ]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
expected = select_tag 'date[minute]', options_for_select(options, "21:00:00"), :id => 'date_minute'
|
49
|
+
actual = select_minute time, :simple_time_select => true
|
50
|
+
assert_equal(expected.nstrip, actual.nstrip, "Simple Time Select generation")
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_usual_time_select
|
54
|
+
# Default time is 9:00pm
|
55
|
+
time = Time.now.change(:hour => 21)
|
56
|
+
|
57
|
+
options = options_for_select((0..59).to_a.map { |m| "%02d" % m }, "00")
|
58
|
+
expected = select_tag 'date[minute]', options, :id => 'date_minute'
|
59
|
+
expected.nstrip!
|
60
|
+
|
61
|
+
# The select_tag helper puts selected="selected" after the value in the options tag
|
62
|
+
# whereas the select_minute helper puts selected="selected" before the value in the options tag.
|
63
|
+
# Therefore the html can be correct, but this test will not validate. To fix this, I am just removing
|
64
|
+
# selected="selected" from both before I compare
|
65
|
+
expected = expected.strip_selected
|
66
|
+
|
67
|
+
actual = select_minute time, :simple_time_select => false
|
68
|
+
actual = actual.strip_selected
|
69
|
+
assert_equal(expected, actual.nstrip, "Usual time select with explicit option")
|
70
|
+
|
71
|
+
actual = select_minute time
|
72
|
+
actual = actual.strip_selected
|
73
|
+
assert_equal(expected, actual.nstrip, "Usual time select with no options")
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_start_and_end_hour
|
77
|
+
time = Time.now.change(:hour => 21)
|
78
|
+
# 10 AM to 2 PM
|
79
|
+
output = select_minute time, :simple_time_select => true, :start_hour => 10, :end_hour => 14
|
80
|
+
puts "#{output.inspect}"
|
81
|
+
end
|
82
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nbrew-simple_time_select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Anthony Amoyal
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-05-19 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A time select component using only ONE select field.
|
23
|
+
email: tonyamoyal@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
30
|
+
files:
|
31
|
+
- CHANGELOG
|
32
|
+
- MIT_LICENSE
|
33
|
+
- README
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- about.yml
|
37
|
+
- init.rb
|
38
|
+
- lib/simple_time_select.rb
|
39
|
+
- nbrew-simple_time_select.gemspec
|
40
|
+
- pkg/simple_time_select-0.0.0.gem
|
41
|
+
- test/simple_time_select_test.rb
|
42
|
+
- test/test_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/nbrew/simple_time_select
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Twelve hour time select from a single input for hour, minute and second. Does not modify month, day and year inputs.
|
77
|
+
test_files:
|
78
|
+
- test/simple_time_select_test.rb
|
79
|
+
- test/test_helper.rb
|