callback_skipper 0.3.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1,4 +1,4 @@
1
- rvm use ree-1.8.7-2011.03@callback_skipper --create
1
+ rvm use ruby-1.9.3-p125@callback_skipper --create
2
2
  if ! command -v bundle ; then
3
3
  gem install bundler
4
4
  bundle
data/README.md CHANGED
@@ -24,6 +24,19 @@ foo = Foo.new
24
24
  foo.skip_callback :save, :after, :do_something_expensive
25
25
  foo.save!
26
26
  ```
27
+ ### Error handling
28
+ If the code attempts to skip an undefined callback, a **CallbackSkipper::CallbackMethodNotDefined** will be raised:
29
+
30
+ ```ruby
31
+ # class Foo is defined above
32
+ foo = Foo.new
33
+ begin
34
+ foo.skip_callback :save ,:after, :method_that_is_not_defined
35
+ rescue CallbackSkipper::CallbackMethodNotDefined
36
+ puts "You should not skip callback methods that are not defined"
37
+ end
38
+ ```
39
+
27
40
  ### Example: FactoryGirl Usage
28
41
 
29
42
  https://github.com/thoughtbot/factory_girl
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
4
+ task :default => 'spec'
@@ -20,5 +20,9 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here
22
22
  s.add_runtime_dependency 'activerecord', ['>= 3.0']
23
- s.add_development_dependency "rake", ["0.9.2.2"]
23
+ s.add_development_dependency 'rake', ['0.9.2.2']
24
+ s.add_development_dependency('sqlite3', ['>= 1.3.5'])
25
+ s.add_development_dependency('rspec', ['>= 2.11.0'])
26
+ s.add_development_dependency('pry', ['>= 0'])
27
+
24
28
  end
@@ -2,10 +2,25 @@ require "callback_skipper/version"
2
2
 
3
3
  module CallbackSkipper
4
4
  extend ActiveSupport::Concern
5
+
6
+ class CallbackNotDefinedError < StandardError; end
7
+
8
+ # skip a specific callback for the given instance
9
+ # @raises CallbackNotDefinedError if no callback is found matching the requested callback
5
10
  def skip_callback(*args)
11
+ raise CallbackNotDefinedError.new("Callback not defined matching: #{self.class.name} #{args}") unless callback_defined?(*args)
6
12
  instance = self
7
13
  args << {:if => lambda { self == instance }}
8
14
  self.class.skip_callback *args
9
15
  end
16
+
17
+ private
18
+ # asserts that the callback exists in the callback chain
19
+ # @param args [Array] array of options matching the set_callback signiture. first param is either the method name or the callback type (before/after/around)
20
+ def callback_defined?(name, *args)
21
+ callback_method_name = args[0].in?([:before, :after, :around]) ? args[1] : args[0]
22
+ self.send("_#{name}_callbacks").collect(&:filter).include?(callback_method_name)
23
+ end
10
24
  end
25
+
11
26
  ActiveRecord::Base.send :include, CallbackSkipper
@@ -1,3 +1,3 @@
1
1
  module CallbackSkipper
2
- VERSION = "0.3.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe CallbackSkipper do
4
+ describe "For an instance of ActiveRecord model with callbacks" do
5
+
6
+ class User < ActiveRecord::Base
7
+ around_create :do_around_create
8
+ after_save :do_after_save
9
+ before_validation :do_before_validation
10
+
11
+ protected
12
+
13
+ def do_around_create
14
+ true
15
+ end
16
+
17
+ def do_after_save
18
+ true
19
+ end
20
+
21
+ def do_before_validation
22
+ true
23
+ end
24
+ end
25
+
26
+ before do
27
+ @user = User.new
28
+ end
29
+
30
+ describe "when its saved while skipping all callbacks" do
31
+ before do
32
+ @user.skip_callback :save, :after, :do_after_save
33
+ @user.skip_callback :create, :around, :do_around_create
34
+ @user.skip_callback :validation, :before, :do_before_validation
35
+ end
36
+ it "should skip all validations" do
37
+ @user.should_receive(:do_after_save).never
38
+ @user.should_receive(:do_around_create).never
39
+ @user.should_receive(:do_before_validation).never
40
+ @user.save!
41
+ end
42
+ describe "when a new instance of user is assigned to the same variable that skipped callbacks before" do
43
+ before do
44
+ @user = User.new
45
+ end
46
+ it "should not skip validations upon saving" do
47
+ @user.should_receive(:do_after_save).once
48
+ @user.should_receive(:do_around_create).once
49
+ @user.should_receive(:do_before_validation).once
50
+ @user.save!
51
+ end
52
+ end
53
+ describe "when a new instance of user is initialized" do
54
+ before do
55
+ @another_user = User.new
56
+ end
57
+ it "should not skip validations upon saving" do
58
+ @another_user.should_receive(:do_after_save).once
59
+ @another_user.should_receive(:do_around_create).once
60
+ @another_user.should_receive(:do_before_validation).once
61
+ @another_user.save!
62
+ end
63
+ end
64
+ end
65
+ it "should through an error when attempting to skip an undefined callback" do
66
+ lambda { @user.skip_callback :save, :after, :method_that_doesnt_exist }.should raise_error CallbackSkipper::CallbackNotDefinedError
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
3
+ ActiveRecord::Schema.define(:version => 1) do
4
+ create_table :users, :force => true do |t|
5
+ t.column :name, :string
6
+ t.column :email, :string
7
+ t.column :password, :string
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'rspec'
7
+ require 'active_record'
8
+ require 'pry'
9
+
10
+ require 'callback_skipper'
11
+ require 'setup_database'
12
+
13
+
14
+ RSpec.configure do |config|
15
+ config.mock_with :rspec
16
+ end
metadata CHANGED
@@ -1,64 +1,103 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: callback_skipper
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Ryan Sonnek
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-01-24 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
- none: false
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- hash: 7
27
- segments:
28
- - 3
29
- - 0
30
- version: "3.0"
31
- requirement: *id001
32
- prerelease: false
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
33
15
  name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
34
22
  type: :runtime
35
- - !ruby/object:Gem::Dependency
36
- version_requirements: &id002 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
37
33
  none: false
38
- requirements:
39
- - - "="
40
- - !ruby/object:Gem::Version
41
- hash: 11
42
- segments:
43
- - 0
44
- - 9
45
- - 2
46
- - 2
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
47
37
  version: 0.9.2.2
48
- requirement: *id002
38
+ type: :development
49
39
  prerelease: false
50
- name: rake
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.5
51
54
  type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.5
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.11.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.11.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
52
94
  description: programatically skip ActiveRecord callbacks
53
- email:
95
+ email:
54
96
  - ryan@codecrate.com
55
97
  executables: []
56
-
57
98
  extensions: []
58
-
59
99
  extra_rdoc_files: []
60
-
61
- files:
100
+ files:
62
101
  - .gitignore
63
102
  - .rvmrc
64
103
  - CONTRIBUTORS.txt
@@ -69,38 +108,40 @@ files:
69
108
  - callback_skipper.gemspec
70
109
  - lib/callback_skipper.rb
71
110
  - lib/callback_skipper/version.rb
72
- homepage: ""
111
+ - spec/lib/callback_skipper_spec.rb
112
+ - spec/setup_database.rb
113
+ - spec/spec_helper.rb
114
+ homepage: ''
73
115
  licenses: []
74
-
75
116
  post_install_message:
76
117
  rdoc_options: []
77
-
78
- require_paths:
118
+ require_paths:
79
119
  - lib
80
- required_ruby_version: !ruby/object:Gem::Requirement
120
+ required_ruby_version: !ruby/object:Gem::Requirement
81
121
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
87
127
  - 0
88
- version: "0"
89
- required_rubygems_version: !ruby/object:Gem::Requirement
128
+ hash: 2424141270523251175
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
130
  none: false
91
- requirements:
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
96
136
  - 0
97
- version: "0"
137
+ hash: 2424141270523251175
98
138
  requirements: []
99
-
100
139
  rubyforge_project: callback_skipper
101
- rubygems_version: 1.8.10
140
+ rubygems_version: 1.8.24
102
141
  signing_key:
103
142
  specification_version: 3
104
143
  summary: Skip ActiveRecord callbacks
105
- test_files: []
106
-
144
+ test_files:
145
+ - spec/lib/callback_skipper_spec.rb
146
+ - spec/setup_database.rb
147
+ - spec/spec_helper.rb