Class: PagesCore::ArchiveFinder
- Inherits:
-
Object
- Object
- PagesCore::ArchiveFinder
- Defined in:
- lib/pages_core/archive_finder.rb
Instance Method Summary collapse
- #by_year(year) ⇒ Object
- #by_year_and_maybe_month(year, month = nil) ⇒ Object
- #by_year_and_month(year, month) ⇒ Object
-
#initialize(relation, options = {}) ⇒ ArchiveFinder
constructor
A new instance of ArchiveFinder.
- #latest_year ⇒ Object
- #latest_year_and_month ⇒ Object
- #months_in_year(year) ⇒ Object
- #months_in_year_with_count(year) ⇒ Object
- #timestamp_attribute ⇒ Object
- #years ⇒ Object
- #years_with_count ⇒ Object
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, = {}) @relation = relation @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_year ⇒ Object
24 25 26 |
# File 'lib/pages_core/archive_finder.rb', line 24 def latest_year latest_year_and_month&.first end |
#latest_year_and_month ⇒ Object
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[].year, record[].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_attribute ⇒ Object
46 47 48 |
# File 'lib/pages_core/archive_finder.rb', line 46 def @options[:timestamp] || :created_at end |
#years ⇒ Object
50 51 52 |
# File 'lib/pages_core/archive_finder.rb', line 50 def years select_years(@relation).map(&:to_i).sort end |
#years_with_count ⇒ Object
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 |