custom_fields 2.2.3 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/config/locales/de.yml +1 -1
- data/lib/custom_fields.rb +1 -0
- data/lib/custom_fields/field.rb +2 -2
- data/lib/custom_fields/target.rb +1 -1
- data/lib/custom_fields/target_helpers.rb +8 -8
- data/lib/custom_fields/types/date.rb +6 -2
- data/lib/custom_fields/types/date_time.rb +97 -0
- data/lib/custom_fields/version.rb +1 -1
- metadata +4 -3
data/config/locales/de.yml
CHANGED
data/lib/custom_fields.rb
CHANGED
data/lib/custom_fields/field.rb
CHANGED
@@ -5,8 +5,8 @@ module CustomFields
|
|
5
5
|
include ::Mongoid::Document
|
6
6
|
include ::Mongoid::Timestamps
|
7
7
|
|
8
|
-
AVAILABLE_TYPES = %w(default string text email date boolean file select float integer
|
9
|
-
tags relationship_default belongs_to has_many many_to_many)
|
8
|
+
AVAILABLE_TYPES = %w(default string text email date date_time boolean file select float integer
|
9
|
+
money tags relationship_default belongs_to has_many many_to_many)
|
10
10
|
|
11
11
|
## types ##
|
12
12
|
AVAILABLE_TYPES.each do |type|
|
data/lib/custom_fields/target.rb
CHANGED
@@ -7,7 +7,7 @@ module CustomFields
|
|
7
7
|
included do
|
8
8
|
|
9
9
|
## types ##
|
10
|
-
%w(default string text email date boolean file select float integer money
|
10
|
+
%w(default string text email date date_time boolean file select float integer money
|
11
11
|
belongs_to has_many many_to_many tags).each do |type|
|
12
12
|
include "CustomFields::Types::#{type.camelize}::Target".constantize
|
13
13
|
end
|
@@ -36,10 +36,10 @@ module CustomFields
|
|
36
36
|
def custom_fields_safe_setters
|
37
37
|
self.custom_fields_recipe['rules'].map do |rule|
|
38
38
|
case rule['type'].to_sym
|
39
|
-
when :date, :money
|
40
|
-
when :file
|
41
|
-
when :select, :belongs_to
|
42
|
-
when :has_many, :many_to_many
|
39
|
+
when :date, :date_time, :money then "formatted_#{rule['name']}"
|
40
|
+
when :file then [rule['name'], "remove_#{rule['name']}"]
|
41
|
+
when :select, :belongs_to then ["#{rule['name']}_id", "position_in_#{rule['name']}"]
|
42
|
+
when :has_many, :many_to_many then nil
|
43
43
|
else
|
44
44
|
rule['name']
|
45
45
|
end
|
@@ -164,10 +164,10 @@ module CustomFields
|
|
164
164
|
#
|
165
165
|
def custom_fields_getters_for(name, type)
|
166
166
|
case type.to_sym
|
167
|
-
when :select
|
168
|
-
when :date, :money
|
169
|
-
when :file
|
170
|
-
when :belongs_to
|
167
|
+
when :select then [name, "#{name}_id"]
|
168
|
+
when :date, :date_time, :money then "formatted_#{name}"
|
169
|
+
when :file then "#{name}_url"
|
170
|
+
when :belongs_to then "#{name}_id"
|
171
171
|
else
|
172
172
|
name
|
173
173
|
end
|
@@ -68,7 +68,7 @@ module CustomFields
|
|
68
68
|
|
69
69
|
def _set_formatted_date(name, value)
|
70
70
|
if value.is_a?(::String) && !value.blank?
|
71
|
-
date = ::Date._strptime(value,
|
71
|
+
date = ::Date._strptime(value, self._formatted_date_format)
|
72
72
|
|
73
73
|
if date
|
74
74
|
value = ::Date.new(date[:year], date[:mon], date[:mday])
|
@@ -81,7 +81,11 @@ module CustomFields
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def _get_formatted_date(name)
|
84
|
-
self.send(name.to_sym).strftime(
|
84
|
+
self.send(name.to_sym).strftime(self._formatted_date_format) rescue nil
|
85
|
+
end
|
86
|
+
|
87
|
+
def _formatted_date_format
|
88
|
+
I18n.t('date.formats.default')
|
85
89
|
end
|
86
90
|
|
87
91
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module CustomFields
|
2
|
+
|
3
|
+
module Types
|
4
|
+
|
5
|
+
module DateTime
|
6
|
+
|
7
|
+
module Field; end
|
8
|
+
|
9
|
+
module Target
|
10
|
+
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
# Adds a date_time field
|
16
|
+
#
|
17
|
+
# @param [ Class ] klass The class to modify
|
18
|
+
# @param [ Hash ] rule It contains the name of the field and if it is required or not
|
19
|
+
#
|
20
|
+
def apply_date_time_custom_field(klass, rule)
|
21
|
+
name = rule['name']
|
22
|
+
|
23
|
+
klass.field name, type: ::DateTime, localize: rule['localized'] || false
|
24
|
+
|
25
|
+
# other methods
|
26
|
+
klass.send(:define_method, :"formatted_#{name}") { _get_formatted_date_time(name) }
|
27
|
+
klass.send(:define_method, :"formatted_#{name}=") { |value| _set_formatted_date_time(name, value) }
|
28
|
+
|
29
|
+
if rule['required']
|
30
|
+
klass.validates_presence_of name, :"formatted_#{name}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Build a hash storing the formatted value for
|
35
|
+
# a date_time custom field of an instance.
|
36
|
+
#
|
37
|
+
# @param [ Object ] instance An instance of the class enhanced by the custom_fields
|
38
|
+
# @param [ String ] name The name of the date_time custom field
|
39
|
+
#
|
40
|
+
# @return [ Hash ] field name => formatted date_time
|
41
|
+
#
|
42
|
+
def date_time_attribute_get(instance, name)
|
43
|
+
if value = instance.send(:"formatted_#{name}")
|
44
|
+
{ name => value, "formatted_#{name}" => value }
|
45
|
+
else
|
46
|
+
{}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Set the value for the instance and the date_time field specified by
|
51
|
+
# the 2 params.
|
52
|
+
#
|
53
|
+
# @param [ Object ] instance An instance of the class enhanced by the custom_fields
|
54
|
+
# @param [ String ] name The name of the date_time custom field
|
55
|
+
# @param [ Hash ] attributes The attributes used to fetch the values
|
56
|
+
#
|
57
|
+
def date_time_attribute_set(instance, name, attributes)
|
58
|
+
return unless attributes.key?(name) || attributes.key?("formatted_#{name}")
|
59
|
+
|
60
|
+
value = attributes[name] || attributes["formatted_#{name}"]
|
61
|
+
|
62
|
+
instance.send(:"formatted_#{name}=", value)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
|
69
|
+
def _set_formatted_date_time(name, value)
|
70
|
+
if value.is_a?(::String) && !value.blank?
|
71
|
+
date_time = ::DateTime._strptime(value, self._formatted_date_time_format)
|
72
|
+
|
73
|
+
if date_time
|
74
|
+
value = ::Time.zone.local(date_time[:year], date_time[:mon], date_time[:mday], date_time[:hour], date_time[:min], date_time[:sec] || 0)#, date_time[:zone] || "")
|
75
|
+
else
|
76
|
+
value = ::Time.zone.parse(value) rescue nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
self.send(:"#{name}=", value)
|
81
|
+
end
|
82
|
+
|
83
|
+
def _get_formatted_date_time(name)
|
84
|
+
self.send(name.to_sym).strftime(self._formatted_date_time_format) rescue nil
|
85
|
+
end
|
86
|
+
|
87
|
+
def _formatted_date_time_format
|
88
|
+
I18n.t('time.formats.default')
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
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: 2013-
|
12
|
+
date: 2013-08-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -218,6 +218,7 @@ files:
|
|
218
218
|
- lib/custom_fields/types/belongs_to.rb
|
219
219
|
- lib/custom_fields/types/boolean.rb
|
220
220
|
- lib/custom_fields/types/date.rb
|
221
|
+
- lib/custom_fields/types/date_time.rb
|
221
222
|
- lib/custom_fields/types/default.rb
|
222
223
|
- lib/custom_fields/types/email.rb
|
223
224
|
- lib/custom_fields/types/file.rb
|
@@ -252,7 +253,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
252
253
|
version: '0'
|
253
254
|
segments:
|
254
255
|
- 0
|
255
|
-
hash:
|
256
|
+
hash: 4124785133345041024
|
256
257
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
257
258
|
none: false
|
258
259
|
requirements:
|