crossey_app/app/models/article.rb
2013-09-30 17:42:55 +02:00

96 lines
2.1 KiB
Ruby

class Article < ActiveRecord::Base
belongs_to :image_file
validates :title, :presence => true, :uniqueness => true
validates :slug, :presence => true, :uniqueness => true
has_one :block, :as => :blockable
has_many :comments, :as => :commentable
belongs_to :category
has_many :enabled_comments,:conditions => {:enabled => true}, :source => :comments, :as => :commentable
after_create :after_creation
HUMAN_NAME = "lien vers un article."
before_validation do
self.slug = [self.published_at.year,self.published_at.month,self.published_at.day].join("-")+"-"+self.title.to_slug
#self.tags_cache = self.tags_cache.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
#self.tags_cache = self.tags_cache.to_s.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
end
def after_creation
@block = Block.new(:block_name => "Contenu")
@block.blockable = self
@block.save
end
def alloweds_types
self.block.allow_types :TitleContent, :TextContent, :ImageContent, :LinkContent, :GalleryContent, :HtmlContent
end
scope :frontpage, where("frontpage = ?",true )
scope :recents, where("enabled = ?",true ).order("published_at DESC, priority_level ASC")
scope :between, lambda { |start, stop|
after(start).before(stop)
}
scope :after, lambda { |date|
where("(published_at >= ?)", date )
}
scope :before, lambda { |date|
where("(published_at <= ?)", date )
}
after_initialize do
if self.published_at
self.published_at = self.published_at.strftime('%d/%m/%Y')
end
if self.end_at
self.end_at = self.end_at.strftime('%d/%m/%Y')
end
end
before_save do
end
has_many :tag_taggables, :as => :taggable
has_many :tags, :through => :tag_taggables
def generate_tags_cache
self.tags_cache = self.tags.map{|a| a.name}.join(",").to_s
end
def tag_by_tag_ids(tags_id)
Tag.find(tags_id).each do |tag|
self.tags << tag if !self.tags.include?(tag)
end
self.generate_tags_cache
self.save
end
before_destroy do
self.tags_cache = ""
self.save
end
end