diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/blog_category.rb | 13 | ||||
-rw-r--r-- | app/models/blog_post.rb | 10 |
2 files changed, 19 insertions, 4 deletions
diff --git a/app/models/blog_category.rb b/app/models/blog_category.rb index 09d32de..c78a7a4 100644 --- a/app/models/blog_category.rb +++ b/app/models/blog_category.rb @@ -8,5 +8,16 @@ class BlogCategory < ActiveRecord::Base validates_uniqueness_of :title has_friendly_id :title, :use_slug => true - + + # this might be able to be optimised a little more + def post_count + count = 0 + + self.posts.each do |p| + count += 1 if p.live? + end + + count + end + end diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb index 0470cf7..a9fe8c2 100644 --- a/app/models/blog_post.rb +++ b/app/models/blog_post.rb @@ -13,11 +13,15 @@ class BlogPost < ActiveRecord::Base default_scope :order => "created_at DESC" if Rails.version < '3.0.0' - named_scope :live, :conditions => {:draft => false} + named_scope :live, lambda { {:conditions => ["published_at < ? and draft = ?", Time.now, false]} } else - scope :live, :conditions => {:draft => false} + scope :live, lambda { where( "published_at < ? and draft = ?", Time.now, false) } end - + + def live? + !draft and published_at <= Time.now + end + def category_ids=(ids) self.categories = ids.reject{|id| id.blank?}.collect {|c_id| BlogCategory.find(c_id.to_i) rescue nil |