Protect active records from being destroyed in a declarative way.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
rails
test
.document
.gitignore
.travis.yml
Gemfile
Gemfile.lock
LICENSE.txt
README.rdoc
Rakefile
VERSION

README.rdoc

protective

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

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