Sha256: 758c99fbcbc9accc69de5367195ab81b2605e69115d7c9e8fc98bf655cf316a0

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'multi_json'

class JSONWithIndifferentAccess
  def self.load(str)
    return str unless str

    obj = HashWithIndifferentAccess.new(MultiJson.load(str))
    obj.freeze
    obj
  end

  def self.dump(obj)
    MultiJson.dump(obj)
  end
end

module Stance
  class EventRecord < ActiveRecord::Base
    belongs_to :subject, polymorphic: true

    scope :active, -> { where dismissed_at: nil }
    scope :dismissed, -> { where.not dismissed_at: nil }

    def self.table_name_prefix
      'stance_'
    end

    def self.dismiss_all
      active.each(&:dismiss)
    end

    def to_s
      name
    end

    def event_class_name
      @event_class_name ||= "#{subject.model_name.name}Events::#{name.tr('.', '/').classify}"
    end

    def event_class
      @event_class ||= event_class_name.constantize
    end

    def dismissed?
      dismissed_at.present?
    end

    def active?
      !dismissed?
    end

    def dismiss
      update_attribute :dismissed_at, Time.current
    end

    unless connection.adapter_name =~ /postg|mysql/i
      serialize :metadata, ::JSONWithIndifferentAccess
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stance-0.4.0 app/models/stance/event_record.rb
stance-0.3.0 app/models/stance/event_record.rb
stance-0.2.0 app/models/stance/event_record.rb
stance-0.1.0 app/models/stance/event_record.rb