active_record-when_change 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5ce12661dc8bab7a2a4aae116b02d0988972a05
4
+ data.tar.gz: 3c52309cdb0a9fc770292dd56e2487edb9981e0e
5
+ SHA512:
6
+ metadata.gz: 43c519621680512d0dd992d196a2c9f925907290562a2a13a2d3fa3dd104c70ac73ed43293512ccd83450149c47763a23a1e245057ac73170a4636b61b702058
7
+ data.tar.gz: 09b9b554b2474666d601646147d5b047f72abb5a4f7a62645c2d8c690bcc1eaf656f3bb3adbfffe8ddfc2f6e6a1dfa68953b4212bc5dee3c64be9208f01d4ff8
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/*.log
19
+ spec/*.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record-when_change.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Zidni Mubarock
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # ActiveRecord::WhenChange
2
+ [![Gem Version](https://badge.fury.io/rb/active_record-when_change.png)](http://badge.fury.io/rb/active_record-when_change)
3
+
4
+ TODO: Write a gem description
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'active_record-when_change'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install active_record-when_change
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( http://github.com/<my-github-username>/active_record-when_change/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/when_change/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record-when_change"
8
+ spec.version = ActiveRecord::WhenChange::VERSION
9
+ spec.authors = ["Zidni Mubarock"]
10
+ spec.email = ["zidmubarock@gmail.com"]
11
+ spec.summary = %q{Simple DSL to control callback attribute changed in ActiveRecord model}
12
+ spec.description = %q{Simple DSL to control callback attribute changed in ActiveRecord model}
13
+ spec.homepage = "http://barock.github.io/active_record-when_change"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "activerecord", "~> 4.0.3"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "database_cleaner", "~> 1.2.0"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rr"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "sqlite3"
30
+ end
@@ -0,0 +1,75 @@
1
+ require 'active_record'
2
+ require 'active_support/all'
3
+ require "active_record/when_change/version"
4
+ module ActiveRecord
5
+ module WhenChange
6
+ VALID_OPTIONS = [:from, :to, :if, :unless]
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ def when_change attr, config={}, method = nil, &block
12
+ # just return if config is blank or (method or block not given)
13
+ return if self.send("id_was".to_sym).nil? || config.blank? || (method == nil && !block_given? )
14
+ config.keys.each{|key| logger.debug("#{key} is not valid options key for ArctiveRecordChanged") unless VALID_OPTIONS.include?(key)}
15
+
16
+ new_attribute = self.send(attr.to_sym)
17
+ old_attribute = self.send("#{attr}_was".to_sym)
18
+ return if new_attribute == old_attribute
19
+
20
+ correct_form = ( config[:from] and ( config[:from] != old_attribute ) ? false : true )
21
+ correct_to = ( config[:to] and ( config[:to] != new_attribute ) ? false : true )
22
+ correct_if = ( (config[:if] and ( eval(config[:if]) != true ) ) ? false : true )
23
+ correct_unless = ( (config[:unless] and (eval(config[:unless]) == true) )? false : true )
24
+
25
+ if correct_form && correct_to && correct_if && correct_unless
26
+ if block_given?
27
+ block.call(self)
28
+ else
29
+ send(method)
30
+ end
31
+ end
32
+ end
33
+
34
+ def attribute_changed(attr)
35
+ @_changed_attrs[attr] ||= ChangedAttributes.new(attr, @old_attributes || {}, self)
36
+ end
37
+
38
+ module ClassMethods
39
+ def when_change
40
+ end
41
+ end
42
+
43
+ class ChangedAttributes
44
+ attr_reader :attr, :old_attr, :current
45
+ def initialize attr, old_attr, current
46
+ @attr = attr
47
+ @old_attr = old_attr
48
+ @current = current
49
+ end
50
+
51
+ def inspect
52
+ old != value
53
+ end
54
+
55
+ def old
56
+ old_attr[attr]
57
+ end
58
+
59
+ def value
60
+ current.read_attribute attr
61
+ end
62
+
63
+ def old_value
64
+ old
65
+ end
66
+
67
+ def new_value
68
+ value
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+
75
+ ActiveRecord::Base.send(:include, ActiveRecord::WhenChange)
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module WhenChange
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe "when_change" do
4
+ it "should be respond_to the model" do
5
+ Post.should respond_to(:when_change)
6
+ Post.new.should respond_to(:when_change)
7
+ end
8
+ context "when model defined some when_change event" do
9
+ after do
10
+ reset_all_constants
11
+ end
12
+
13
+ context "whem its defined inside after_create" do
14
+ 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
+ after_commit do
22
+ when_change :status, from: :draft, to: :publised do
23
+ notify_subsribers
24
+ end
25
+ end
26
+ def notify_subsribers
27
+ true
28
+ end
29
+ end
30
+ end
31
+ let(:post){Post.new(status: :draft)}
32
+ it "should not triggered the action" do
33
+ post.should respond_to(:notify_subsribers)
34
+ mock(post).notify_subsribers.times(0)
35
+ post.update_attributes status: :publised
36
+ end
37
+ end
38
+
39
+ context "whem its defined inside after_update" do
40
+ before do
41
+ Post.class_eval do
42
+ after_update do
43
+ when_change :status, from: :draft, to: :publised do
44
+ notify_subsribers
45
+ end
46
+ end
47
+ def notify_subsribers
48
+ true
49
+ end
50
+ end
51
+ end
52
+ let(:post){Post.create(status: :draft)}
53
+ it "should not triggered the action" do
54
+ post.should respond_to(:notify_subsribers)
55
+ mock(post).notify_subsribers.times(1)
56
+ post.update_attributes status: :publised
57
+ end
58
+ end
59
+ end
60
+ it "should reset constant" do
61
+ Post.new.should_not respond_to(:notify_subsribers)
62
+ end
63
+
64
+ end
@@ -0,0 +1,40 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'pry'
8
+ require 'database_cleaner'
9
+ require 'active_record/when_change'
10
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/test.log')
11
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
12
+ :database => File.dirname(__FILE__) + "/test.sqlite3")
13
+ load File.dirname(__FILE__) + '/support/schema.rb'
14
+ load File.dirname(__FILE__) + '/support/post.rb'
15
+ load File.dirname(__FILE__) + '/support/constants_helper.rb'
16
+ RSpec.configure do |config|
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.run_all_when_everything_filtered = true
19
+ config.filter_run :focus
20
+ config.include ConstantsHelper
21
+ config.before(:suite) do
22
+ DatabaseCleaner.strategy = :deletion
23
+ DatabaseCleaner.clean_with(:truncation)
24
+ end
25
+
26
+ config.before(:each) do
27
+ DatabaseCleaner.start
28
+ end
29
+
30
+ config.after(:each) do
31
+ DatabaseCleaner.clean
32
+ end
33
+ config.mock_with :rr
34
+
35
+ # Run specs in random order to surface order dependencies. If you find an
36
+ # order dependency and want to debug it, you can fix the order by providing
37
+ # the seed, which is printed after each run.
38
+ # --seed 1234
39
+ config.order = 'random'
40
+ end
@@ -0,0 +1,41 @@
1
+ module ConstantsHelper
2
+
3
+ def overwrite_constant(constant, value, object = Object)
4
+ constant = constant.to_sym
5
+ saved_constants[object] ||= {}
6
+ saved_constants[object][constant] = object.const_get(constant) unless saved_constants[object].has_key?(constant)
7
+ silence_warnings { object.const_set(constant, value) }
8
+ end
9
+
10
+ def overwrite_constants(constants = {}, object = Object)
11
+ constants.each do |constant, value|
12
+ overwrite_constant(constant, value, object)
13
+ end
14
+ end
15
+
16
+ def reset_constant(constant, object = Object)
17
+ return nil unless saved_constants[object]
18
+ constant = constant.to_sym
19
+ silence_warnings { object.const_set(constant, saved_constants[object][constant]) }
20
+ saved_constants[object].delete(constant)
21
+ end
22
+
23
+ def reset_constants(constants = nil, object = Object)
24
+ return nil unless saved_constants[object]
25
+ constants ||= saved_constants[object].keys
26
+ constants.each do |constant|
27
+ reset_constant(constant, object)
28
+ end
29
+ end
30
+
31
+ def reset_all_constants
32
+ saved_constants.keys.each do |object|
33
+ reset_constants(nil, object)
34
+ end
35
+ end
36
+
37
+ def saved_constants
38
+ @saved_constants ||= {}
39
+ end
40
+
41
+ end
@@ -0,0 +1,19 @@
1
+ class Post < ActiveRecord::Base
2
+ # after_create do
3
+ # when_change :status, from: :draft, to: :publised do
4
+ # notify_subsribers
5
+ # end
6
+ # end
7
+ # after_commit do
8
+ # when_change :status, from: :draft, to: :publised do
9
+ # notify_subsribers
10
+ # end
11
+ # end
12
+
13
+
14
+
15
+
16
+
17
+ # def notify_subsribers
18
+ # end
19
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+ create_table :posts, :force => true do |t|
4
+ t.string :text
5
+ t.string :status
6
+ t.timestamps
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-when_change
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Zidni Mubarock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: database_cleaner
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Simple DSL to control callback attribute changed in ActiveRecord model
126
+ email:
127
+ - zidmubarock@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rspec
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - active_record-when_change.gemspec
139
+ - lib/active_record/when_change.rb
140
+ - lib/active_record/when_change/version.rb
141
+ - spec/lib/when_change_spec.rb
142
+ - spec/spec_helper.rb
143
+ - spec/support/constants_helper.rb
144
+ - spec/support/post.rb
145
+ - spec/support/schema.rb
146
+ homepage: http://barock.github.io/active_record-when_change
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.2.2
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Simple DSL to control callback attribute changed in ActiveRecord model
170
+ test_files:
171
+ - spec/lib/when_change_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/support/constants_helper.rb
174
+ - spec/support/post.rb
175
+ - spec/support/schema.rb