2014-06-06 16:34:07 +02:00
|
|
|
# Article category
|
2011-05-07 21:55:24 +02:00
|
|
|
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.
|
2011-05-07 21:55:24 +02:00
|
|
|
has_many :articles
|
2011-05-18 16:10:30 +02:00
|
|
|
|
2014-06-06 16:41:01 +02:00
|
|
|
normalize_attributes :name, :description
|
|
|
|
|
2011-05-18 16:10:30 +02:00
|
|
|
validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 }
|
2011-05-07 21:55:24 +02:00
|
|
|
|
2012-12-11 10:53:01 +01:00
|
|
|
before_destroy :check_for_associated_articles
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2014-06-06 16:34:07 +02:00
|
|
|
# Deny deleting the category when there are associated articles.
|
2012-12-11 10:53:01 +01:00
|
|
|
def check_for_associated_articles
|
2013-03-17 20:11:57 +01:00
|
|
|
raise I18n.t('activerecord.errors.has_many_left', collection: Article.model_name.human) if articles.undeleted.exists?
|
2012-12-11 10:53:01 +01:00
|
|
|
end
|
|
|
|
|
2011-05-07 21:55:24 +02:00
|
|
|
end
|
|
|
|
|