Module: C::BlogsHelper

Included in:
FrontEnd::BlogsController
Defined in:
app/helpers/c/blogs_helper.rb

Instance Method Summary collapse

Instance Method Details

#archive_blogs(limit = nil) ⇒ Object

Call in the controller to allow usage of blog_archive_menu and blog_archive_month_submenu Returns the collection of blogs filtered by the params Optionally accepts a limit for the size of the returned collection



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
# File 'app/helpers/c/blogs_helper.rb', line 15

def archive_blogs limit = nil
  @blog_archive_filter_years, @blog_archive_filter_months = [[], []]

  year = params[:year] || Time.now.year
  if params[:month].to_i > 0 && params[:month].to_i < 13
    month = params[:month]
  else
    month = Time.now.month
    params[:month] = nil
  end

  if C::Blog.any?
    age_limit = C::Blog.order(:created_at).limit(1).first&.created_at.beginning_of_year
    num_years = Time.now.year - age_limit.year
    @blog_archive_filter_years = (age_limit.year..(age_limit.year + num_years)).to_a.reverse
    @blog_archive_filter_years.select! { |y|  C::Blog.created_in_year(year: y).any? }
    if age_limit.year.to_s == params[:year].to_s
      @blog_archive_filter_months = (1..age_limit.month).to_a
    else
      @blog_archive_filter_months = (1..12).to_a
    end
    @blog_archive_filter_months.select! { |m|  C::Blog.created_in_month(year: year, month: m).any?  }
    if params[:month]
      return C::Blog.created_in_month(month: month, year: year, limit: limit).ordered
    else
      return C::Blog.created_in_year(year: year, limit: limit).ordered
    end
  end
  return nil
end

#blog_archive_menu(options = {}) ⇒ Object

Returns html for a ul of links, which set params Only months from @blog_archive_filter_years are listed Also accepts an options hash The :permitted option expects an array of keys which are permitted to be included in the generated links The :submenu option expects a boolean, which can be used to disable rendering the month submenu



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/c/blogs_helper.rb', line 51

def blog_archive_menu options = {}
  permitted = options[:permitted] || []
  submenu = options[:submenu]
  submenu = true if submenu.nil?

   :ul do
    result = ""

    @blog_archive_filter_years.each do |filter_year|
      if params[:year].to_s == filter_year.to_s
        if submenu
          if params[:month]
            result +=  :li, (
            (render partial: 'c/front/blogs/archive_filter_year', locals: {permitted: permitted, archive_filter_year: filter_year}) +
                blog_archive_month_submenu(permitted: permitted)
            )
          else
            result +=  :li, (
            raw filter_year.to_s + blog_archive_month_submenu(permitted: permitted)
            )
          end
        else
          result +=  :li, filter_year.to_s
        end
      else
        result +=  :li, (render partial: 'c/front/blogs/archive_filter_year', locals: {permitted: permitted, archive_filter_year: filter_year})
      end
    end
    raw result
  end
end

#blog_archive_month_submenu(options = {}) ⇒ Object

Returns html for a ul of links, which set params Only months from @blog_archive_filter_months are listed Accepts an options hash The :permitted option expects an array of keys which are permitted to be included in the generated links



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/c/blogs_helper.rb', line 87

def blog_archive_month_submenu options = {}
  permitted = options[:permitted] || []
   :ul do
    # render partial: 'c/front/blogs/archive_filter_month', collection: @blog_archive_filter_months, locals: {permitted: permitted}
    result = ""
    @blog_archive_filter_months.each do |filter_month|
      if params[:month].to_s == filter_month.to_s
        result +=  :li, (Date::MONTHNAMES[filter_month])
      else
        result += render partial: 'c/front/blogs/archive_filter_month', locals: {permitted: permitted, archive_filter_month: filter_month}
      end
    end
    raw result
  end
end

#safe_params(unsafe = {}, options = {}) ⇒ Object

Given a hash, permits only :month and :year Accepts an options hash The :permitted option expects an array of other keys to permit



106
107
108
109
110
# File 'app/helpers/c/blogs_helper.rb', line 106

def safe_params unsafe = {}, options = {}
  permitted = options[:permitted] || []
  permitted.push :month, :year
  params.merge(unsafe).merge(only_path: true, script_name: nil).permit(permitted)
end

#test_methodObject



8
9
10
# File 'app/helpers/c/blogs_helper.rb', line 8

def test_method
  true
end