dirty_associations 0.4.0 → 0.4.1
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/lib/dirty_associations.rb +21 -5
- data/lib/dirty_associations/version.rb +1 -1
- data/test/dirty_associations_test.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 053ba80b3d4e3764f1aeea8fae54416b1561af28
|
4
|
+
data.tar.gz: 61a8276db83af320e03f3c743aaf8d899ffe27c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f31875def37d4bad43200c00fbb965b0bbb0a0454cda57356c6366295fbed4e63fe7fc6f3432f2480337c1f2db6d18f6ae0fac014d2ee00c2d147921b71f6490
|
7
|
+
data.tar.gz: fa9910330333548683423af9648efcf307d6a23695f9439aef579db8dc875d6a661a353183f96fb282123158de4832a5e40ae744a79a54293327d1c87d3b71cb
|
data/lib/dirty_associations.rb
CHANGED
@@ -13,14 +13,19 @@ module DirtyAssociations
|
|
13
13
|
#
|
14
14
|
# The +association+ parameter should be a string or symbol representing the name of an association.
|
15
15
|
def monitor_association_changes(association)
|
16
|
+
define_method "#{association}=" do |value|
|
17
|
+
attribute_will_change!(association.to_s) if _association_will_change?(association, value)
|
18
|
+
super(value)
|
19
|
+
end
|
20
|
+
|
16
21
|
ids = "#{association.to_s.singularize}_ids"
|
17
22
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
+
define_method "#{ids}=" do |value|
|
24
|
+
attribute_will_change!(association.to_s) if _ids_will_change?(ids, value)
|
25
|
+
super(value)
|
26
|
+
end
|
23
27
|
|
28
|
+
[association, ids].each do |name|
|
24
29
|
define_method "#{name}_change" do
|
25
30
|
changes[name]
|
26
31
|
end
|
@@ -35,4 +40,15 @@ module DirtyAssociations
|
|
35
40
|
end
|
36
41
|
end
|
37
42
|
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def _association_will_change?(association, value)
|
47
|
+
send(association) != value
|
48
|
+
end
|
49
|
+
|
50
|
+
def _ids_will_change?(ids, value)
|
51
|
+
value = Array(value).reject &:blank?
|
52
|
+
send(ids) != value
|
53
|
+
end
|
38
54
|
end
|
@@ -30,6 +30,24 @@ class DirtyAssociationsTest < ActiveSupport::TestCase
|
|
30
30
|
assert bar.foos_changed?
|
31
31
|
end
|
32
32
|
|
33
|
+
test "setting has_many association ids works with non-array" do
|
34
|
+
foo = FactoryGirl.create(:foo)
|
35
|
+
|
36
|
+
refute bar.foos_changed?
|
37
|
+
|
38
|
+
bar.foo_ids = foo.id
|
39
|
+
assert_equal [ foo.id ], bar.foo_ids
|
40
|
+
assert bar.foos_changed?
|
41
|
+
end
|
42
|
+
|
43
|
+
test "setting has_many association ids ignores blanks" do
|
44
|
+
foos = bar.foos
|
45
|
+
bar.foo_ids += [ "", nil ]
|
46
|
+
assert_equal foos, bar.foos
|
47
|
+
|
48
|
+
refute bar.foos_changed?
|
49
|
+
end
|
50
|
+
|
33
51
|
test "changes reset by save" do
|
34
52
|
bar.foos = [ FactoryGirl.create(:foo) ]
|
35
53
|
assert bar.foos_changed?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dirty_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jen Page
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|