Class: Dokno::Category
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Dokno::Category
- Defined in:
- app/models/dokno/category.rb
Class Method Summary collapse
-
.branch(parent_category_id:, at_top: true) ⇒ Object
The given Category and all child Categories.
-
.cache_key ⇒ Object
Used to invalidate the fragment cache of the hierarchical category select options.
- .select_option_markup(selected_category_codes: nil, context_category: nil, level: 0) ⇒ Object
Instance Method Summary collapse
-
#articles_in_branch(order: :updated) ⇒ Object
All Articles in the Category, including all child Categories.
- #branch ⇒ Object
- #breadcrumb(**args) ⇒ Object
- #category_link(category, args = {}) ⇒ Object
Class Method Details
.branch(parent_category_id:, at_top: true) ⇒ Object
The given Category and all child Categories. Useful for filtering associated articles.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/dokno/category.rb', line 53 def self.branch(parent_category_id:, at_top: true) return if parent_category_id.blank? categories = [] parent_category = find(parent_category_id) child_categories = parent_category.children.to_a child_categories.each do |child_category| categories << child_category << branch(parent_category_id: child_category.id, at_top: false) end categories.prepend parent_category if at_top categories.flatten end |
.cache_key ⇒ Object
Used to invalidate the fragment cache of the hierarchical category select options
48 49 50 |
# File 'app/models/dokno/category.rb', line 48 def self.cache_key [maximum(:updated_at), Article.maximum(:updated_at)].compact.max end |
.select_option_markup(selected_category_codes: nil, context_category: nil, level: 0) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/dokno/category.rb', line 68 def self.select_option_markup(selected_category_codes: nil, context_category: nil, level: 0) return '' if level.positive? && context_category.blank? = [] level_categories = where(category_id: context_category&.id).alpha_order level_categories.each do |category| << option_markup(category: category, selected_category_codes: selected_category_codes, level: level) << select_option_markup(selected_category_codes: selected_category_codes, context_category: category, level: (level + 1)) end .join end |
Instance Method Details
#articles_in_branch(order: :updated) ⇒ Object
All Articles in the Category, including all child Categories
34 35 36 37 38 39 40 41 |
# File 'app/models/dokno/category.rb', line 34 def articles_in_branch(order: :updated) records = Article .includes(:categories_dokno_articles, :categories) .joins(:categories) .where(dokno_categories: { id: self.class.branch(parent_category_id: id).pluck(:id) }) Article.apply_sort(records, order: order) end |
#branch ⇒ Object
43 44 45 |
# File 'app/models/dokno/category.rb', line 43 def branch self.class.branch(parent_category_id: id) end |
#breadcrumb(**args) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/dokno/category.rb', line 14 def (**args) crumbs = [(category_link(self, args) unless args[:hide_self])] parent_category_id = category_id loop do break if parent_category_id.blank? parent_category = Category.find(parent_category_id) crumbs.prepend category_link(parent_category, args) parent_category_id = parent_category.category_id end crumbs.compact.join(" > ").html_safe end |
#category_link(category, args = {}) ⇒ Object
29 30 31 |
# File 'app/models/dokno/category.rb', line 29 def category_link(category, args={}) %(<a href="#{article_index_path(category.code)}?search_term=#{CGI.escape(args[:search_term].to_s)}&order=#{CGI.escape(args[:order].to_s)}">#{category.name}</a>) end |