module ActsAsFootprintable::Footprinter

Public Class Methods

included(base) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 5
def self.included(base)
  base.class_eval do
    has_many :footprints, class_name: 'ActsAsFootprintable::Footprint', as: :footprinter, dependent: :destroy do
      def footprintable
        includes(:footprintable).map(&:footprintable)
      end
    end
  end
end

Public Instance Methods

access_histories(limit = nil) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 24
def access_histories(limit = nil)
  get_access_history_records(footprints, limit)
end
access_histories_for(klass, limit = nil) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 20
def access_histories_for(klass, limit = nil)
  get_access_history_records(footprints.for_type(klass), limit)
end
footprintable() click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 8
def footprintable
  includes(:footprintable).map(&:footprintable)
end
leave_footprints(footprintable) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 15
def leave_footprints(footprintable)
  footprint = ActsAsFootprintable::Footprint.new(footprintable: footprintable, footprinter: self)
  footprint.save
end

Private Instance Methods

get_access_history_records(target, limit = nil) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 30
def get_access_history_records(target, limit = nil)
  footprints.where(id: recent_footprint_ids(target, limit)).order('created_at DESC')
end
recent_footprint_ids(target, limit = nil) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 38
    def recent_footprint_ids(target, limit = nil)
      records = footprints.where(<<~SQL)
        (
          #{table_name}.footprintable_id,
          #{table_name}.footprintable_type,
          #{table_name}.created_at
        ) IN (#{recent_footprints_by(target).to_sql})
      SQL
      records = records.order('footprints.created_at desc')
      records = records.limit(limit) unless limit.nil?
      records.ids
    end
recent_footprints_by(target) click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 51
def recent_footprints_by(target)
  target.group(
    "#{table_name}.footprintable_id",
    "#{table_name}.footprintable_type"
  ).select(
    "#{table_name}.footprintable_id",
    "#{table_name}.footprintable_type",
    "MAX(#{table_name}.created_at) AS created_at"
  )
end
table_name() click to toggle source
# File lib/acts_as_footprintable/footprinter.rb, line 34
def table_name
  ActsAsFootprintable::Footprint.table_name
end