vidibus-inheritance 0.3.10 → 0.3.11

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.rdoc CHANGED
@@ -58,6 +58,12 @@ All attributes will be inherited, except these ACQUIRED_ATTRIBUTES:
58
58
  updated_at
59
59
  version
60
60
  versions
61
+
62
+ You may overwrite acquired attributes by defining a method on your inherited document and its embedded documents:
63
+
64
+ def acquired_attributes
65
+ Vidibus::Inheritance::Mongoid::ACQUIRED_ATTRIBUTES + %w[my custom values]
66
+ end
61
67
 
62
68
 
63
69
  === Manage mutations of embedded documents
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.10
1
+ 0.3.11
@@ -1,2 +1,3 @@
1
1
  require "inheritance/mongoid"
2
- require "inheritance/validators"
2
+ require "inheritance/validators"
3
+
@@ -2,10 +2,14 @@ module Vidibus
2
2
  module Inheritance
3
3
  module Mongoid
4
4
  extend ActiveSupport::Concern
5
-
5
+
6
6
  ACQUIRED_ATTRIBUTES = %w[_id _type uuid ancestor_uuid mutated_attributes mutated created_at updated_at version versions]
7
7
 
8
8
  included do
9
+
10
+ # To define additional aquired attributes on your model, set @@acquired_attributes on it.
11
+ #@@aquired_attributes = nil
12
+
9
13
  attr_accessor :inherited_attributes, :_inherited
10
14
  attr_protected :mutated_attributes, :mutated
11
15
 
@@ -139,7 +143,13 @@ module Vidibus
139
143
  clone.save!
140
144
  clone
141
145
  end
142
-
146
+
147
+ # Returns acquired attributes.
148
+ # Overwrite this method to define custom ones.
149
+ def acquired_attributes
150
+ ACQUIRED_ATTRIBUTES
151
+ end
152
+
143
153
  private
144
154
 
145
155
  # Performs inheritance of attributes while excluding acquired and mutated ones.
@@ -147,7 +157,7 @@ module Vidibus
147
157
  def inherit_attributes(options = {})
148
158
  track_mutations
149
159
  self._inherited = true
150
- exceptions = ACQUIRED_ATTRIBUTES
160
+ exceptions = self.acquired_attributes
151
161
  reset = options[:reset]
152
162
  if !reset
153
163
  exceptions += mutated_attributes
@@ -228,7 +238,7 @@ module Vidibus
228
238
  # Stores changed attributes as #mutated_attributes unless they have been inherited recently.
229
239
  def track_mutations
230
240
  changed_items = new_record? ? attributes.keys : changes.keys
231
- changed_items -= ACQUIRED_ATTRIBUTES
241
+ changed_items -= self.acquired_attributes
232
242
  if inherited_attributes
233
243
  for key, value in inherited_attributes
234
244
  changed_items.delete(key) if value == attributes[key]
@@ -270,11 +280,15 @@ module Vidibus
270
280
  # Returns list of inheritable attributes of a given document.
271
281
  # The list will include the _id as reference.
272
282
  def inheritable_document_attributes(doc)
273
- attrs = doc.attributes.except(*ACQUIRED_ATTRIBUTES)
283
+ # puts "doc = #{doc.inspect}"
284
+ # puts "doc.acquired_attributes = #{doc.acquired_attributes.inspect}"
285
+ # puts "doc.try!(:acquired_attributes) = #{doc.try!(:acquired_attributes).inspect}"
286
+ exceptions = doc.try!(:acquired_attributes) || ACQUIRED_ATTRIBUTES
287
+ attrs = doc.attributes.except(*exceptions)
274
288
  attrs[:_reference_id] = doc._id
275
289
  attrs
276
290
  end
277
-
291
+
278
292
  # Applies changes to inheritors.
279
293
  def update_inheritors
280
294
  return unless inheritors.any?
data/spec/models.rb CHANGED
@@ -19,6 +19,7 @@ class Child
19
19
  include Mongoid::Document
20
20
  field :name
21
21
  field :mutated, :type => Boolean
22
+ field :custom
22
23
  validates :name, :presence => true
23
24
  embedded_in :model, :inverse_of => :children
24
25
  embeds_many :puppets
@@ -34,10 +34,31 @@ describe "Inheritance" do
34
34
  inheritor.age.should eql(35)
35
35
  end
36
36
 
37
- it "should not inherit acquired attributes" do
38
- inheritor.update_attributes(:ancestor => ancestor)
39
- Model::ACQUIRED_ATTRIBUTES.should include("uuid")
40
- inheritor.uuid.should_not eql(ancestor.uuid)
37
+ context "of acquired attributes" do
38
+ it "should not inherit ACQUIRED_ATTRIBUTES" do
39
+ inheritor.update_attributes(:ancestor => ancestor)
40
+ Model::ACQUIRED_ATTRIBUTES.should include("uuid")
41
+ inheritor.uuid.should_not eql(ancestor.uuid)
42
+ end
43
+
44
+ it "should not inherit custom acquired_attributes" do
45
+ Model.send(:define_method, :acquired_attributes) do
46
+ Model::ACQUIRED_ATTRIBUTES + ["name"]
47
+ end
48
+ inheritor.update_attributes(:ancestor => anna)
49
+ inheritor.name.should be_nil
50
+ Model.send(:remove_method, :acquired_attributes)
51
+ end
52
+
53
+ it "should not inherit custom acquired_attributes on embedded documents" do
54
+ Child.send(:define_method, :acquired_attributes) do
55
+ Vidibus::Inheritance::Mongoid::ACQUIRED_ATTRIBUTES + ["custom"]
56
+ end
57
+ anna.children.create(:name => "Lucy", :custom => "something")
58
+ inheritor.update_attributes(:ancestor => anna)
59
+ inheritor.children.first.custom.should be_nil
60
+ Child.send(:remove_method, :acquired_attributes)
61
+ end
41
62
  end
42
63
 
43
64
  it "should apply ancestor's attributes to inheritor but keep previously mutated attributes" do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{vidibus-inheritance}
8
- s.version = "0.3.10"
8
+ s.version = "0.3.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andre Pankratz"]
12
- s.date = %q{2010-08-16}
12
+ s.date = %q{2010-08-17}
13
13
  s.description = %q{This gem allows inheritance of objects for Rails 3 with Mongoid. It will update all attributes and embedded documents of inheritors when ancestor gets changed.}
14
14
  s.email = %q{andre@vidibus.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-inheritance
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 10
10
- version: 0.3.10
9
+ - 11
10
+ version: 0.3.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andre Pankratz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-16 00:00:00 +02:00
18
+ date: 2010-08-17 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency