lib/ecrire/app/models/admin/post.rb in ecrire-0.28.3 vs lib/ecrire/app/models/admin/post.rb in ecrire-0.29.0
- old
+ new
@@ -5,10 +5,29 @@
has_many :titles, -> { order "titles.created_at DESC" }, class_name: Admin::Title
before_save :compile!, prepend: true
before_save :excerptize!
+ def self.search(params = {})
+ rel = self
+
+ if params.has_key?(:tag) && !params[:tag].blank?
+ rel = rel.where('? = ANY(posts.tags)', params[:tag])
+ end
+
+ if params.has_key?(:title) && !params[:title].blank?
+ titles = Admin::Title.search_by_name(params[:title])
+ rel = rel.where('id in (?)', titles.pluck(:post_id).uniq.compact)
+ end
+
+ if params.has_key?(:status)
+ rel = rel.status(params[:status])
+ end
+
+ rel
+ end
+
def publish!(params = {})
self.assign_attributes(params)
self.published_at = DateTime.now
self.save!
end
@@ -27,9 +46,13 @@
super || ""
end
def content
read_attribute(:content) || ""
+ end
+
+ def status
+ published? ? 'published' : 'draft'
end
private
def compile!