Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read only mode for Ruby on Rails application

I have an interactive Ruby on Rails application which I would like to put into a "read only mode" during certain times. This will allow users to read the data they need but block them from actions that write to the database.

One way to do this would be to have a true/false variable located in the database that was checked before any write was made.

My question. Is there a more elegant solution for this problem out there?

like image 684
Nicolo77 Avatar asked Aug 02 '10 21:08

Nicolo77


3 Answers

If you really want to prevent any database write, the easiest way I can imagine would be to override the readonly? method of a model to always return true, either in selected models or maybe even for all ActiveRecord models. If a model is set to readonly (normally done by calling #readonly! on it), any try to save the record will raise an ActiveRecord::ReadOnlyRecord error.

module ActiveRecord
  class Base
    def readonly?
      true
    end
  end
end

(actually untested code, but you get the idea…)

like image 109
Zargony Avatar answered Nov 17 '22 23:11

Zargony


Another good one which I liked a little better is Declarative Authorization which is covered by Railscasts as well: Railscasts - Declarative Authorization

like image 2
nuclearsandwich Avatar answered Nov 17 '22 22:11

nuclearsandwich


Permissions plugin? Something simple like cancan where you define what a user can do, when. It will allow you to display links, or not, and restrict access to controller actions. The railscast will explain better than I can.

http://github.com/ryanb/cancan

http://railscasts.com/episodes/192-authorization-with-cancan

like image 2
mark Avatar answered Nov 17 '22 21:11

mark