Class: ArticleCategory

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/article_category.rb

Overview

Article category

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#articlesArray<Article>


11
# File 'app/models/article_category.rb', line 11

has_many :articles

#nameString


# File 'app/models/article_category.rb', line 4


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)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/article_category.rb', line 21

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 = .where('name LIKE ?', "%#{category}%") unless c && c.any?
  # case-insensitive phrase present in category description
  c = .where('description LIKE ?', "%#{category}%").select {|s| s.description.match /(^|,)\s*#{category}\s*(,|$)/i} unless c && c.any?
  # return closest match if there are multiple
  c = c.sort_by {|s| s.name.length}.first if c.respond_to? :sort_by
  c
end

Instance Method Details

#check_for_associated_articlesObject (protected)

Deny deleting the category when there are associated articles.


38
39
40
# File 'app/models/article_category.rb', line 38

def check_for_associated_articles
  raise I18n.t('activerecord.errors.has_many_left', collection: Article.model_name.human) if articles.undeleted.exists?
end