Sha256: c48997877af464af6fe859bb74afd6397f943c9e082824940bc1486ddc30b248

Contents?: true

Size: 936 Bytes

Versions: 3

Compression:

Stored size: 936 Bytes

Contents

module Monocle
  class View < ActiveRecord::Base
	  belongs_to :viewable, :polymorphic => true

	  validates_presence_of :viewable_id
	  validates_presence_of :viewable_type

	  def self.by_type(type)
	    where(:type => type)
    end

    def self.since(since, options = {})
      options[:limit] ||= 10
      options[:order] ||= "SUM(views) DESC"
      options[:type] ||= :yearly

      by_type("Monocle::" + options[:type].to_s.classify + "View")
      .where("viewed_on_start_date >= ?", Date.today - since)
      .group("viewable_type, viewable_id")
      .order(options[:order])
      .limit(options[:limit])
    end

    def self.all_time(options = {})
      options[:limit] ||= 10
      options[:order] ||= "views DESC"

      by_type("Monocle::OverallView")
      .group("viewable_type, viewable_id")
      .order(options[:order])
      .limit(options[:limit])
    end

    def view!
      increment!(:views)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
monocle-0.1.1 lib/monocle/view.rb
monocle-0.1.0 lib/monocle/view.rb
monocle-0.0.1 lib/monocle/view.rb