activemodel-attribute_changed_specification 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,6 +27,14 @@ user.name = 'Bob'
27
27
  user.name_changed?(from: nil, to: 'Bob') # => true
28
28
  user.name_changed?(from: 'Paul', to: 'Bob') # => false
29
29
  ```
30
+ using only from or to
31
+
32
+ ```ruby
33
+ user = User.new
34
+ user.name = 'Bob'
35
+ user.name_changed?(to: 'Bob') # => true
36
+ user.name_changed?(from: 'Paul') # => false
37
+ ```
30
38
 
31
39
  You can still use original `_changed?` method.
32
40
 
@@ -9,7 +9,18 @@ module AttributeChangedSpecification
9
9
  attribute_changed_without_specification?(attr)
10
10
  else
11
11
  return false unless self.changes.include?(attr)
12
- self.changes[attr][0] == args[0][:from] && __send__(attr) == args[0][:to]
12
+ from = args[0][:from]
13
+ to = args[0][:to]
14
+
15
+ if from && to
16
+ self.changes[attr][0] == from && __send__(attr) == to
17
+ elsif from
18
+ self.changes[attr][0] == from
19
+ elsif to
20
+ __send__(attr) == to
21
+ else
22
+ attribute_changed_without_specification?(attr, args)
23
+ end
13
24
  end
14
25
  end
15
26
  alias_method_chain :attribute_changed?, :specification
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  module AttributeChangedSpecification
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -73,4 +73,21 @@ class DirtyTest < ActiveModel::TestCase
73
73
  assert @model.color_changed?
74
74
  assert @model.color_changed?(from: 'red', to: 'green')
75
75
  end
76
+
77
+ test "dectect change passing from or to" do
78
+ @model.color = 'red'
79
+ @model.save
80
+ @model.color = 'green'
81
+
82
+ assert @model.color_changed?
83
+ assert @model.color_changed?(from: 'red')
84
+
85
+ assert @model.color_changed?(to: 'green')
86
+ end
87
+
88
+ test "pass *args to original method when invalid arguments was given" do
89
+ @model.color = 'red'
90
+
91
+ assert_raise(ArgumentError) { @model.color_changed?(via: self) }
92
+ end
76
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel-attribute_changed_specification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-19 00:00:00.000000000 Z
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel