model_guard

Gem Version Build Status Gem Downloads Maintainability

Two small concerns for ActiveRecord that can guard against direct creates, updates and destroys. Handy if you want to force the use of directing all persistence through service objects and guard against accidental CRUD operations in the console.

Installation

Include the gem in your Gemfile:

gem "model_guard"

Usage

Create / update protection

Protects against #save, #save!, #update, #update!. Instead use #guarded_save, #guarded_save!, #guarded_update, #guarded_update! or #guarded_create.

class User < ActiveRecord::Base
  include ModelGuard::CreateOrUpdateGuard
end

CreateGuardedUser.guarded_create(name: "Joe")
user.guarded_update(name: "Joe")

or

user.allow_direct_create_or_update!
user.save

Destroy protection

class User < ActiveRecord::Base
  include ModelGuard::DestroyGuard
end

Will raise an exception when calling destroy on an instance of User. Instead use guarded_destroy or guarded_destroy!.

user.guarded_destroy
user.guarded_destroy!

Support

Submit suggestions or feature requests as a GitHub Issue or Pull Request (preferred). If you send a pull request, remember to update the corresponding unit tests and/or add new tests.