Repository URL to install this package:
|
Version:
0.2.2 ▾
|
module Neoteric
module NewsPost
def self.included(base)
base.table_name = 'news_posts'
base.extend ClassMethods
base.send :include, ::Neoteric::Archiver
base.archive_fields :single => :published_at,
:scope => :published
base.extend FriendlyId
base.friendly_id :title, :use => :slugged
base.send :include, ::Neoteric::Navigator
base.navigate :scope => :published,
:field => :published_at,
:order => 'published_at DESC'
base.send :before_save, :set_default_status,
:trigger_state_transition_callbacks
base.attr_accessible :body, :excerpt, :published_at, :status, :title
base.send :default_scope, :order => ['published_at DESC', :title]
base.scope :published, base.where(:status => 'published')
base.scope :drafted, base.where(:status => 'drafted')
end
module ClassMethods
def states_for_select
[['Drafted', 'drafted'], ['Published', 'published']]
end
end
def publish
self.status = 'published'
set_published_at
end
def publish!
publish && save
end
def draft
self.status = 'drafted'
clear_published_at
end
def draft!
draft && save
end
private
def date_and_title
[published_at.to_s, title].join(' ')
end
def set_published_at
self.published_at = Date.today if published_at.blank?
end
def clear_published_at
self.published_at = nil
end
def trigger_state_transition_callbacks
new_status = status.gsub(/ed$/,'').to_sym
send(new_status)
end
def set_default_status
return unless status.blank?
self.status = 'drafted'
end
end
end