foodsoft/app/models/article_category.rb

28 lines
751 B
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
2011-05-18 16:10:30 +02:00
validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 }
before_destroy :check_for_associated_articles
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