time_of_day_attr 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
|
|
1
|
+
module TimeOfDayAttr
|
2
|
+
module ActiveRecordExt
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def time_of_day_attr *attrs
|
8
|
+
options = attrs.extract_options!
|
9
|
+
options[:formats] ||= [:default, :hour]
|
10
|
+
attrs.each do |attr|
|
11
|
+
define_method("#{attr}=") do |value|
|
12
|
+
if value.is_a?(String)
|
13
|
+
delocalized_values = options[:formats].map { |format| TimeOfDayAttr.delocalize(value, format: format) rescue nil }
|
14
|
+
value = delocalized_values.compact.first || send(attr)
|
15
|
+
end
|
16
|
+
super(value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
ActiveRecord::Base.send(:include, TimeOfDayAttr::ActiveRecordExt)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module TimeOfDayAttr
|
2
|
+
module FormBuilderExt
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def time_of_day_field(method, options = {})
|
6
|
+
unless options[:value]
|
7
|
+
value = object.send(method)
|
8
|
+
options[:value] = TimeOfDayAttr.localize(value, options.extract!(:format, :omit_minutes_at_full_hour))
|
9
|
+
end
|
10
|
+
text_field(method, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
ActionView::Helpers::FormBuilder.send(:include, TimeOfDayAttr::FormBuilderExt)
|
data/lib/time_of_day_attr.rb
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
require 'time_of_day_attr/active_record_ext'
|
2
|
+
require 'action_view'
|
3
|
+
require 'time_of_day_attr/form_builder_ext'
|
4
|
+
|
5
|
+
files = Dir[File.join(File.dirname(__FILE__), '../config/locales/*.yml')]
|
6
|
+
I18n.load_path.concat(files)
|
7
|
+
|
1
8
|
module TimeOfDayAttr
|
2
9
|
|
3
10
|
class << self
|
@@ -29,25 +36,4 @@ module TimeOfDayAttr
|
|
29
36
|
|
30
37
|
end
|
31
38
|
|
32
|
-
ActiveRecord::Base.class_eval do
|
33
|
-
|
34
|
-
def self.time_of_day_attr *attrs
|
35
|
-
options = attrs.extract_options!
|
36
|
-
options[:formats] ||= [:default, :hour]
|
37
|
-
attrs.each do |attr|
|
38
|
-
define_method("#{attr}=") do |value|
|
39
|
-
if value.is_a?(String)
|
40
|
-
delocalized_values = options[:formats].map { |format| TimeOfDayAttr.delocalize(value, format: format) rescue nil }
|
41
|
-
value = delocalized_values.compact.first || send(attr)
|
42
|
-
end
|
43
|
-
super(value)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
39
|
end
|
51
|
-
|
52
|
-
files = Dir[File.join(File.dirname(__FILE__), '../config/locales/*.yml')]
|
53
|
-
I18n.load_path.concat(files)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_of_day_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -56,6 +56,8 @@ files:
|
|
56
56
|
- config/locales/time_of_day.en.yml
|
57
57
|
- config/locales/time_of_day.de.yml
|
58
58
|
- lib/time_of_day_attr.rb
|
59
|
+
- lib/time_of_day_attr/active_record_ext.rb
|
60
|
+
- lib/time_of_day_attr/form_builder_ext.rb
|
59
61
|
- MIT-LICENSE
|
60
62
|
- Rakefile
|
61
63
|
- README.rdoc
|