Module: CategoriesHelper

Defined in:
app/helpers/categories_helper.rb

Instance Method Summary collapse

Instance Method Details

#active?(category) ⇒ Boolean

Checks if the specified category is the currently active page.

Parameters:

Returns:

  • (Boolean)


6
7
8
9
10
# File 'app/helpers/categories_helper.rb', line 6

def active?(category)
  current_category
  current_page?(category_url(category)) || (category.is_homepage && current_page?(root_url)) ||
    (defined?(@current_category) && @current_category&.id == category.id)
end

#current_categoryCategory

Sets and returns the currently-active category.

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/categories_helper.rb', line 26

def current_category
  @current_category ||= if defined?(@category) && !@category&.id.nil?
                          @category
                        elsif defined?(@post) && !@post&.category.nil?
                          @post.category
                        elsif defined?(@question) && !@question&.category.nil?
                          @question.category
                        elsif defined?(@article) && !@article&.category.nil?
                          @article.category
                        elsif defined?(@edit) && !@edit&.post&.category.nil?
                          @edit.post.category
                        end
end

#expandable?Boolean

Checks if the current page is within the scope of a category and can use an expanded header.

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'app/helpers/categories_helper.rb', line 15

def expandable?
  (defined?(@category) && !@category&.id.nil? && !current_page?(new_category_url)) ||
    (defined?(@post) && !@post&.category.nil?) ||
    (defined?(@question) && !@question&.category.nil?) ||
    (defined?(@article) && !@article&.category.nil?) ||
    (defined?(@edit) && !@edit&.post&.category&.nil?)
end

#pending_suggestions?Boolean

Note:

Cached - cache is manually broken when new suggestions are created.

Checks if there are any pending edit suggestions in the current category.

Returns:

  • (Boolean)


44
45
46
47
48
# File 'app/helpers/categories_helper.rb', line 44

def pending_suggestions?
  Rails.cache.fetch "pending_suggestions/#{current_category.id}" do
    SuggestedEdit.where(post: Post.undeleted.where(category: current_category), active: true).any?
  end
end