About attr_readonly, attr_protected, attr_accessible as instance methods

Hello all,

my first post to Rails Core - so please bear with me if I missed something obvious, this once. :slight_smile:

I followed the discussion about attr_readonly being somewhat inconsistent with interest. I am currently involved in a project where - I think - making attr_readonly and/or attr_protected an instance method of ActiveRecord::Base, instead of (or in addition to) a class method, would make absolute sense, and I'm wondering what one would need to do to achieve this.

Rationale: there are quite a few cases where some attributes should not be changeable, depending on some condition. For example:

class User < AR:Base   attr_protected :firstname, :lastname, :gender_id, :birthdate, :if => :passport_verified? end

Bonus: a FormBuilder can read these attributes from the live @object and automatically set form fields 'disabled' if they are protected or read only, but need to be displayed. This is exactly what I'm trying to achieve, and this is why simple validations don't work: I need the status of the attribute "live", not only upon saving. Plus, there might be several instances of one object in memory (eg. @user, @admin) and in this case they'd need separate values for attr_protected.

The "if" and "unless" option already exist in validations, so I took my time digging through Rails core (and learnt quite a bit), however I'm not quite sure

- if this makes any sense at all (or maybe my idea is even easier implemented another way?) - if this has been done elsewhere (I've been googling, but nothing found so far) - how to proceed.

Also, now that I've already spent some nights in front of the Rails source, somebody will probably say "oh, all you've got to do is $three_liner" ...

But nevertheless, any suggestions would be highly appreciated!

Thanks,

Jens