aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/blog_category.rb3
-rw-r--r--app/models/blog_comment.rb9
-rw-r--r--app/models/blog_post.rb46
-rw-r--r--app/models/categorization.rb5
4 files changed, 50 insertions, 13 deletions
diff --git a/app/models/blog_category.rb b/app/models/blog_category.rb
index 8ffe834..bc0165a 100644
--- a/app/models/blog_category.rb
+++ b/app/models/blog_category.rb
@@ -1,6 +1,7 @@
class BlogCategory < ActiveRecord::Base
- has_and_belongs_to_many :posts, :class_name => 'BlogPost'
+ has_many :categorizations
+ has_many :posts, :through => :categorizations, :source => :blog_post
acts_as_indexed :fields => [:title]
diff --git a/app/models/blog_comment.rb b/app/models/blog_comment.rb
index 015b96d..13aceb1 100644
--- a/app/models/blog_comment.rb
+++ b/app/models/blog_comment.rb
@@ -13,7 +13,7 @@ class BlogComment < ActiveRecord::Base
alias_attribute :message, :body
validates :name, :message, :presence => true
- validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
+ validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
scope :unmoderated, :conditions => {:state => nil}
scope :approved, :conditions => {:state => 'approved'}
@@ -39,6 +39,13 @@ class BlogComment < ActiveRecord::Base
self.state.nil?
end
+ def self.toggle!
+ currently = RefinerySetting.find_or_set(:comments_allowed, true, {
+ :scoping => 'blog'
+ })
+ RefinerySetting.set(:comments_allowed, {:value => !currently, :scoping => 'blog'})
+ end
+
before_create do |comment|
unless BlogComment::Moderation.enabled?
comment.state = comment.ham? ? 'approved' : 'rejected'
diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb
index 1887a97..db85a70 100644
--- a/app/models/blog_post.rb
+++ b/app/models/blog_post.rb
@@ -1,7 +1,17 @@
-class BlogPost < ActiveRecord::Base
+require 'acts-as-taggable-on'
+class BlogPost < ActiveRecord::Base
+
+ default_scope :order => 'published_at DESC'
+ #.first & .last will be reversed -- consider a with_exclusive_scope on these?
+
+ belongs_to :author, :class_name => 'User', :foreign_key => :user_id
+
has_many :comments, :class_name => 'BlogComment', :dependent => :destroy
- has_and_belongs_to_many :categories, :class_name => 'BlogCategory'
+ acts_as_taggable
+
+ has_many :categorizations
+ has_many :categories, :through => :categorizations, :source => :blog_category
acts_as_indexed :fields => [:title, :body]
@@ -11,22 +21,26 @@ class BlogPost < ActiveRecord::Base
has_friendly_id :title, :use_slug => true
scope :by_archive, lambda { |archive_date|
- where(['published_at between ? and ?', archive_date.beginning_of_month, archive_date.end_of_month]).order("published_at DESC")
+ where(['published_at between ? and ?', archive_date.beginning_of_month, archive_date.end_of_month])
+ }
+
+ scope :by_year, lambda { |archive_year|
+ where(['published_at between ? and ?', archive_year.beginning_of_year, archive_year.end_of_year])
}
- scope :all_previous, where(['published_at <= ?', Time.now.beginning_of_month]).order("published_at DESC")
-
- scope :live, lambda { where( "published_at < ? and draft = ?", Time.now, false).order("published_at DESC") }
+ scope :all_previous, lambda { where(['published_at <= ?', Time.now.beginning_of_month]) }
- scope :previous, lambda { |i| where(["published_at < ? and draft = ?", i.published_at, false]).order("published_at DESC").limit(1) }
- scope :next, lambda { |i| where(["published_at > ? and draft = ?", i.published_at, false]).order("published_at ASC").limit(1) }
+ scope :live, lambda { where( "published_at <= ? and draft = ?", Time.now, false) }
+ scope :previous, lambda { |i| where(["published_at < ? and draft = ?", i.published_at, false]).limit(1) }
+ # next is now in << self
+
def next
- self.class.next(self).first
+ BlogPost.next(self).first
end
def prev
- self.class.previous(self).first
+ BlogPost.previous(self).first
end
def live?
@@ -40,17 +54,27 @@ class BlogPost < ActiveRecord::Base
end
class << self
+ def next current_record
+ self.send(:with_exclusive_scope) do
+ where(["published_at > ? and draft = ?", current_record.published_at, false]).order("published_at ASC")
+ end
+ end
+
def comments_allowed?
RefinerySetting.find_or_set(:comments_allowed, true, {
:scoping => 'blog'
})
end
+
+ def uncategorized
+ BlogPost.live.reject { |p| p.categories.any? }
+ end
end
module ShareThis
DEFAULT_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
- class << self
+ class << self
def key
RefinerySetting.find_or_set(:share_this_key, BlogPost::ShareThis::DEFAULT_KEY, {
:scoping => 'blog'
diff --git a/app/models/categorization.rb b/app/models/categorization.rb
new file mode 100644
index 0000000..32e9967
--- /dev/null
+++ b/app/models/categorization.rb
@@ -0,0 +1,5 @@
+class Categorization < ActiveRecord::Base
+ set_table_name 'blog_categories_blog_posts'
+ belongs_to :blog_post
+ belongs_to :blog_category
+end \ No newline at end of file