Class: ArticleCategory
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ArticleCategory
- Defined in:
- app/models/article_category.rb
Overview
Article category
Instance Attribute Summary collapse
-
#article_versions ⇒ Array<ArticleVersion>
ArticleVersions 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 undeleted article_versions.
- #deleted? ⇒ Boolean
- #mark_as_deleted ⇒ Object
Instance Attribute Details
#article_versions ⇒ Array<ArticleVersion>
Returns ArticleVersions with this category.
10 |
# File 'app/models/article_category.rb', line 10 has_many :article_versions |
#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: :article_versions |
#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 undeleted article_versions.
67 68 69 70 71 72 |
# File 'app/models/article_category.rb', line 67 def check_for_associated_articles return unless article_versions.latest.undeleted.exists? raise I18n.t('activerecord.errors.has_many_left', collection: Article.model_name.human) end |
#deleted? ⇒ Boolean
53 54 55 |
# File 'app/models/article_category.rb', line 53 def deleted? deleted_at.present? end |
#mark_as_deleted ⇒ Object
57 58 59 60 61 62 |
# File 'app/models/article_category.rb', line 57 def mark_as_deleted transaction do check_for_associated_articles update_column :deleted_at, Time.now end end |