Skip to content

codez/protective

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Protect active records from being destroyed in a declarative way.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages