Table locking in AttrLocked

As promised, AttrLocked has been updated to include table locking. If you have any tables in your app that you want to be read-only, just put this in your model:

class SomeReadOnlyTable < ActiveRecord::Base

That will stop and save/update/delete operations on the model’s table, and you won’t be able to modify the attributes of any instances of it, even new unsaved instances. You can still associate other models with your locked one, as long as the foreign key field itself is not locked in some way.

AttrLocked updated

I’ve just made an update to AttrLocked. The initial release allowed you to carry on setting locked attributes using the record[:attr] = value style. Now, both that and record.attr = value are disabled on locked attributes. I may add a feature that lets you lock down a whole table so it’s read-only, and you cannot create, delete or modify records from it. More on that when it happens.

New Rails plugin: AttrLocked

I just released a new plugin for Rails, called AttrLocked. It lets you specify that certain model attributes should not be changeable once a record has been created – perfect for making sure usernames are fixed or your financial data doesn’t get tampered with. It will not let you change a record’s locked attributes in any way, and will disable form fields as appropriate to stop people bothering to change the data. Example:

class Payment < ActiveRecord::Base
  attr_locked :time, :amount, :transaction_id

Now, when you grab a record from the database, using time=, amount= and transaction_id= won’t work. attributes=, save and update_attributes will silently ignore the locked attributes, and update_attribute will return false if you try to modify a locked attribute. Naturally, you are allowed to set values on new records.

I’ve only tested it on my current project, which is in Rails 1.2.2, but I’d really appreciate testing with other applications. Install like so:

script/plugin install