Merge pull request #203 from substantial/cleanup_models
Cleanup model code
This commit is contained in:
commit
98f8d3f515
3 changed files with 14 additions and 39 deletions
|
@ -8,7 +8,7 @@ module Refinery
|
|||
:email_field => :email,
|
||||
:message_field => :body
|
||||
|
||||
belongs_to :post, :class_name => 'Refinery::Blog::Post', :foreign_key => 'blog_post_id'
|
||||
belongs_to :post, :foreign_key => 'blog_post_id'
|
||||
|
||||
acts_as_indexed :fields => [:name, :email, :message]
|
||||
|
||||
|
@ -132,4 +132,4 @@ module Refinery
|
|||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,11 +8,10 @@ module Refinery
|
|||
is_seo_meta if self.table_exists?
|
||||
|
||||
default_scope :order => 'published_at DESC'
|
||||
#.first & .last will be reversed -- consider a with_exclusive_scope on these?
|
||||
|
||||
belongs_to :author, :class_name => 'Refinery::User', :foreign_key => :user_id, :readonly => true
|
||||
|
||||
has_many :comments, :class_name => 'Refinery::Blog::Comment', :dependent => :destroy, :foreign_key => :blog_post_id
|
||||
has_many :comments, :dependent => :destroy, :foreign_key => :blog_post_id
|
||||
acts_as_taggable
|
||||
|
||||
has_many :categorizations, :dependent => :destroy, :foreign_key => :blog_post_id
|
||||
|
@ -41,23 +40,17 @@ module Refinery
|
|||
self.per_page = Refinery::Blog.posts_per_page
|
||||
|
||||
def next
|
||||
self.class.next(self).first
|
||||
self.class.next(self)
|
||||
end
|
||||
|
||||
def prev
|
||||
self.class.previous(self).first
|
||||
self.class.previous(self)
|
||||
end
|
||||
|
||||
def live?
|
||||
!draft and published_at <= Time.now
|
||||
end
|
||||
|
||||
def category_ids=(ids)
|
||||
self.categories = ids.reject{|id| id.blank?}.collect {|c_id|
|
||||
Refinery::Blog::Category.find(c_id.to_i) rescue nil
|
||||
}.compact
|
||||
end
|
||||
|
||||
def friendly_id_source
|
||||
custom_url.present? ? custom_url : title
|
||||
end
|
||||
|
@ -72,23 +65,19 @@ module Refinery
|
|||
end
|
||||
|
||||
def published_dates_older_than(date)
|
||||
where("published_at <= ?", date).map(&:published_at)
|
||||
published_before(date).pluck(:published_at)
|
||||
end
|
||||
|
||||
def live
|
||||
where( "published_at <= ? and draft = ?", Time.now, false)
|
||||
end
|
||||
|
||||
def recent(count)
|
||||
live.limit(count)
|
||||
end
|
||||
|
||||
|
||||
def popular(count)
|
||||
unscoped.order("access_count DESC").limit(count)
|
||||
end
|
||||
|
||||
def previous(item)
|
||||
where(["published_at < ? and draft = ?", item.published_at, false]).limit(1)
|
||||
published_before(item.published_at).first
|
||||
end
|
||||
|
||||
def uncategorized
|
||||
|
@ -96,11 +85,14 @@ module Refinery
|
|||
end
|
||||
|
||||
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
|
||||
where(["published_at > ? and draft = ?", current_record.published_at, false]).first
|
||||
end
|
||||
|
||||
def published_before(date=Time.now)
|
||||
where("published_at < ? and draft = ?", date, false)
|
||||
end
|
||||
alias_method :live, :published_before
|
||||
|
||||
def comments_allowed?
|
||||
Refinery::Setting.find_or_set(:comments_allowed, true, :scoping => 'blog')
|
||||
end
|
||||
|
|
|
@ -148,23 +148,6 @@ module Refinery
|
|||
end
|
||||
end
|
||||
|
||||
describe "#category_ids=" do
|
||||
before do
|
||||
@cat1 = FactoryGirl.create(:blog_category, :id => 1)
|
||||
@cat2 = FactoryGirl.create(:blog_category, :id => 2)
|
||||
@cat3 = FactoryGirl.create(:blog_category, :id => 3)
|
||||
post.category_ids = [1,2,"","",3]
|
||||
end
|
||||
|
||||
it "rejects blank category ids" do
|
||||
post.categories.count.should == 3
|
||||
end
|
||||
|
||||
it "returns array of categories based on given ids" do
|
||||
post.categories.should == [@cat1, @cat2, @cat3]
|
||||
end
|
||||
end
|
||||
|
||||
describe ".comments_allowed?" do
|
||||
context "with Refinery::Setting comments_allowed set to true" do
|
||||
before do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue