Class: Panda::CMS::Visit

Inherits:
ApplicationRecord show all
Defined in:
app/models/panda/cms/visit.rb

Class Method Summary collapse

Class Method Details

Returns the most popular pages by visit count

Parameters:

  • limit (Integer) (defaults to: 10)

    Number of pages to return (default: 10)

  • period (ActiveSupport::Duration) (defaults to: nil)

    Time period to consider (default: all time)

Returns:

  • (Array<Hash>)

    Array of hashes with page and visit_count



14
15
16
17
18
19
20
21
22
23
# File 'app/models/panda/cms/visit.rb', line 14

def self.popular_pages(limit: 10, period: nil)
  scope = joins(:page).where.not(page_id: nil)
  scope = scope.where("visited_at >= ?", period.ago) if period

  scope
    .group("panda_cms_pages.id", "panda_cms_pages.title", "panda_cms_pages.path")
    .select("panda_cms_pages.id, panda_cms_pages.title, panda_cms_pages.path, COUNT(*) as visit_count")
    .order("visit_count DESC")
    .limit(limit)
end