mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix deprecation warnings when using config.active_record.mass_assignment_sanitizer=
This commit is contained in:
parent
ae3767c799
commit
641611a690
2 changed files with 40 additions and 3 deletions
|
@ -2,7 +2,7 @@ require 'active_support/concern'
|
||||||
|
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
ActiveSupport.on_load(:active_record_config) do
|
ActiveSupport.on_load(:active_record_config) do
|
||||||
mattr_accessor :whitelist_attributes, instance_accessor: false
|
mattr_accessor :whitelist_attributes, instance_accessor: false
|
||||||
mattr_accessor :mass_assignment_sanitizer, instance_accessor: false
|
mattr_accessor :mass_assignment_sanitizer, instance_accessor: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,12 +11,12 @@ module ActiveRecord
|
||||||
include ActiveModel::MassAssignmentSecurity
|
include ActiveModel::MassAssignmentSecurity
|
||||||
|
|
||||||
included do
|
included do
|
||||||
attr_accessible(nil) if Model.whitelist_attributes
|
initialize_mass_assignment_sanitizer
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def inherited(child) # :nodoc:
|
def inherited(child) # :nodoc:
|
||||||
child.attr_accessible(nil) if Model.whitelist_attributes
|
child.send :initialize_mass_assignment_sanitizer if self == Base
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,6 +28,11 @@ module ActiveRecord
|
||||||
default << 'id' unless primary_key.eql? 'id'
|
default << 'id' unless primary_key.eql? 'id'
|
||||||
default
|
default
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def initialize_mass_assignment_sanitizer
|
||||||
|
attr_accessible(nil) if Model.whitelist_attributes
|
||||||
|
self.mass_assignment_sanitizer = Model.mass_assignment_sanitizer if Model.mass_assignment_sanitizer
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Allows you to set all the attributes at once by passing in a hash with keys
|
# Allows you to set all the attributes at once by passing in a hash with keys
|
||||||
|
|
|
@ -278,6 +278,38 @@ class MassAssignmentSecurityTest < ActiveRecord::TestCase
|
||||||
ActiveRecord::Model.whitelist_attributes = prev
|
ActiveRecord::Model.whitelist_attributes = prev
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "ActiveRecord::Model.mass_assignment_sanitizer works for models which include Model" do
|
||||||
|
begin
|
||||||
|
sanitizer = Object.new
|
||||||
|
prev, ActiveRecord::Model.mass_assignment_sanitizer = ActiveRecord::Model.mass_assignment_sanitizer, sanitizer
|
||||||
|
|
||||||
|
klass = Class.new { include ActiveRecord::Model }
|
||||||
|
assert_equal sanitizer, klass._mass_assignment_sanitizer
|
||||||
|
|
||||||
|
ActiveRecord::Model.mass_assignment_sanitizer = nil
|
||||||
|
klass = Class.new { include ActiveRecord::Model }
|
||||||
|
assert_not_nil klass._mass_assignment_sanitizer
|
||||||
|
ensure
|
||||||
|
ActiveRecord::Model.mass_assignment_sanitizer = prev
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ActiveRecord::Model.mass_assignment_sanitizer works for models which inherit Base" do
|
||||||
|
begin
|
||||||
|
sanitizer = Object.new
|
||||||
|
prev, ActiveRecord::Model.mass_assignment_sanitizer = ActiveRecord::Model.mass_assignment_sanitizer, sanitizer
|
||||||
|
|
||||||
|
klass = Class.new(ActiveRecord::Base)
|
||||||
|
assert_equal sanitizer, klass._mass_assignment_sanitizer
|
||||||
|
|
||||||
|
sanitizer2 = Object.new
|
||||||
|
klass.mass_assignment_sanitizer = sanitizer2
|
||||||
|
assert_equal sanitizer2, Class.new(klass)._mass_assignment_sanitizer
|
||||||
|
ensure
|
||||||
|
ActiveRecord::Model.mass_assignment_sanitizer = prev
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue