Permalink
Browse files

using alias method chain so method override happens

  • Loading branch information...
1 parent 222c66b commit 195840d81e664d0798708c2b35070f1a8bfa31bd @ryanb committed Jun 1, 2009
Showing with 13 additions and 17 deletions.
  1. +0 −4 init.rb
  2. +13 −10 lib/trusted_params/active_record_additions.rb
  3. +0 −3 spec/spec_helper.rb
View
@@ -1,5 +1 @@
require 'trusted_params'
-
-ActiveRecord::Base.class_eval do
- include TrustedParams::ActiveRecordAdditions
-end
@@ -2,33 +2,36 @@ module TrustedParams
module ActiveRecordAdditions
def self.included(base)
base.extend(ClassMethods)
+ base.alias_method_chain :remove_attributes_protected_from_mass_assignment, :trusted_params
end
- def remove_attributes_protected_from_mass_assignment(attributes)
- unless self.class.accessible_attributes.include? "all"
+ def remove_attributes_protected_from_mass_assignment_with_trusted_params(attributes)
+ unless self.class.accessible_attributes && self.class.accessible_attributes.include?("all")
attributes.each do |key, value|
- unless self.class.accessible_attributes.include?(key.to_s) || attributes.trusted?(key)
- raise ActiveRecord::UnavailableAttributeAssignmentError, "attribute \"#{key}\" is protected from mass assignment"
+ unless (self.class.accessible_attributes && self.class.accessible_attributes.include?(key.to_s)) || attributes.trusted?(key)
+ raise ActiveRecord::UnavailableAttributeAssignmentError, "attribute \"#{key}\" is protected from mass assignment, use attr_accessible"
end
end
end
attributes
end
module ClassMethods
- def attr_protected(*args)
+ def self.extended(base)
+ base.metaclass.alias_method_chain :attr_protected, :disabled
+ end
+
+ def attr_protected_with_disabled(*args)
raise "attr_protected has been disabled by trusted-params plugin, use attr_accessible"
end
end
end
end
module ActiveRecord
- # TODO for some reason this doesn't work for overriding methods
- # currently this is done in init.rb instead
- # Base.class_eval do
- # include TrustedParams::ActiveRecordAdditions
- # end
+ Base.class_eval do
+ include TrustedParams::ActiveRecordAdditions
+ end
class UnavailableAttributeAssignmentError < ActiveRecordError
end
View
@@ -9,9 +9,6 @@
end
class MockedModel < ActiveRecord::Base
-
- include TrustedParams::ActiveRecordAdditions
-
class_inheritable_hash :paginate_options
def self.paginate(options)

0 comments on commit 195840d

Please sign in to comment.