class Activity < ApplicationRecord belongs_to :user validates :action, :resource_type, presence: true scope :recent, -> { order(created_at: :desc) } scope :for_user, ->(user) { where(user_id: user.id) } def self.log(user, action, resource_type, resource_id = nil, details = {}) create( user_id: user.id, action: action, resource_type: resource_type, resource_id: resource_id, details: details.to_json ) end def details_hash return {} if details.blank? JSON.parse(details) end end