active_record-when_change 0.0.3 → 0.0.4
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/active_record/when_change.rb +3 -3
- data/lib/active_record/when_change/version.rb +1 -1
- data/spec/lib/when_change_spec.rb +27 -43
- 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: c7e017b6ca7ed73f70b7ac906edee79b39941931
|
4
|
+
data.tar.gz: e257700dc988e89ba3c638e3e374ff578f4ab0ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9f804bae907a7c310ac4a5d7308f6cd21f02426a4240176cd4f6dfbf45c273cef8a3aa37c36a5121d8824c264fb3c17f4a7c63a0ec7301b0a1c12c841a9234f
|
7
|
+
data.tar.gz: 42f6e2a9dcfdd4c383cbee19aa6ac9358463b62b7fc3c9a0eaa1d0b763b4294cdd442e195cb8b26df13174563197c9411dac143bd2e1ed02ee51592d5614df43
|
@@ -13,15 +13,15 @@ module ActiveRecord
|
|
13
13
|
|
14
14
|
def when_change attr, config={}, method = nil, &block
|
15
15
|
# just return if config is blank or (method or block not given)
|
16
|
-
return if
|
16
|
+
return if self.new_record? || config.blank? || (method == nil && !block_given? )
|
17
17
|
config.keys.each{|key| logger.debug("#{key} is not valid options key for ArctiveRecordChanged") unless VALID_OPTIONS.include?(key)}
|
18
18
|
|
19
19
|
new_attribute = self.send(attr.to_sym)
|
20
20
|
old_attribute = self.send("#{attr}_was".to_sym)
|
21
21
|
return if new_attribute == old_attribute
|
22
22
|
|
23
|
-
correct_form = ( config[:from] and ( config[:from] != old_attribute ) ? false : true )
|
24
|
-
correct_to = ( config[:to] and ( config[:to] != new_attribute ) ? false : true )
|
23
|
+
correct_form = ( (config[:from] and ( config[:from] != old_attribute )) ? false : true )
|
24
|
+
correct_to = ( (config[:to] and ( config[:to] != new_attribute )) ? false : true )
|
25
25
|
correct_if = ( (config[:if] and ( eval(config[:if]) != true ) ) ? false : true )
|
26
26
|
correct_unless = ( (config[:unless] and (eval(config[:unless]) == true) )? false : true )
|
27
27
|
|
@@ -1,25 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "when_change" do
|
4
|
+
before do
|
5
|
+
Post.send(:define_method,:notify_subsribers, ->{true})
|
6
|
+
end
|
7
|
+
after do
|
8
|
+
Post.reset_callbacks(:update)
|
9
|
+
end
|
4
10
|
it "should be respond_to the model" do
|
5
11
|
Post.should respond_to(:when_change)
|
6
12
|
Post.new.should respond_to(:when_change)
|
7
13
|
end
|
8
14
|
context "when model defined some when_change event" do
|
9
|
-
after(:each) do
|
10
|
-
reset_all_constants
|
11
|
-
end
|
12
|
-
|
13
15
|
context "whem its defined inside after_create" do
|
14
16
|
before do
|
15
|
-
Post.
|
16
|
-
|
17
|
-
|
18
|
-
notify_subsribers
|
19
|
-
end
|
20
|
-
end
|
21
|
-
def notify_subsribers
|
22
|
-
true
|
17
|
+
Post.after_create do
|
18
|
+
when_change :status, from: :draft, to: :publised do
|
19
|
+
notify_subsribers
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
@@ -32,50 +29,37 @@ describe "when_change" do
|
|
32
29
|
|
33
30
|
context "whem its defined inside after_update" do
|
34
31
|
before do
|
35
|
-
Post.
|
36
|
-
|
37
|
-
|
38
|
-
notify_subsribers
|
39
|
-
end
|
40
|
-
end
|
41
|
-
def notify_subsribers
|
42
|
-
true
|
32
|
+
Post.after_update do
|
33
|
+
when_change :status, from: 'draft', to: 'published' do
|
34
|
+
notify_subsribers
|
43
35
|
end
|
44
36
|
end
|
45
37
|
end
|
46
|
-
let(:post){Post.create(status:
|
47
|
-
|
48
|
-
|
49
|
-
|
38
|
+
let(:post){Post.create(status: 'draft')}
|
39
|
+
it "should triggered the calback" do
|
40
|
+
mock(post).notify_subsribers.times(1)
|
41
|
+
post.update_attributes(status: 'published')
|
50
42
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
reset_all_constants
|
56
|
-
Post.class_eval do
|
57
|
-
after_update do
|
43
|
+
context "when if" do
|
44
|
+
context "when true" do
|
45
|
+
before do
|
46
|
+
Post.after_update do
|
58
47
|
when_change :status, if: 'true' do
|
59
48
|
notify_subsribers
|
60
49
|
end
|
61
50
|
end
|
62
|
-
|
63
|
-
|
64
|
-
|
51
|
+
end
|
52
|
+
let(:post){Post.create(status: :draft)}
|
53
|
+
it_behaves_like "callback triggered" do
|
54
|
+
let(:method){:notify_subsribers}
|
55
|
+
let(:attributes){{status: :publised}}
|
65
56
|
end
|
66
57
|
end
|
67
|
-
|
68
|
-
it_behaves_like "callback triggered" do
|
69
|
-
let(:method){:notify_subsribers}
|
70
|
-
let(:attributes){{status: :publised}}
|
58
|
+
context "when false" do
|
71
59
|
end
|
72
60
|
end
|
73
|
-
context "when false" do
|
74
|
-
end
|
75
61
|
end
|
76
|
-
|
77
|
-
it "should reset constant" do
|
78
|
-
Post.new.should_not respond_to(:notify_subsribers)
|
62
|
+
|
79
63
|
end
|
80
64
|
|
81
65
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-when_change
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zidni Mubarock
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|