custom_fields 1.0.0.beta.6 → 1.0.0.beta.7

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.
@@ -94,6 +94,18 @@ module CustomFields
94
94
  end
95
95
  EOV
96
96
 
97
+ # mongoid tiny patch: for performance optimization (ie: we do want to invalidate klass every time we save a field)
98
+ unless instance_methods.include?('write_attributes_with_custom_fields')
99
+ class_eval do
100
+ def write_attributes_with_custom_fields(attrs = nil)
101
+ self.instance_variable_set(:@_writing_attributes_with_custom_fields, true)
102
+ self.write_attributes_without_custom_fields(attrs)
103
+ end
104
+
105
+ alias_method_chain :write_attributes, :custom_fields
106
+ end
107
+ end
108
+
97
109
  if itself
98
110
  class_eval <<-EOV
99
111
  alias :self_custom_fields :#{singular_name}_custom_fields
@@ -34,6 +34,7 @@ module CustomFields
34
34
 
35
35
  ## callbacks ##
36
36
  before_validation :set_alias
37
+ after_save :invalidate_klass
37
38
 
38
39
  ## methods ##
39
40
 
@@ -86,6 +87,12 @@ module CustomFields
86
87
  def to_hash(more = {})
87
88
  self.fields.keys.inject({}) do |memo, meth|
88
89
  memo[meth] = self.send(meth.to_sym); memo
90
+ end.tap do |hash|
91
+ self.class.field_types.keys.each do |type|
92
+ if self.respond_to?(:"#{type}_to_hash")
93
+ hash.merge!(self.send(:"#{type}_to_hash"))
94
+ end
95
+ end
89
96
  end.merge({
90
97
  'id' => self._id,
91
98
  'new_record' => self.new_record?,
@@ -145,6 +152,13 @@ module CustomFields
145
152
 
146
153
  alias_method_chain :parentize, :custom_fields
147
154
 
155
+ def invalidate_klass
156
+ return if self._parent.instance_variable_get(:@_writing_attributes_with_custom_fields)
157
+
158
+ target_name = self.association_name.to_s.gsub('_custom_fields', '')
159
+ self._parent.send(:"invalidate_#{target_name}_klass")
160
+ end
161
+
148
162
  end
149
163
 
150
164
  end
@@ -10,6 +10,11 @@ module CustomFields
10
10
 
11
11
  klass = Object.const_defined?(klass_name) ? Object.const_get(klass_name): nil
12
12
 
13
+ if klass && klass.built_at != parent.updated_at.try(:utc) # new version ?
14
+ self.invalidate_proxy_class_with_custom_fields(parent, association_name)
15
+ klass = nil
16
+ end
17
+
13
18
  if klass.nil?
14
19
  klass = self.build_proxy_class_with_custom_fields(fields, parent, association_name)
15
20
 
@@ -34,7 +39,7 @@ module CustomFields
34
39
  def self.build_proxy_class_with_custom_fields(fields, parent, association_name)
35
40
  (klass = Class.new(self)).class_eval <<-EOF
36
41
 
37
- cattr_accessor :custom_fields, :_parent, :association_name
42
+ cattr_accessor :custom_fields, :_parent, :association_name, :built_at
38
43
 
39
44
  def self.model_name
40
45
  @_model_name ||= ActiveModel::Name.new(self.superclass)
@@ -85,6 +90,8 @@ module CustomFields
85
90
 
86
91
  [*fields].each { |field| klass.apply_custom_field(field) }
87
92
 
93
+ klass.built_at = parent.updated_at.try(:utc)
94
+
88
95
  klass
89
96
  end
90
97
 
@@ -28,6 +28,10 @@ module CustomFields
28
28
  self.category_items.collect(&:_id)
29
29
  end
30
30
 
31
+ def category_to_hash
32
+ { 'category_items' => self.category_items.collect(&:to_hash) }
33
+ end
34
+
31
35
  def apply_category_type(klass)
32
36
  klass.class_eval <<-EOF
33
37
 
@@ -82,6 +86,21 @@ module CustomFields
82
86
  embedded_in :custom_field, :inverse_of => :category_items
83
87
 
84
88
  validates_presence_of :name
89
+
90
+ def to_hash(more = {})
91
+ self.fields.keys.inject({}) do |memo, meth|
92
+ memo[meth] = self.send(meth.to_sym); memo
93
+ end.merge({
94
+ 'id' => self._id,
95
+ 'new_record' => self.new_record?,
96
+ 'errors' => self.errors
97
+ }).merge(more)
98
+ end
99
+
100
+ def to_json
101
+ self.to_hash.to_json
102
+ end
103
+
85
104
  end
86
105
  end
87
106
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module CustomFields
3
- VERSION = "1.0.0.beta.6"
3
+ VERSION = "1.0.0.beta.7"
4
4
  end
5
5
  end
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: 62196367
4
+ hash: 62196365
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
9
  - 0
10
10
  - beta
11
- - 6
12
- version: 1.0.0.beta.6
11
+ - 7
12
+ version: 1.0.0.beta.7
13
13
  platform: ruby
14
14
  authors:
15
15
  - Didier Lafforgue
@@ -17,12 +17,13 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-03-08 00:00:00 +01:00
20
+ date: 2011-03-18 00:00:00 +01:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
+ name: mongoid
24
25
  prerelease: false
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ requirement: &id001 !ruby/object:Gem::Requirement
26
27
  none: false
27
28
  requirements:
28
29
  - - ~>
@@ -36,11 +37,11 @@ dependencies:
36
37
  - 7
37
38
  version: 2.0.0.rc.7
38
39
  type: :runtime
39
- requirement: *id001
40
- name: mongoid
40
+ version_requirements: *id001
41
41
  - !ruby/object:Gem::Dependency
42
+ name: activesupport
42
43
  prerelease: false
43
- version_requirements: &id002 !ruby/object:Gem::Requirement
44
+ requirement: &id002 !ruby/object:Gem::Requirement
44
45
  none: false
45
46
  requirements:
46
47
  - - ">="
@@ -52,11 +53,11 @@ dependencies:
52
53
  - 4
53
54
  version: 3.0.4
54
55
  type: :runtime
55
- requirement: *id002
56
- name: activesupport
56
+ version_requirements: *id002
57
57
  - !ruby/object:Gem::Dependency
58
+ name: locomotive_carrierwave
58
59
  prerelease: false
59
- version_requirements: &id003 !ruby/object:Gem::Requirement
60
+ requirement: &id003 !ruby/object:Gem::Requirement
60
61
  none: false
61
62
  requirements:
62
63
  - - ">="
@@ -66,8 +67,7 @@ dependencies:
66
67
  - 0
67
68
  version: "0"
68
69
  type: :runtime
69
- requirement: *id003
70
- name: locomotive_carrierwave
70
+ version_requirements: *id003
71
71
  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.
72
72
  email:
73
73
  - didier@nocoffee.fr