Giter Site home page Giter Site logo

activerecord-be_readonly's Introduction

Be Readonly for ActiveRecord 3.x/4.x/5.x and Rails 3.x/4.x/5.x

Makes any ActiveRecord model become read-only in Rails with:

be_readonly

There are other gems that provide similar behavior, but from what I saw, they did either too much or too little. This one is just right, and be_readonly couldn't be much shorter to type and read without just being "readonly", but readonly would be too easily confused with related instance methods in ActiveRecord.

What it does

Adding this gem to you bundle will add a class method called be_readonly on each model when Rails loads via a railtie.

If be_readonly is called on the class, it defines the class methods: delete and delete_all and the instance methods: before_create, before_save, before_destroy, update_all, and delete to all raise a ActiveRecord::ReadOnlyRecord error. It also defines the instance method readonly? to return true, which disables a lot that tries to create and update in ActiveRecord.

Setup

In your ActiveRecord/Rails 3.0+ project, add this to your Gemfile:

gem 'activerecord-be_readonly'

For the development version:

gem 'activerecord-be_readonly', :git => 'git://github.com/garysweaver/activerecord-be_readonly.git'

Then run:

bundle install

Usage

In your model just include this, preferably somewhere noticeable like at the top:

be_readonly

Using without Rails

Or if you are off-rails, but use ActiveRecord, just do this in the model class:

include BeReadonly::Now

Configuration

You can create an initializer (/config/initializers/activerecord-be_readonly.rb) with the following options:

To turn off read-only application-wide, set the enabled setting to false (useful if you'd like to turn off read-only only for tests, etc.)

BeReadonly.enabled = false    # Default: true

To disable read-only for record creation application-wide, set the create_allowed setting to true

BeReadonly.create_allowed = true    # Default: false

Contributors

Thanks to:

License

Copyright (c) 2012 Gary S. Weaver, released under the MIT license.

activerecord-be_readonly's People

Contributors

garysweaver avatar koic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

activerecord-be_readonly's Issues

[feature request] ORM agnosticism

Should be easy to detect ORM and just use different error class, but of course, the gem's name should be changed to something like "activemodel-bereadonly".

Project status

Hi @garysweaver,

There are a couple of PRs opened, and Rails 6 will be a barrier for many who want to keep using it.
Can we get those PRs merged and new gem cut please?

More generally, is this project still maintained?

Thanks!

Is there any plan to support test mode?

Thanks for the convenient library. I am using it and it works very well but only I can not find a way to disable it under some special cases, e.g. generating testing data for testing. Is it possible to allow it works normally under development but does nothing under test.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.