custom_fields 2.0.0.rc1 → 2.0.0.rc2
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/lib/custom_fields/extensions/mongoid/fields/i18n.rb +1 -1
- data/lib/custom_fields/extensions/mongoid/fields/internal/localized.rb +4 -2
- data/lib/custom_fields/extensions/mongoid/fields.rb +2 -1
- data/lib/custom_fields/source.rb +152 -156
- data/lib/custom_fields/target.rb +1 -5
- data/lib/custom_fields/types/date.rb +9 -13
- data/lib/custom_fields/types/select.rb +26 -34
- data/lib/custom_fields/version.rb +3 -1
- metadata +26 -37
- data/lib/custom_fields/types_old/boolean.rb +0 -13
- data/lib/custom_fields/types_old/category.rb +0 -107
- data/lib/custom_fields/types_old/date.rb +0 -49
- data/lib/custom_fields/types_old/default.rb +0 -44
- data/lib/custom_fields/types_old/file.rb +0 -27
- data/lib/custom_fields/types_old/has_many/proxy_collection.rb +0 -103
- data/lib/custom_fields/types_old/has_many/reverse_lookup_proxy_collection.rb +0 -101
- data/lib/custom_fields/types_old/has_many.rb +0 -100
- data/lib/custom_fields/types_old/has_one.rb +0 -60
- data/lib/custom_fields/types_old/string.rb +0 -13
- data/lib/custom_fields/types_old/text.rb +0 -15
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
module CustomFields
|
|
2
|
-
module Types
|
|
3
|
-
module HasMany
|
|
4
|
-
|
|
5
|
-
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
included do
|
|
8
|
-
field :target
|
|
9
|
-
field :reverse_lookup
|
|
10
|
-
|
|
11
|
-
validates_presence_of :target, :if => :has_many?
|
|
12
|
-
|
|
13
|
-
register_type :has_many, Array
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
module InstanceMethods
|
|
17
|
-
|
|
18
|
-
def target_klass
|
|
19
|
-
self.target.constantize rescue nil
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def reverse_has_many?
|
|
23
|
-
self.reverse_lookup && !self.reverse_lookup.strip.blank?
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def safe_reverse_lookup
|
|
27
|
-
if self.reverse_lookup =~ /^custom_field_[0-9]+$/
|
|
28
|
-
self.reverse_lookup
|
|
29
|
-
else
|
|
30
|
-
self.target_klass.custom_field_alias_to_name(self.reverse_lookup)
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def reverse_lookup_alias
|
|
35
|
-
self.target_klass.custom_field_name_to_alias(self.reverse_lookup)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def apply_has_many_type(klass)
|
|
39
|
-
klass.class_eval <<-EOF
|
|
40
|
-
|
|
41
|
-
before_validation :store_#{self.safe_alias.singularize}_ids
|
|
42
|
-
|
|
43
|
-
after_save :persist_#{self.safe_alias}
|
|
44
|
-
|
|
45
|
-
def #{self.safe_alias}=(ids_or_objects)
|
|
46
|
-
self.#{self.safe_alias}.update(ids_or_objects)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def #{self.safe_alias}
|
|
50
|
-
@_#{self._name} ||= build_#{self.safe_alias.singularize}_proxy_collection
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def #{self.safe_alias}_klass
|
|
54
|
-
'#{self.target.to_s}'.constantize rescue nil
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def #{self.safe_alias.singularize}_ids
|
|
58
|
-
self.#{self.safe_alias}.ids
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def store_#{self.safe_alias.singularize}_ids
|
|
62
|
-
self.#{self.safe_alias}.store_values
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def persist_#{self.safe_alias}
|
|
66
|
-
self.#{self.safe_alias}.persist
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
EOF
|
|
70
|
-
|
|
71
|
-
if reverse_has_many?
|
|
72
|
-
klass.class_eval <<-EOF
|
|
73
|
-
def build_#{self.safe_alias.singularize}_proxy_collection
|
|
74
|
-
::CustomFields::Types::HasMany::ReverseLookupProxyCollection.new(self, self.#{self.safe_alias}_klass, '#{self._name}', {
|
|
75
|
-
:reverse_lookup_field => '#{self.safe_reverse_lookup}'
|
|
76
|
-
})
|
|
77
|
-
end
|
|
78
|
-
EOF
|
|
79
|
-
else
|
|
80
|
-
klass.class_eval <<-EOF
|
|
81
|
-
def build_#{self.safe_alias.singularize}_proxy_collection
|
|
82
|
-
::CustomFields::Types::HasMany::ProxyCollection.new(self, self.#{self.safe_alias}_klass, '#{self._name}').tap do |collection|
|
|
83
|
-
collection.reload.update(self.#{self._name})
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
EOF
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def add_has_many_validation(klass)
|
|
91
|
-
if self.required?
|
|
92
|
-
klass.validates_length_of self.safe_alias.to_sym, :minimum => 1, :too_short => :blank
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
end
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
module CustomFields
|
|
2
|
-
module Types
|
|
3
|
-
module HasOne
|
|
4
|
-
|
|
5
|
-
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
included do
|
|
8
|
-
field :target
|
|
9
|
-
|
|
10
|
-
validates_presence_of :target, :if => :has_one?
|
|
11
|
-
|
|
12
|
-
register_type :has_one, BSON::ObjectId
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
module InstanceMethods
|
|
16
|
-
|
|
17
|
-
def apply_has_one_type(klass)
|
|
18
|
-
|
|
19
|
-
klass.field :"#{self._name}_position", :type => Integer, :default => 0 # needed by the has_many reverse
|
|
20
|
-
|
|
21
|
-
klass.class_eval <<-EOF
|
|
22
|
-
|
|
23
|
-
def #{self.safe_alias}=(id_or_object)
|
|
24
|
-
if id_or_object.respond_to?(:_id)
|
|
25
|
-
target_id = id_or_object._id
|
|
26
|
-
@_#{self._name} = id_or_object
|
|
27
|
-
else
|
|
28
|
-
target_id = id_or_object
|
|
29
|
-
@_#{self._name} = nil # empty previous cached value
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
write_attribute(:#{self._name}, target_id)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def #{self.safe_alias}
|
|
36
|
-
return @_#{self._name} unless @_#{self._name}.blank? # memoization
|
|
37
|
-
|
|
38
|
-
target_id = self.send(:#{self._name})
|
|
39
|
-
|
|
40
|
-
return nil if target_id.blank?
|
|
41
|
-
|
|
42
|
-
target_klass = '#{self.target.to_s}'.constantize
|
|
43
|
-
|
|
44
|
-
if target_klass.embedded?
|
|
45
|
-
@_#{self._name} = target_klass._parent.reload.send(target_klass.association_name).find(target_id)
|
|
46
|
-
else
|
|
47
|
-
@_#{self._name} = target_klass.find(target_id)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
@_#{self._name}
|
|
51
|
-
rescue # target_klass does not exist anymore or the target element has been removed since
|
|
52
|
-
nil
|
|
53
|
-
end
|
|
54
|
-
EOF
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|