Project

protective

0.0
No commit activity in last 3 years
No release in over 3 years
Protect records from being destroyed in a declarative way. Simply add 'protect_if :dont_delete, "Record is marked as not deletable"' to your ActiveRecord model. Your record will no be destroyed and the given message is added to the errors object.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

protective¶ ↑

<img src=“https://secure.travis-ci.org/codez/protective.png” />

In your ActiveRecord models, you used to protect the instances from being destroyed as follows:

before_destroy :protect_if_has_attachments

def protect_if_has_attachments
  unless attachments.empty?
    errors.add(:base, "Record has attachments and cannot be destroyed")
    false
  end
end

With protective, this is reduced to:

protect_if :attachments, "Record has attachments and cannot be destroyed"

Of course, you may pass the name of any defined method. If its result evaluates to present?, the record is not going to be destroyed. No exception is thrown, but a message is added to the errors object of your model. This may then be used further on.

To use protective, simply add it to your Gemfile:

gem 'protective'

Copyright © 2011-2017 Pascal Zumkehr. See LICENSE.txt for further details.