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 +4 -4
- data/README.md +28 -0
- data/app/models/secretary/version.rb +1 -1
- data/lib/secretary/gem_version.rb +1 -1
- data/lib/secretary/has_secretary.rb +4 -1
- data/lib/secretary/versioned_attributes.rb +28 -1
- data/spec/factories.rb +1 -1
- data/spec/internal/log/test.log +13768 -0
- data/spec/lib/secretary/dirty_singular_association_spec.rb +2 -4
- data/spec/lib/secretary/versioned_attributes_spec.rb +31 -1
- data/spec/tmp/db/migrate/{20131119033407_create_versions.rb → 20131119071129_create_versions.rb} +0 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d57032707a48935f4cd868ce3230c1e30628931
|
4
|
+
data.tar.gz: b84674f6e9d5ff487974a94657b9765ddfe37650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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!
|
@@ -30,7 +30,10 @@ module Secretary
|
|
30
30
|
|
31
31
|
attr_accessor :logged_user_id
|
32
32
|
|
33
|
-
after_save :generate_version,
|
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
|
-
|
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
|