2012-07-13 04:51:13 -04:00
|
|
|
module ActiveModel
|
2012-09-20 12:47:32 -04:00
|
|
|
# Raised when forbidden attributes are used for mass assignment.
|
|
|
|
#
|
|
|
|
# class Person < ActiveRecord::Base
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# params = ActionController::Parameters.new(name: 'Bob')
|
|
|
|
# Person.new(params)
|
|
|
|
# # => ActiveModel::ForbiddenAttributesError
|
|
|
|
#
|
|
|
|
# params.permit!
|
|
|
|
# Person.new(params)
|
|
|
|
# # => #<Person id: nil, name: "Bob">
|
2012-08-13 01:41:04 -04:00
|
|
|
class ForbiddenAttributesError < StandardError
|
2012-07-13 04:51:13 -04:00
|
|
|
end
|
|
|
|
|
2012-09-20 13:14:29 -04:00
|
|
|
module ForbiddenAttributesProtection # :nodoc:
|
|
|
|
protected
|
2012-11-08 20:31:23 -05:00
|
|
|
def sanitize_for_mass_assignment(attributes)
|
2015-10-02 04:26:16 -04:00
|
|
|
if attributes.respond_to?(:permitted?)
|
|
|
|
raise ActiveModel::ForbiddenAttributesError if !attributes.permitted?
|
|
|
|
attributes.to_h
|
2012-09-20 13:14:29 -04:00
|
|
|
else
|
|
|
|
attributes
|
|
|
|
end
|
2012-07-13 04:51:13 -04:00
|
|
|
end
|
2014-08-06 20:17:19 -04:00
|
|
|
alias :sanitize_forbidden_attributes :sanitize_for_mass_assignment
|
2012-07-13 04:51:13 -04:00
|
|
|
end
|
|
|
|
end
|