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.



5
6
7
8
# File 'lib/pages_core/archive_finder.rb', line 5

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

Instance Method Details

#by_year(year) ⇒ Object



10
11
12
# File 'lib/pages_core/archive_finder.rb', line 10

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

#by_year_and_maybe_month(year, month = nil) ⇒ Object



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

def by_year_and_maybe_month(year, month = nil)
  return by_year(year) unless month

  by_year_and_month(year, month)
end

#by_year_and_month(year, month) ⇒ Object



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

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

#latest_yearObject



24
25
26
# File 'lib/pages_core/archive_finder.rb', line 24

def latest_year
  latest_year_and_month&.first
end

#latest_year_and_monthObject



28
29
30
31
32
33
# File 'lib/pages_core/archive_finder.rb', line 28

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



35
36
37
# File 'lib/pages_core/archive_finder.rb', line 35

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

#months_in_year_with_count(year) ⇒ Object



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

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

#timestamp_attributeObject



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

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

#yearsObject



50
51
52
# File 'lib/pages_core/archive_finder.rb', line 50

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

#years_with_countObject



54
55
56
# File 'lib/pages_core/archive_finder.rb', line 54

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