Sha256: 394226c608a9994ce4e2db8c5749c6745d6dfad8fe15a6c4585062fb63389082

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module BrowsingHistory::Browser
  def browsing_histories
    @browsing_histories ||= Association.new(self)
  end

  def self.included(klass)
    klass.include IdentifyMethods
    klass.extend  IdentifyMethods
  end

  module IdentifyMethods
    def browser_id
      browser_instance? ? id : nil
    end

    def browser_type
      browser_instance? ? self.class.to_s : to_s
    end

    def browser_class
      browser_instance? ? self.class : self
    end

    def browser_instance?
      !instance_of?(Object)
    end
  end

  class Association
    def initialize(browser)
      @browser = browser
    end

    def recent(historizable, limit: 20)
      BrowsingHistory::History.where(
        browser: @browser,
        historizable: historizable,
        limit: limit
      )
    end

    def previous(historizable, between, limit: 20)
      BrowsingHistory::History.where(
        browser: @browser,
        historizable: historizable,
        between: between,
        limit: limit
      )
    end

    def add(historizable)
      BrowsingHistory::History.create(
        browser: @browser,
        historizable: historizable
      )
    end
    alias << add

    def count(historizable)
      BrowsingHistory::History.count(
        browser: @browser,
        historizable: historizable
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
browsing_history-0.0.3 lib/browsing_history/browser.rb
browsing_history-0.0.2 lib/browsing_history/browser.rb