date_and_time_input 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +5 -0
- data/README.md +25 -0
- data/Rakefile +10 -0
- data/date_and_time_input.gemspec +24 -0
- data/lib/action_view/helpers/date_and_time_helper.rb +73 -0
- data/lib/active_record/datetime_multiparameter_attribute.rb +29 -0
- data/lib/date_and_time_input.rb +10 -0
- data/test/action_view/helpers/date_and_time_helper_test.rb +13 -0
- data/test/test_helper.rb +4 -0
- metadata +81 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# DateAndTimeInput
|
2
|
+
|
3
|
+
DateAndTimeInput is a Rails extension, providing a form input for DateTime attributes
|
4
|
+
arranged as two input fields, for date and time, respectively.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
$ gem install date_and_time_input
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
To use DateAndTimeInput in your view templates,
|
13
|
+
you need to specify the input presentation as follows:
|
14
|
+
|
15
|
+
date_and_time_field :letter, :sent_at
|
16
|
+
|
17
|
+
or, as a `form_for` field:
|
18
|
+
|
19
|
+
f.date_and_time :sent_at
|
20
|
+
|
21
|
+
and that's it.
|
22
|
+
|
23
|
+
## Authors
|
24
|
+
|
25
|
+
- [Slava Kravchenko](https://github.com/cordawyn)
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = 'date_and_time_input'
|
3
|
+
gem.authors = ['Slava Kravchenko']
|
4
|
+
gem.email = ['slava.kravchenko@gmail.com']
|
5
|
+
gem.version = ("$Release: 0.0.1 $" =~ /[\.\d]+/) && $&
|
6
|
+
gem.platform = Gem::Platform::RUBY
|
7
|
+
gem.homepage = "https://github.com/cordawyn/date_and_time_input"
|
8
|
+
gem.summary = "Rails extension for Datetime 2-field inputs"
|
9
|
+
gem.description = <<HERE
|
10
|
+
DateAndTimeInput is a Rails extension, providing a form input for DateTime attributes
|
11
|
+
arranged as two input fields, for date and time, respectively.
|
12
|
+
HERE
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($\) - ["Gemfile.lock"]
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_dependency "actionpack", "~> 3.2.0"
|
20
|
+
|
21
|
+
gem.license = "The MIT License (MIT)"
|
22
|
+
gem.extra_rdoc_files = ['README.md']
|
23
|
+
gem.has_rdoc = false
|
24
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
module DateAndTimeHelper
|
4
|
+
DEFAULT_PREFIX = 'object'.freeze
|
5
|
+
POSITION = { :date => 1, :time => 2 }.freeze
|
6
|
+
|
7
|
+
def date_and_time_field(object_name, method, options = {}, html_options = {})
|
8
|
+
InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_and_time_field_tag(options, html_options).html_safe
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class InstanceTag
|
13
|
+
def to_date_and_time_field_tag(options = {}, html_options = {})
|
14
|
+
options = options.stringify_keys
|
15
|
+
html_options = html_options.stringify_keys
|
16
|
+
|
17
|
+
options["include_position"] = true unless options.has_key?("include_position")
|
18
|
+
options["prefix"] ||= @object_name
|
19
|
+
|
20
|
+
html_options["value"] = html_options.fetch("value"){ value_before_type_cast(object) }
|
21
|
+
html_options["value"] &&= ERB::Util.html_escape(html_options["value"])
|
22
|
+
{ date: value_to_date(html_options["value"]),
|
23
|
+
time: value_to_time(html_options["value"])
|
24
|
+
}.inject("") do |output, (field_type, value)|
|
25
|
+
html_options.merge!("id" => input_id_from_type(field_type, options),
|
26
|
+
"name" => input_name_from_type(field_type, options),
|
27
|
+
"type" => field_type,
|
28
|
+
"value" => value)
|
29
|
+
output << tag("input", html_options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def value_to_date(value)
|
36
|
+
value && value.to_date.to_s
|
37
|
+
end
|
38
|
+
|
39
|
+
def value_to_time(value)
|
40
|
+
value && value.strftime("%H:%M")
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the name attribute for the input tag.
|
44
|
+
# => post[written_on(1)]
|
45
|
+
def input_name_from_type(type, options = {})
|
46
|
+
prefix = options["prefix"] || ActionView::Helpers::DateAndTimeHelper::DEFAULT_PREFIX
|
47
|
+
prefix += "[#{options['index']}]" if options.has_key?("index")
|
48
|
+
|
49
|
+
field_name = method_name.dup
|
50
|
+
if options["include_position"]
|
51
|
+
field_name += "(#{ActionView::Helpers::DateAndTimeHelper::POSITION[type]})"
|
52
|
+
end
|
53
|
+
|
54
|
+
options["discard_type"] ? prefix : "#{prefix}[#{field_name}]"
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns the id attribute for the input tag.
|
58
|
+
# => "post_written_on_1"
|
59
|
+
def input_id_from_type(type, options = {})
|
60
|
+
id = input_name_from_type(type, options).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
|
61
|
+
id = options["namespace"] + '_' + id if options["namespace"]
|
62
|
+
|
63
|
+
id
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class FormBuilder
|
68
|
+
def date_and_time(method, options = {})
|
69
|
+
@template.date_and_time_field(@object_name, method, options)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
# ActiveRecord (temporal) extension to handle 2-field datetime attribute assignment
|
3
|
+
module DatetimeMultiparameterAttribute
|
4
|
+
private
|
5
|
+
|
6
|
+
def read_value_from_parameter(name, values_hash_from_param)
|
7
|
+
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
8
|
+
if (klass == Time || klass == Datetime) && values_hash_from_param.size == 2
|
9
|
+
t = Time.zone.parse("#{values_hash_from_param[1]} #{values_hash_from_param[2]}")
|
10
|
+
if t
|
11
|
+
values = {
|
12
|
+
1 => t.year,
|
13
|
+
2 => t.month,
|
14
|
+
3 => t.day,
|
15
|
+
4 => t.hour,
|
16
|
+
5 => t.min,
|
17
|
+
6 => t.sec,
|
18
|
+
7 => t.usec
|
19
|
+
}
|
20
|
+
read_other_parameter_value(klass, name, values)
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
if defined?(::ActionView)
|
2
|
+
require "action_view/helpers/date_and_time_helper"
|
3
|
+
ActionView::Helpers.send :autoload, :DateAndTimeHelper
|
4
|
+
ActionView::Helpers.send :include, ActionView::Helpers::DateAndTimeHelper
|
5
|
+
end
|
6
|
+
|
7
|
+
if defined?(::ActiveRecord)
|
8
|
+
require "active_record/datetime_multiparameter_attribute"
|
9
|
+
ActiveRecord::Base.send :include, ActiveRecord::DatetimeMultiparameterAttribute
|
10
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe ActionView::Helpers::DateAndTimeHelper do
|
4
|
+
include ActionView::Helpers::DateAndTimeHelper
|
5
|
+
|
6
|
+
describe "date_and_time_field" do
|
7
|
+
subject { date_and_time_field(:object, :created_at) }
|
8
|
+
|
9
|
+
it "must produce proper markup" do
|
10
|
+
subject.must_equal "<input id=\"object_created_at_1\" name=\"object[created_at(1)]\" type=\"date\" /><input id=\"object_created_at_2\" name=\"object[created_at(2)]\" type=\"time\" />"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_and_time_input
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Slava Kravchenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
none: false
|
21
|
+
name: actionpack
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.2.0
|
29
|
+
none: false
|
30
|
+
description: ! 'DateAndTimeInput is a Rails extension, providing a form input for
|
31
|
+
DateTime attributes
|
32
|
+
|
33
|
+
arranged as two input fields, for date and time, respectively.
|
34
|
+
|
35
|
+
'
|
36
|
+
email:
|
37
|
+
- slava.kravchenko@gmail.com
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README.md
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- date_and_time_input.gemspec
|
48
|
+
- lib/action_view/helpers/date_and_time_helper.rb
|
49
|
+
- lib/active_record/datetime_multiparameter_attribute.rb
|
50
|
+
- lib/date_and_time_input.rb
|
51
|
+
- test/action_view/helpers/date_and_time_helper_test.rb
|
52
|
+
- test/test_helper.rb
|
53
|
+
homepage: https://github.com/cordawyn/date_and_time_input
|
54
|
+
licenses:
|
55
|
+
- The MIT License (MIT)
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
none: false
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
none: false
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.24
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Rails extension for Datetime 2-field inputs
|
78
|
+
test_files:
|
79
|
+
- test/action_view/helpers/date_and_time_helper_test.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
has_rdoc: false
|