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.



7
8
9
# File 'app/models/archives_sidebar.rb', line 7

def archives
  @archives
end

Class Method Details

.date_funcsObject



9
10
11
12
13
14
15
16
# File 'app/models/archives_sidebar.rb', line 9

def self.date_funcs
  @date_func ||=
    if Content.connection.class.name =~ /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



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

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