Sha256: 596a6f36420f19f88c85e0a9dea80ad3852227d9ea982ae7f6f0f9923a063b08

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

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_func
    @date_func ||=
      begin
        if Content.connection.is_a?(ActiveRecord::ConnectionAdapters::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
      rescue NameError
        '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_func = self.class.date_func

    article_counts = Content.find_by_sql(["select count(*) as count, #{date_func} from contents where type='Article' and published = ? and published_at < ? group by year,month order by year desc,month desc limit ? ", true, Time.now, 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

6 entries across 6 versions & 1 rubygems

Version Path
publify_core-9.0.0.pre6 app/models/archives_sidebar.rb
publify_core-9.0.0.pre5 app/models/archives_sidebar.rb
publify_core-9.0.0.pre4 app/models/archives_sidebar.rb
publify_core-9.0.0.pre3 app/models/archives_sidebar.rb
publify_core-9.0.0.pre2 app/models/archives_sidebar.rb
publify_core-9.0.0.pre1 app/models/archives_sidebar.rb