Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add pretty reasons to error messages

  • Loading branch information...
commit a7224b0ab984166be81e4b98498b9016501b5837 1 parent ffc1fd2
@garybernhardt authored
Showing with 39 additions and 22 deletions.
  1. +27 −22 lib/do_not_want.rb
  2. +12 −0 spec/rails_integration_spec.rb
View
49 lib/do_not_want.rb
@@ -1,33 +1,38 @@
module DoNotWant
BAD_INSTANCE_METHODS = {
- :decrement => [:callbacks],
- :decrement! => [:validation],
- :increment => [:callbacks],
- :increment! => [:validation],
- :toggle => [:callbacks],
- :toggle! => [:validation],
- :update_attribute => [:validation],
+ :decrement => ["callbacks"],
+ :decrement! => ["validation"],
+ :increment => ["callbacks"],
+ :increment! => ["validation"],
+ :toggle => ["callbacks"],
+ :toggle! => ["validation"],
+ :update_attribute => ["validation"],
}
BAD_INSTANCE_METHOD_NAMES = BAD_INSTANCE_METHODS.keys
BAD_CLASS_METHODS = {
- :decrement_counter => [:validation, :callbacks],
- :delete => [:callbacks],
- :delete_all => [:callbacks],
- :find_by_sql => [:callbacks],
- :increment_counter => [:validation, :callbacks],
- :update_all => [:validation, :callbacks],
- :update_counters => [:validation, :callbacks],
+ :decrement_counter => ["validation", "callbacks"],
+ :delete => ["callbacks"],
+ :delete_all => ["callbacks"],
+ :find_by_sql => ["callbacks"],
+ :increment_counter => ["validation", "callbacks"],
+ :update_all => ["validation", "callbacks"],
+ :update_counters => ["validation", "callbacks"],
}
BAD_CLASS_METHOD_NAMES = BAD_CLASS_METHODS.keys
class NotSafe < Exception
- def initialize(called_class, called_method, reason)
- class_name = called_class.name
+ def initialize(called_object, called_method, reason)
+ class_name = called_object.class.name
method_name = called_method.to_s
- super "%s#%s isn't safe because %s" % [
- class_name,
- method_name,
+
+ method_description = if called_object.is_a?(Class)
+ "#{called_object.name}.#{method_name}"
+ else
+ "#{class_name}##{method_name}"
+ end
+
+ super "#{method_description} isn't safe because %s" % [
reason
]
end
@@ -49,7 +54,7 @@ def self.do_not_want!(method_name, reason)
if use_real_method
return self.send original_method_name, *args
end
- raise DoNotWant::NotSafe.new(self.class, method_name, reason)
+ raise DoNotWant::NotSafe.new(self, method_name, reason)
end
end
end
@@ -58,12 +63,12 @@ module ActiveRecord
class Base
DoNotWant::BAD_INSTANCE_METHODS.each do |method_name, reasons|
- do_not_want!(method_name, 'it skips validation')
+ do_not_want!(method_name, "it skips #{reasons.join(' and ')}")
end
class << self
DoNotWant::BAD_CLASS_METHODS.each do |method_name, reasons|
- do_not_want!(method_name, 'it skips validation')
+ do_not_want!(method_name, "it skips #{reasons.join(' and ')}")
end
end
end
View
12 spec/rails_integration_spec.rb
@@ -37,5 +37,17 @@ class Cheese < ActiveRecord::Base
it 'allows safe class methods' do
Cheese.columns.count.should == 2
end
+
+ it 'gives reasons' do
+ expect { cheese.decrement }.to raise_error(
+ DoNotWant::NotSafe,
+ "Cheese#decrement isn't safe because it skips callbacks")
+ expect { cheese.decrement! }.to raise_error(
+ DoNotWant::NotSafe,
+ "Cheese#decrement! isn't safe because it skips validation")
+ expect { Cheese.update_all }.to raise_error(
+ DoNotWant::NotSafe,
+ "Cheese.update_all isn't safe because it skips validation and callbacks")
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.