Sha256: 21267226cfc6deb7d398d8cf19206e91281ab2bad088d6ed2c5dd29f96912f17

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# coding: utf-8
module ActsAsFootprintable
  module Footprinter
    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

    def leave_footprints(footprintable)
      footprint = ActsAsFootprintable::Footprint.new(:footprintable => footprintable, :footprinter => self)
      if footprint.save
        true
      else
        false
      end
    end

    def access_histories_for(klass, limit=nil)
      get_access_history_records(limit) do
        footprints.for_type(klass).group('footprintable_id').having('MAX(created_at)').pluck(:id)
      end
    end

    def access_histories(limit=nil)
      get_access_history_records(limit) do
        footprints.group('footprintable_id, footprintable_type').having('MAX(created_at)').pluck(:id)
      end
    end

    private
    def get_access_history_records(limit=nil)
      records = footprints.where(:id => yield).order("footprints.created_at desc")
      records = records.limit(limit) unless limit.nil?
      records.map{|footprint| footprint.footprintable}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_footprintable-0.1.0 lib/acts_as_footprintable/footprinter.rb