Ruby on Rails
Friday, January 26, 2018
Here is the code snippet.
On Friday, January 26, 2018 at 6:36:50 PM UTC+5:30, Prince Bansal wrote:
-- class Attendance < ApplicationRecord
ATTENDANCE_EDITABLE_VALIDITY = 7
before_save :is_editable?
belongs_to :attendance_detail
def is_editable? errors.add(:attendance, 'modification is not allowed.') if attendance_detail.created_at.present? && (Time.zone.now.to_date - attendance_detail.created_at.to_date).to_i >= ATTENDANCE_EDITABLE_VALIDITY unless current_user.super_admin? || current_user.admin? errors.empty? endend
I have lots of conditional callbacks based on current_user in AR.
I do not want to move these conditions in controller. As there are lot of other conditions that are user permission specific.On Friday, January 26, 2018 at 6:36:50 PM UTC+5:30, Prince Bansal wrote:
Hello Guys,I faced a situation in Rails that I need current_user method inside model. There is a condition that I do not have any column that is associatedwith current_user in model. I do not want to declare virtual attribute in each activerecord model also do not want to set it in global variable. How can I achieve this?Your help will be highly appreciated.Thanks.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/06d886de-2b66-46ac-b71d-7d37981a194a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment