custom_fields 1.0.0.beta.14 → 1.0.0.beta.15
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/README.textile +1 -1
- data/lib/custom_fields/custom_fields_for.rb +57 -13
- data/lib/custom_fields/extensions/mongoid/relations/accessors.rb +2 -19
- data/lib/custom_fields/extensions/mongoid/relations/builders.rb +27 -0
- data/lib/custom_fields/version.rb +1 -1
- data/lib/custom_fields.rb +5 -4
- metadata +15 -16
data/README.textile
CHANGED
@@ -2,8 +2,31 @@ module CustomFields
|
|
2
2
|
|
3
3
|
module CustomFieldsFor
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
cattr_accessor :custom_fields
|
9
|
+
|
10
|
+
self.custom_fields = []
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
|
15
|
+
def custom_fields?(collection_name)
|
16
|
+
self.class.custom_fields?(collection_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def clone_metadata_for_custom_fields(metadata)
|
20
|
+
singular_name = metadata.name.to_s.singularize.gsub(/^_/, '')
|
21
|
+
|
22
|
+
klass = self.send(:"fetch_#{singular_name}_klass")
|
23
|
+
|
24
|
+
# safer to do that because we are going to modify the metadata klass for next operations
|
25
|
+
metadata.clone.tap do |metadata|
|
26
|
+
metadata.instance_variable_set(:@klass, klass)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
7
30
|
end
|
8
31
|
|
9
32
|
# Enhance an embedded collection OR the instance itself (by passing self) by providing methods to manage custom fields.
|
@@ -34,19 +57,15 @@ module CustomFields
|
|
34
57
|
#
|
35
58
|
module ClassMethods
|
36
59
|
|
37
|
-
def
|
38
|
-
|
60
|
+
def custom_fields?(collection_name)
|
61
|
+
self.custom_fields.include?(collection_name.to_s)
|
62
|
+
end
|
39
63
|
|
40
|
-
|
64
|
+
def custom_fields_for(collection_name)
|
65
|
+
singular_name = collection_name.to_s.singularize
|
41
66
|
dynamic_custom_field_class_name = "#{self.name}#{singular_name.camelize}Field"
|
42
67
|
|
43
|
-
|
44
|
-
(klass = Class.new(::CustomFields::Field)).class_eval <<-EOF
|
45
|
-
embedded_in :#{self.name.underscore}, :inverse_of => :#{singular_name}_custom_fields
|
46
|
-
EOF
|
47
|
-
|
48
|
-
Object.const_set(dynamic_custom_field_class_name, klass)
|
49
|
-
end
|
68
|
+
self.declare_embedded_in_definition_in_custom_field(collection_name)
|
50
69
|
|
51
70
|
# enhance the class itself
|
52
71
|
if (itself = %w(itself self).include?(collection_name.to_s))
|
@@ -58,10 +77,12 @@ module CustomFields
|
|
58
77
|
def safe_#{singular_name}
|
59
78
|
self.#{collection_name} || self.build_#{collection_name}
|
60
79
|
end
|
61
|
-
|
62
80
|
EOV
|
63
81
|
end
|
64
82
|
|
83
|
+
# record the collection_name
|
84
|
+
self.custom_fields << collection_name.to_s
|
85
|
+
|
65
86
|
# common part
|
66
87
|
class_eval <<-EOV
|
67
88
|
field :#{singular_name}_custom_fields_counter, :type => Integer, :default => 0
|
@@ -141,6 +162,29 @@ module CustomFields
|
|
141
162
|
|
142
163
|
end
|
143
164
|
|
165
|
+
protected
|
166
|
+
|
167
|
+
def dynamic_custom_field_class_name(collection_name)
|
168
|
+
"#{self.name}#{collection_name.to_s.singularize.camelize}Field"
|
169
|
+
end
|
170
|
+
|
171
|
+
# An embedded relationship has to be defined on both side in order for it
|
172
|
+
# to work properly. But because custom_field can be embedded in different
|
173
|
+
# models that it's not aware of, we have to declare manually the definition
|
174
|
+
# once we know the target class.
|
175
|
+
def declare_embedded_in_definition_in_custom_field(target_collection_name)
|
176
|
+
singular_name = target_collection_name.to_s.singularize
|
177
|
+
klass_name = self.dynamic_custom_field_class_name(target_collection_name)
|
178
|
+
|
179
|
+
unless Object.const_defined?(klass_name)
|
180
|
+
(klass = Class.new(::CustomFields::Field)).class_eval <<-EOF
|
181
|
+
embedded_in :#{self.name.underscore}, :inverse_of => :#{singular_name}_custom_fields
|
182
|
+
EOF
|
183
|
+
|
184
|
+
Object.const_set(klass_name, klass)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
144
188
|
end
|
145
189
|
|
146
190
|
end
|
@@ -16,31 +16,14 @@ module Mongoid # :nodoc:
|
|
16
16
|
#
|
17
17
|
# @since 2.0.0.rc.1
|
18
18
|
def create_relation_with_custom_fields(object, metadata)
|
19
|
-
|
20
|
-
|
21
|
-
if custom_fields?(self, association_name)
|
22
|
-
metadata = metadata.clone # 2 parent instances should not share the exact same option instance
|
23
|
-
|
24
|
-
custom_fields = self.send(:"ordered_#{custom_fields_association_name(association_name)}")
|
25
|
-
|
26
|
-
klass = metadata.klass.to_klass_with_custom_fields(custom_fields, self, association_name)
|
27
|
-
|
28
|
-
metadata.instance_variable_set(:@klass, klass)
|
19
|
+
if self.respond_to?(:custom_fields?) && self.custom_fields?(metadata.name)
|
20
|
+
metadata = self.clone_metadata_for_custom_fields(metadata)
|
29
21
|
end
|
30
22
|
|
31
23
|
create_relation_without_custom_fields(object, metadata)
|
32
24
|
end
|
33
25
|
|
34
26
|
alias_method_chain :create_relation, :custom_fields
|
35
|
-
|
36
|
-
def custom_fields_association_name(association_name)
|
37
|
-
"#{association_name.to_s.singularize}_custom_fields".to_sym
|
38
|
-
end
|
39
|
-
|
40
|
-
def custom_fields?(object, association_name)
|
41
|
-
object.respond_to?(custom_fields_association_name(association_name))
|
42
|
-
end
|
43
|
-
|
44
27
|
end
|
45
28
|
|
46
29
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongoid # :nodoc:
|
2
|
+
module Relations #:nodoc:
|
3
|
+
|
4
|
+
module Builders
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods #:nodoc:
|
8
|
+
|
9
|
+
def builder_with_custom_fields(name, metadata)
|
10
|
+
tap do
|
11
|
+
define_method("build_#{name}") do |*args|
|
12
|
+
if self.custom_fields?(metadata.name)
|
13
|
+
metadata = self.clone_metadata_for_custom_fields(metadata)
|
14
|
+
end
|
15
|
+
|
16
|
+
document = Factory.build(metadata.klass, args.first || {})
|
17
|
+
send("#{name}=", document, :binding => true)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method_chain :builder, :custom_fields
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/custom_fields.rb
CHANGED
@@ -4,24 +4,25 @@ require 'active_support'
|
|
4
4
|
require 'carrierwave/orm/mongoid'
|
5
5
|
|
6
6
|
module CustomFields
|
7
|
-
|
7
|
+
|
8
8
|
@@options = {
|
9
9
|
:reserved_aliases => Mongoid.destructive_fields
|
10
10
|
}
|
11
|
-
|
11
|
+
|
12
12
|
def self.options=(options)
|
13
13
|
@@options.merge!(options)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def self.options
|
17
17
|
@@options
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
end
|
21
21
|
|
22
22
|
require 'custom_fields/version'
|
23
23
|
require 'custom_fields/extensions/mongoid/document'
|
24
24
|
require 'custom_fields/extensions/mongoid/relations/accessors'
|
25
|
+
require 'custom_fields/extensions/mongoid/relations/builders'
|
25
26
|
require 'custom_fields/types/default'
|
26
27
|
require 'custom_fields/types/string'
|
27
28
|
require 'custom_fields/types/text'
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 62196381
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- beta
|
11
|
-
-
|
12
|
-
version: 1.0.0.beta.
|
11
|
+
- 15
|
12
|
+
version: 1.0.0.beta.15
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Didier Lafforgue
|
@@ -17,27 +17,28 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2011-06-04 00:00:00
|
21
|
-
default_executable:
|
20
|
+
date: 2011-06-04 00:00:00 Z
|
22
21
|
dependencies:
|
23
22
|
- !ruby/object:Gem::Dependency
|
23
|
+
name: mongoid
|
24
24
|
prerelease: false
|
25
|
+
type: :runtime
|
25
26
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
27
|
none: false
|
27
28
|
requirements:
|
28
29
|
- - "="
|
29
30
|
- !ruby/object:Gem::Version
|
30
|
-
hash:
|
31
|
+
hash: 11
|
31
32
|
segments:
|
32
33
|
- 2
|
33
34
|
- 0
|
34
|
-
-
|
35
|
-
version: 2.0.
|
36
|
-
type: :runtime
|
35
|
+
- 2
|
36
|
+
version: 2.0.2
|
37
37
|
requirement: *id001
|
38
|
-
name: mongoid
|
39
38
|
- !ruby/object:Gem::Dependency
|
39
|
+
name: activesupport
|
40
40
|
prerelease: false
|
41
|
+
type: :runtime
|
41
42
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
43
|
none: false
|
43
44
|
requirements:
|
@@ -49,11 +50,11 @@ dependencies:
|
|
49
50
|
- 0
|
50
51
|
- 7
|
51
52
|
version: 3.0.7
|
52
|
-
type: :runtime
|
53
53
|
requirement: *id002
|
54
|
-
name: activesupport
|
55
54
|
- !ruby/object:Gem::Dependency
|
55
|
+
name: locomotive_carrierwave
|
56
56
|
prerelease: false
|
57
|
+
type: :runtime
|
57
58
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
59
|
none: false
|
59
60
|
requirements:
|
@@ -63,9 +64,7 @@ dependencies:
|
|
63
64
|
segments:
|
64
65
|
- 0
|
65
66
|
version: "0"
|
66
|
-
type: :runtime
|
67
67
|
requirement: *id003
|
68
|
-
name: locomotive_carrierwave
|
69
68
|
description: Manage custom fields to a mongoid document or a collection. This module is one of the core features we implemented in our custom cms named Locomotive.
|
70
69
|
email:
|
71
70
|
- didier@nocoffee.fr
|
@@ -83,6 +82,7 @@ files:
|
|
83
82
|
- lib/custom_fields/custom_fields_for.rb
|
84
83
|
- lib/custom_fields/extensions/mongoid/document.rb
|
85
84
|
- lib/custom_fields/extensions/mongoid/relations/accessors.rb
|
85
|
+
- lib/custom_fields/extensions/mongoid/relations/builders.rb
|
86
86
|
- lib/custom_fields/field.rb
|
87
87
|
- lib/custom_fields/metadata.rb
|
88
88
|
- lib/custom_fields/proxy_class_enabler.rb
|
@@ -101,7 +101,6 @@ files:
|
|
101
101
|
- config/locales/en.yml
|
102
102
|
- config/locales/fr.yml
|
103
103
|
- config/locales/pt-BR.yml
|
104
|
-
has_rdoc: true
|
105
104
|
homepage: http://github.com/locomotivecms/custom_fields
|
106
105
|
licenses: []
|
107
106
|
|
@@ -133,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
132
|
requirements: []
|
134
133
|
|
135
134
|
rubyforge_project: nowarning
|
136
|
-
rubygems_version: 1.
|
135
|
+
rubygems_version: 1.8.5
|
137
136
|
signing_key:
|
138
137
|
specification_version: 3
|
139
138
|
summary: Custom fields extension for Mongoid
|