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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c35d26cc36f27c04d6498116a6eadefe32b8e38
4
- data.tar.gz: a81fa91183efb5cef250d469ca8326edb5e37479
3
+ metadata.gz: 053ba80b3d4e3764f1aeea8fae54416b1561af28
4
+ data.tar.gz: 61a8276db83af320e03f3c743aaf8d899ffe27c4
5
5
  SHA512:
6
- metadata.gz: cec571b1f0fe99bb131f0be8ceb07a00153b47006a6b5d899e3c6890ac464a4d340792e0b80f8a6999cd9fd89f16cebf986c7cddb8eb423ff1db21297d96f5ee
7
- data.tar.gz: 43c76320d0e4f79de94cafb130fbb9540a0d30ae6e489638225dcb6d8bf9a015cc40d9e2a89ab7ce0372402e029dbf4863fec173730bd5a74c705dddd34bda62
6
+ metadata.gz: f31875def37d4bad43200c00fbb965b0bbb0a0454cda57356c6366295fbed4e63fe7fc6f3432f2480337c1f2db6d18f6ae0fac014d2ee00c2d147921b71f6490
7
+ data.tar.gz: fa9910330333548683423af9648efcf307d6a23695f9439aef579db8dc875d6a661a353183f96fb282123158de4832a5e40ae744a79a54293327d1c87d3b71cb
@@ -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
- [association, ids].each do |name|
19
- define_method "#{name}=" do |value|
20
- attribute_will_change!(association.to_s) unless send(name) == value
21
- super(value)
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
@@ -1,3 +1,3 @@
1
1
  module DirtyAssociations
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  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.0
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-03 00:00:00.000000000 Z
11
+ date: 2014-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails