83 lines
1.9 KiB
Ruby
83 lines
1.9 KiB
Ruby
class Article < ActiveRecord::Base
|
|
belongs_to :image_file
|
|
validates :title, :presence => 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
|
|
|
|
before_validation do
|
|
self.slug = self.slug.to_slug
|
|
self.tags_cache = self.tags_cache.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
|
|
|
|
end
|
|
|
|
|
|
before_save do
|
|
#self.tags_cache = nil
|
|
|
|
tag_list = self.tags_cache.split(',').uniq
|
|
|
|
tag_names = []
|
|
tag_objects_list = []
|
|
|
|
tag_list.each do |tag_name|
|
|
if tag = Tag.create(:name => tag_name) and tag.id
|
|
self.tags << tag
|
|
else
|
|
tag = Tag.find_by_slug(tag_name.to_slug)
|
|
self.tags << tag if !self.tags.include?(tag)
|
|
end
|
|
tag_objects_list << tag
|
|
tag_names << tag.name
|
|
end
|
|
tags_to_remove = tags - tag_objects_list
|
|
tags_id_to_remove = (tags - tag_objects_list).map {|t| t.id}
|
|
puts tags_id_to_remove
|
|
self.tag_taggables.where(:tag_id => tags_id_to_remove).each { |t| t.destroy }
|
|
end
|
|
|
|
|
|
|
|
has_many :tag_taggables, :as => :taggable
|
|
has_many :tags, :through => :tag_taggables
|
|
|
|
def after_creation
|
|
@block = Block.new(:block_name => "Contenu")
|
|
@block.blockable = self
|
|
@block.save
|
|
|
|
ContentType.all.each do |content_type|
|
|
@block.content_types << content_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
scope :recents, where("enabled = ?",true ).order("published_at DESC")
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
end
|