Class: ArticleCategory
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ArticleCategory
- Defined in:
- app/models/article_category.rb
Overview
Article category
Instance Attribute Summary collapse
-
#articles ⇒ Array<Article>
Articles with this category.
-
#name ⇒ String
Title of the category.
-
#order_articles ⇒ Array<OrderArticle>
Order articles with this category.
-
#orders ⇒ Array<Order>
Orders with articles in this category.
Class Method Summary collapse
-
.find_match(category) ⇒ Object
Find a category that matches a category name; may return nil.
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
Instance Method Summary collapse
-
#check_for_associated_articles ⇒ Object
protected
Deny deleting the category when there are associated articles.
Instance Attribute Details
#articles ⇒ Array<Article>
Returns Articles with this category.
10 |
# File 'app/models/article_category.rb', line 10 has_many :articles |
#name ⇒ String
Returns Title of the category.
|
# File 'app/models/article_category.rb', line 3
|
#order_articles ⇒ Array<OrderArticle>
Returns Order articles with this category.
13 |
# File 'app/models/article_category.rb', line 13 has_many :order_articles, through: :articles |
#orders ⇒ Array<Order>
Returns Orders with articles in this category.
16 |
# File 'app/models/article_category.rb', line 16 has_many :orders, through: :order_articles |
Class Method Details
.find_match(category) ⇒ Object
Find a category that matches a category name; may return nil. TODO more intelligence like remembering earlier associations (global and/or per-supplier)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/article_category.rb', line 34 def self.find_match(category) return if category.blank? || category.length < 3 c = nil ## exact match - not needed, will be returned by next query as well # c ||= ArticleCategory.where(name: category).first # case-insensitive substring match (take the closest match = shortest) c = ArticleCategory.where('name LIKE ?', "%#{category}%") unless c && c.any? # case-insensitive phrase present in category description unless c && c.any? c = ArticleCategory.where('description LIKE ?', "%#{category}%").select do |s| s.description.match(/(^|,)\s*#{category}\s*(,|$)/i) end end # return closest match if there are multiple c = c.sort_by { |s| s.name.length }.first if c.respond_to? :sort_by c end |
.ransackable_associations(_auth_object = nil) ⇒ Object
28 29 30 |
# File 'app/models/article_category.rb', line 28 def self.ransackable_associations(_auth_object = nil) %w[articles order_articles orders] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
24 25 26 |
# File 'app/models/article_category.rb', line 24 def self.ransackable_attributes(_auth_object = nil) %w[id name] end |
Instance Method Details
#check_for_associated_articles ⇒ Object (protected)
Deny deleting the category when there are associated articles.
56 57 58 59 60 61 |
# File 'app/models/article_category.rb', line 56 def check_for_associated_articles return unless articles.undeleted.exists? raise I18n.t('activerecord.errors.has_many_left', collection: Article.model_name.human) end |