Class: PagesCore::ArchiveFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/pages_core/archive_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(relation, options = {}) ⇒ ArchiveFinder

Returns a new instance of ArchiveFinder.



3
4
5
6
# File 'lib/pages_core/archive_finder.rb', line 3

def initialize(relation, options = {})
  @relation = relation
  @options = options
end

Instance Method Details

#by_year(year) ⇒ Object



8
9
10
# File 'lib/pages_core/archive_finder.rb', line 8

def by_year(year)
  filter_by_time(range_for_year(year))
end

#by_year_and_month(year, month) ⇒ Object



12
13
14
# File 'lib/pages_core/archive_finder.rb', line 12

def by_year_and_month(year, month)
  filter_by_time(range_for_year_and_month(year, month))
end

#latest_yearObject



16
17
18
# File 'lib/pages_core/archive_finder.rb', line 16

def latest_year
  latest_year_and_month&.first
end

#latest_year_and_monthObject



20
21
22
23
24
25
# File 'lib/pages_core/archive_finder.rb', line 20

def latest_year_and_month
  ordered_relation.first.try do |record|
    [record[timestamp_attribute].year,
     record[timestamp_attribute].month]
  end
end

#months_in_year(year) ⇒ Object



27
28
29
# File 'lib/pages_core/archive_finder.rb', line 27

def months_in_year(year)
  select_months(by_year(year)).map(&:to_i).sort
end

#months_in_year_with_count(year) ⇒ Object



31
32
33
34
35
36
# File 'lib/pages_core/archive_finder.rb', line 31

def months_in_year_with_count(year)
  group_by_month(by_year(year))
    .count
    .to_a
    .sort_by(&:first)
end

#timestamp_attributeObject



38
39
40
# File 'lib/pages_core/archive_finder.rb', line 38

def timestamp_attribute
  @options[:timestamp] || :created_at
end

#yearsObject



42
43
44
# File 'lib/pages_core/archive_finder.rb', line 42

def years
  select_years(@relation).map(&:to_i).sort
end

#years_with_countObject



46
47
48
# File 'lib/pages_core/archive_finder.rb', line 46

def years_with_count
  years.map { |year| [year, by_year(year).count] }
end