heylium_app/app/models/article.rb
Nicolas Bally 8e18de4077 initial
2016-08-03 00:22:29 +02:00

84 lines
1.4 KiB
Ruby
Executable File

class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
belongs_to :image_file
has_many :blocks, :as => :blockable
belongs_to :category
has_many :enabled_comments,-> {where(:enabled => true)}, :source => :comments, :as => :commentable
after_create :after_creation
has_many :lang_articles, :dependent => :destroy
accepts_nested_attributes_for :lang_articles
has_many :tag_taggables, :as => :taggable
has_many :tags, :through => :tag_taggables
HUMAN_NAME = "lien vers un article."
def after_creation
LangSite.all.each do |lang|
@block = Block.new(:block_name => "Contenu", :lang_site => lang)
@block.blockable = self
@block.save
end
end
def alloweds_types
self.block.allow_types :TitleContent, :TextContent, :ImageContent, :LinkContent, :GalleryContent, :HtmlContent
end
scope :recents, -> {order("articles.published_at DESC, articles.created_at DESC")}
scope :between, lambda { |start, stop|
after(start).before(stop)
}
scope :after, lambda { |date|
where("(articles.published_at >= ?)", date )
}
scope :before, lambda { |date|
where("(articles.published_at <= ?)", date )
}
after_initialize do
if self.published_at
self.published_at = self.published_at.strftime('%d/%m/%Y')
end
end
end