Class: ArchivesSidebar

Inherits:
Sidebar
  • Object
show all
Defined in:
app/models/archives_sidebar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sidebar

#admin_state, apply_staging_on_active!, #content_partial, description, #description, #display_name, display_name, #fieldmap, #fields, #html_id, ordered_sidebars, path_name, #publish, purge, setting, #short_name, short_name, #to_locals_hash

Instance Attribute Details

#archivesObject

Returns the value of attribute archives.



9
10
11
# File 'app/models/archives_sidebar.rb', line 9

def archives
  @archives
end

Class Method Details

.date_funcsObject



11
12
13
14
15
16
17
18
19
# File 'app/models/archives_sidebar.rb', line 11

def self.date_funcs
  @date_funcs ||=
    if Content.connection.class.name.include?("SQLite3Adapter")
      ["strftime('%Y', published_at) as year", "strftime('%m', published_at) as month"]
    else
      ["extract(year from published_at) as year",
       "extract(month from published_at) as month"]
    end
end

Instance Method Details

#parse_request(_contents, _params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/archives_sidebar.rb', line 21

def parse_request(_contents, _params)
  # The original query that was here instantiated every article and every
  # tag, and then sorted through them just to do a 'group by date'.
  # Unfortunately, there's no universally-supported way to do this
  # across all three of our supported DBs.  So, we resort to a bit of
  # DB-specific code.
  date_funcs = self.class.date_funcs

  article_counts = Article.published.select("count(*) as count", *date_funcs)
    .group(:year, :month).reorder("year desc", "month desc").limit(count.to_i)

  @archives = article_counts.map do |entry|
    month = entry.month.to_i
    year = entry.year.to_i
    {
      name: I18n.l(Date.new(year, month), format: "%B %Y"),
      month: month,
      year: year,
      article_count: entry.count
    }
  end
end