Sha256: cbff3b07d7ef07b6e431f8543afd5fbea66df6aad8da881452b0cefba25f755b

Contents?: true

Size: 1.48 KB

Versions: 21

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

class ArchivesSidebar < Sidebar
  description "Displays links to monthly archives"
  setting :title, "Archives"
  setting :show_count, true, label: "Show article counts", input_type: :checkbox
  setting :count, 10, label: "Number of Months"

  attr_accessor :archives

  def self.date_funcs
    @date_funcs ||=
      if /SQLite3Adapter/.match?(Content.connection.class.name)
        ["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

  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
end

SidebarRegistry.register_sidebar ArchivesSidebar

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
HornsAndHooves-publify_core-10.5.0 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.4.0 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.3.0 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.2.0 app/models/archives_sidebar.rb
publify_core-9.2.10 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.1.1 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.1.0 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.0.3 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.0.2 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.0.1 app/models/archives_sidebar.rb
HornsAndHooves-publify_core-10.0.0 app/models/archives_sidebar.rb
publify_core-9.2.9 app/models/archives_sidebar.rb
publify_core-9.2.8 app/models/archives_sidebar.rb
publify_core-9.2.7 app/models/archives_sidebar.rb
publify_core-9.2.6 app/models/archives_sidebar.rb
publify_core-9.2.5 app/models/archives_sidebar.rb
publify_core-9.2.4 app/models/archives_sidebar.rb
publify_core-9.2.3 app/models/archives_sidebar.rb
publify_core-9.2.2 app/models/archives_sidebar.rb
publify_core-9.2.1 app/models/archives_sidebar.rb