aboutsummaryrefslogtreecommitdiffstats
path: root/app/decorators/controllers/refinery/pages_controller_decorator.rb
blob: 6754fe4478f5028d70186211b1fb8e028e8a1be4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# This file is part of hmnoweb, a RefineryCMS based Webapp for heavymetal.no
# Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

ApplicationController.class_eval do
  include Refinery::Blog::ControllerHelper

  before_filter :populate_sidebars
  before_filter :populate_home_page, :only => [:home]

  protected

    def populate_sidebars
      @sidebar_modules = []

      Refinery::Blog::Category.all.each do |c|
        @sidebar_modules << SidebarBlogCategory.new(c)
      end

      #
      # The blog categories and tags are listed in the right sidebar
      # on every page.
      #
      find_all_blog_categories
      find_tags
    end

    def populate_home_page
      @posts = Refinery::Blog::Post
               .live
               .includes(:comments, :categories)
               .where(:refinery_blog_categories => { :sidebar_position => 0 })
               .page(params[:page])
    end

    # Override Refinery::Blog::ControllerHelper.find_tags
    #
    # Limit the number of tags to show:
    # This should show the 50 most popular tags from the past 30 days.
    # I think this is a fair compromise between most popular and recent.
    def find_tags
      opts = {
        :order => 'count DESC',
        :start_at => 30.days.ago,
        :limit => 50
      }

      @tags = Refinery::Blog::Post.tag_counts_on(:tags, opts)
      if @tags.empty?
        opts.delete(:start_at)
        @tags = Refinery::Blog::Post.tag_counts_on(:tags, opts)
      end
    end
end