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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8221e6fff490d4b80ce3fb4fb3d66740e584b620
4
- data.tar.gz: fe02eaadb35ea631179c8490c627d25c1271aa3d
3
+ metadata.gz: c7e017b6ca7ed73f70b7ac906edee79b39941931
4
+ data.tar.gz: e257700dc988e89ba3c638e3e374ff578f4ab0ab
5
5
  SHA512:
6
- metadata.gz: fe4905219abcebff8bc4834670581004952ea82f6a50137b0a6a3638b8b0fa4ed3d79a7798ecb124996345a8bc53004ebf5a3425082faa9e19702ec9011ba953
7
- data.tar.gz: 9619d747a2496343384c6c66c93f3ec3af6204612c996bc94e4b5e18e9a11e14d2ae110f20f7dc9c81bb35fae2c3de8c4d49377bce3dc25fea000d870b590dbc
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 !@_new_record || config.blank? || (method == nil && !block_given? )
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,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module WhenChange
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -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.class_eval do
16
- after_create do
17
- when_change :status, from: :draft, to: :publised do
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.class_eval do
36
- after_update do
37
- when_change :status, from: :draft, to: :publised do
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: :draft)}
47
- it_behaves_like "callback triggered" do
48
- let(:method){:notify_subsribers}
49
- let(:attributes){{status: :publised}}
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
- end
52
- context "when if" do
53
- context "when true" do
54
- before do
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
- def notify_subsribers
63
- true
64
- end
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
- let(:post){Post.create(status: :draft)}
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
- end
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.3
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-02-28 00:00:00.000000000 Z
11
+ date: 2014-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord