Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add reasons to bad method list

  • Loading branch information...
commit 0f6d57176d09ecdf56edd0ce8c89c93eb42be79a 1 parent fe0f548
@garybernhardt authored
Showing with 20 additions and 16 deletions.
  1. +18 −14 lib/do_not_want.rb
  2. +2 −2 spec/rails_integration_spec.rb
View
32 lib/do_not_want.rb
@@ -1,16 +1,20 @@
module DoNotWant
- RAILS_INSTANCE_METHOD_THAT_SKIP_VALIDATION = [
- :decrement!,
- :increment!,
- :toggle!,
- :update_attribute,
- ]
- RAILS_CLASS_METHODS_THAT_SKIP_VALIDATION = [
- :decrement_counter,
- :increment_counter,
- :update_all,
- :update_counters,
- ]
+ BAD_INSTANCE_METHODS = {
+ :decrement! => [:validation],
+ :increment! => [:validation],
+ :toggle! => [:validation],
+ :update_attribute => [:validation],
+ }
+ BAD_INSTANCE_METHOD_NAMES = BAD_INSTANCE_METHODS.keys
+
+ BAD_CLASS_METHODS = {
+ :decrement_counter => [:validation],
+ :increment_counter => [:validation],
+ :update_all => [:validation],
+ :update_counters => [:validation],
+ }
+ BAD_CLASS_METHOD_NAMES = BAD_CLASS_METHODS.keys
+
class NotSafe < Exception
def initialize(called_class, called_method, reason)
class_name = called_class.name
@@ -47,12 +51,12 @@ def self.do_not_want!(method_name, reason)
module ActiveRecord
class Base
- DoNotWant::RAILS_INSTANCE_METHOD_THAT_SKIP_VALIDATION.each do |method_name|
+ DoNotWant::BAD_INSTANCE_METHODS.each do |method_name, reasons|
do_not_want!(method_name, 'it skips validation')
end
class << self
- DoNotWant::RAILS_CLASS_METHODS_THAT_SKIP_VALIDATION.each do |method_name|
+ DoNotWant::BAD_CLASS_METHODS.each do |method_name, reasons|
do_not_want!(method_name, 'it skips validation')
end
end
View
4 spec/rails_integration_spec.rb
@@ -17,7 +17,7 @@ class Cheese < ActiveRecord::Base
let(:cheese) { Cheese.create! }
it 'rejects unsafe instance methods' do
- DoNotWant::RAILS_INSTANCE_METHOD_THAT_SKIP_VALIDATION.each do |method_name|
+ DoNotWant::BAD_INSTANCE_METHOD_NAMES.each do |method_name|
expect do
cheese.send method_name
end.to raise_error DoNotWant::NotSafe
@@ -29,7 +29,7 @@ class Cheese < ActiveRecord::Base
end
it 'rejects unsafe class methods' do
- DoNotWant::RAILS_CLASS_METHODS_THAT_SKIP_VALIDATION.each do |method_name|
+ DoNotWant::BAD_CLASS_METHOD_NAMES.each do |method_name|
expect { Cheese.send method_name }.to raise_error DoNotWant::NotSafe
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.