How can I set a model to be read-only every time that it is accessed if an attribute within the same model is set to true?
I have looked everywhere and the model read only seems to have very little documentation and even web results.
Edit (Additional Info): I have two methods in my Model (application.rb) - not in private
def lock()
self.locked = true
save(validate: false)
end
def unlock()
self.locked = false
save(validate: false)
end
I call them from my applications controller on update with:
if params[:application][:locked] == false
@application.unlock
return
elsif params[:application][:locked] == true
@application.lock
return
end
and in the Model (application.rb) I have - not in private:
def readonly?
locked == true
end
Updated:
Notice that I added a
belongs_to
association there because you'll most likely need this because yourApplication
as you said is actually already a normal model anyway. If you do not have this association, and are setting thelocked
internally as a class instance variable of yourApplication
class (i.e. you have@locked
class instance variable), then (depending on your requirements), you'll have problems with 1) persistency because each request (per different process/server) will default tolocked = nil
(which might or might not be a problem to you), and also 2) concurrency because threads share the value of this class instance variable, which means that simultaneous requests would need this@locked
value be evaluated independently; which becomes potentially dangerous if@locked
is set totrue
in one thread, while on another@locked
is overidden and is set tofalse
. But if these are not a problem, I can still update my answer to not usebelongs_to :application
; let me know.