secretary-rails 1.0.0.beta5 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a3fabc2aa72279b7c65cfb1437502c7828516aa
4
- data.tar.gz: e1a90574dfafbcc326d590658fd6a7841aebfa4b
3
+ metadata.gz: 2d57032707a48935f4cd868ce3230c1e30628931
4
+ data.tar.gz: b84674f6e9d5ff487974a94657b9765ddfe37650
5
5
  SHA512:
6
- metadata.gz: df71ac7242fe988ee50c01f21a83ced6c0ff2d3a3ba620ca09eee8eb3b0298aec0ef6014fd8e12fc005a9c776bfd782f3bde541e341e3851b35e66e533d46779
7
- data.tar.gz: 029dfa9765095925a73db1c3d2d2d894878731725fc7cbaf9d1522e872bbf190ac2da8d294839a00b39aef16d33780de0269280b5fd8bf3554331abf712f0093
6
+ metadata.gz: 1cf913c07526528e2b4c8f016a8d7f2ad72d1393b471f8590e261dc69338be83df8acb6a1262345f85693904576b0dd9acba6807a3fd9cfd84375224f8ff8e5d
7
+ data.tar.gz: d385d6f11fcc7db4b81a6f5791158576bd5e341fe233899e6581f25f6f514d9492e6c0f1572016b0753168ac32e19382d697c83a817d96b71179f59f63cebee6
data/README.md CHANGED
@@ -251,6 +251,34 @@ end
251
251
  Article.versioned_attributes # => ["headline", "images"]
252
252
  ```
253
253
 
254
+ #### Changes vs. Versions
255
+ There is one aspect that may seem a bit confusing. The behavior of
256
+ `record.changes`, and other Dirty attribute methods from ActiveModel, is
257
+ preserved, so any attribute you change will be added to the record's changes.
258
+ **However**, this does *not* necessarily mean that a version will be created,
259
+ because you may have changed an attribute that isn't versioned. For example:
260
+
261
+ ```ruby
262
+ class Article < ActiveRecord::Base
263
+ has_secretary on: ["headline", "body"]
264
+ end
265
+
266
+ article = Article.find(1)
267
+ article.changed? #=> false
268
+
269
+ article.slug = "new-slug-for-article" # "slug" is not versioned
270
+ article.changed? #=> true
271
+ article.changed #=> ["slug"]
272
+
273
+ article.versioned_changes #=> {}
274
+ article.save! # A new version isn't created!
275
+ ```
276
+
277
+ This also goes for associations: if you change an association on a parent
278
+ object, but in an "insignificant" way (i.e., no versioned attributes are
279
+ changed), then that association won't be considered "changed" when it comes
280
+ time to build the version.
281
+
254
282
 
255
283
  ## Contributing
256
284
  Fork it and send a pull request!
@@ -20,7 +20,7 @@ module Secretary
20
20
  # I didn't want to override the public ActiveRecord
21
21
  # API.
22
22
  def generate(object)
23
- changes = object.versioned_changes
23
+ changes = object.send(:__versioned_changes)
24
24
 
25
25
  object.versions.create({
26
26
  :user_id => object.logged_user_id,
@@ -1,3 +1,3 @@
1
1
  module Secretary
2
- GEM_VERSION = "1.0.0.beta5"
2
+ GEM_VERSION = "1.0.0"
3
3
  end
@@ -30,7 +30,10 @@ module Secretary
30
30
 
31
31
  attr_accessor :logged_user_id
32
32
 
33
- after_save :generate_version, :if => lambda { self.changed? }
33
+ after_save :generate_version,
34
+ :if => lambda { __versioned_changes.present? }
35
+
36
+ after_save :reset_versioned_changes
34
37
 
35
38
  include InstanceMethodsOnActivation
36
39
  end
@@ -74,7 +74,16 @@ module Secretary
74
74
  end
75
75
  end
76
76
 
77
- modified_changes[key] = [previous, current]
77
+ # This really shouldn't need to be here,
78
+ # but there is some confusion if we're destroying
79
+ # an associated object in a save callback on the
80
+ # parent object. We can't know that the callback
81
+ # is going to destroy this object on save,
82
+ # so we just have to add the association normally
83
+ # and then filter it out here.
84
+ if previous != current
85
+ modified_changes[key] = [previous, current]
86
+ end
78
87
  end
79
88
 
80
89
  modified_changes
@@ -93,5 +102,23 @@ module Secretary
93
102
  def versioned_attribute?(key)
94
103
  self.class.versioned_attributes.include?(key.to_s)
95
104
  end
105
+
106
+
107
+ private
108
+
109
+ # Memoized version changes.
110
+ # This is just so when we're near the end of an object's journey to
111
+ # persistence, we don't have to keep running the whole `versioned_changes`
112
+ # method, which is rather expensive. When we reach that point, we can
113
+ # be reasonably certain that no additional changes will occur, so it's
114
+ # safe to memoize the method. However, while an object is being modified,
115
+ # memoizing would be wrong, since that hash it constantly changing.
116
+ def __versioned_changes
117
+ @__versioned_changes ||= versioned_changes
118
+ end
119
+
120
+ def reset_versioned_changes
121
+ @__versioned_changes = nil
122
+ end
96
123
  end
97
124
  end
data/spec/factories.rb CHANGED
@@ -36,7 +36,7 @@ FactoryGirl.define do
36
36
  name "Bryan Ricker"
37
37
  end
38
38
 
39
- factory :version, class: "Secretary::Version" do
39
+ factory :version, :class => "Secretary::Version" do
40
40
  versioned { |v| v.association :story }
41
41
  user
42
42
  end