Better mass assignment with an attribute filter abstraction.
AttributeFilter is built to be extensible. You can sanitize attributes with a built-in or custom strategy. We provide whitelist and blacklist sanitization strategies, similar to Rails' attr_accessible and attr_protected.
Additionally you can respond to when sanitization occurs via a built-in or custom listener.
Add this line to your application's Gemfile:
gem 'attribute_filter'
And then execute:
$ bundle
Or install it yourself as:
$ gem install attribute_filter
class UserAttributeFilter < AttributeFilter::Filter
strategy :white_list,
white_list: [:first_name, :last_name, :login, :password, :password_confirmation]
listener :logger
listener :exceptional
end
User.new({protected_attribute: "blah", first_name: "John"},
filter: UserAttributeFilter)
It's easy to define your own sanitization strategy
class MyStrategy < AttributeFilter::Strategy
def sanitize(attributes = {})
#build your sanitization implementation here
end
end
AttributeFilter.add_strategy(:mine, MyStrategy)
Listeners are fired when attributes are sanitized from a mass assignment call.
class MyListener < AttributeFilter::SanitizationListener
def attributes_sanitized(old_attributes, new_attributes)
#respond to sanitization your own way here
end
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request