foodsoft/app/models/article_category.rb

44 lines
1.6 KiB
Ruby
Raw Normal View History

2014-06-06 16:34:07 +02:00
# Article category
class ArticleCategory < ActiveRecord::Base
2014-06-06 16:34:07 +02:00
# @!attribute name
# @return [String] Title of the category.
# @!attrubute description
# @return [String] Description (currently unused)
# @!attribute articles
# @return [Array<Article>] Articles with this category.
has_many :articles
2011-05-18 16:10:30 +02:00
normalize_attributes :name, :description
validates :name, :presence => true, :uniqueness => true, :length => { :minimum => 2 }
before_destroy :check_for_associated_articles
# Find a category that matches a category name; may return nil.
# TODO more intelligence like remembering earlier associations (global and/or per-supplier)
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
c = ArticleCategory.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
protected
2014-06-06 16:34:07 +02:00
# Deny deleting the category when there are associated articles.
def check_for_associated_articles
raise I18n.t('activerecord.errors.has_many_left', collection: Article.model_name.human) if articles.undeleted.exists?
end
end