Created
April 27, 2017 14:37
-
-
Save ryenski/eaba6e2297ca154693fe036f446c9525 to your computer and use it in GitHub Desktop.
An ActiveRecord model that stores the tracked changes for our action auditor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: activities | |
# | |
# id :uuid not null, primary key | |
# user_id :uuid | |
# tenant_id :uuid | |
# contact_id :uuid | |
# trackable_type :string | |
# trackable_id :uuid | |
# action :string | |
# tracked_changes :json | |
# detail :string | |
# visibility :integer default("report") | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
# Foreign Keys | |
# | |
# fk_rails_4f81fb35a0 (contact_id => contacts.id) | |
# fk_rails_7e11bb717f (user_id => contacts.id) | |
# fk_rails_fd961d739b (tenant_id => tenants.id) | |
# | |
class Activity < ApplicationRecord | |
belongs_to :tenant | |
belongs_to :user # who did it | |
belongs_to :contact # whose page should this show up on? | |
belongs_to :trackable, -> { unscope(where: :trashed_at) }, polymorphic: true | |
scope :archived, -> {where(action: 'archived')} | |
scope :archived_or_trashed, -> {where(action: %w(archived trashed))} | |
store_accessor :tracked_changes | |
scope :created, -> {where(action: 'create')} | |
after_initialize :set_defaults | |
def set_defaults | |
if self.new_record? | |
self.tenant_id ||= Tenant.current_id | |
self.user_id ||= User.current_id | |
end | |
end | |
# visibility | |
# * report: (default) Only shows up in system reports (e.g. login activity, etc. ) | |
# * member: Only the member can see | |
# * members_only: (default) Only logged in members can see | |
# * everyone: anyone can see, logged in or not | |
enum visibility: [:report, :member, :members_only, :everyone] | |
def summary | |
[action, trackable_type].join(' ') | |
end | |
alias_method :to_s, :summary | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment