Making use of modules and putting classes under modules.

This commit is contained in:
Philip Arndt 2011-07-01 09:14:46 +12:00
parent 47a71b309f
commit 7028ef3ddd
7 changed files with 260 additions and 238 deletions

View file

@ -1,7 +1,11 @@
class Admin::Blog::CategoriesController < Admin::BaseController module Admin
module Blog
class CategoriesController < Admin::BaseController
crudify :blog_category, crudify :blog_category,
:title_attribute => :title, :title_attribute => :title,
:order => 'title ASC' :order => 'title ASC'
end
end
end end

View file

@ -1,36 +1,40 @@
class Admin::Blog::CommentsController < Admin::BaseController module Admin
module Blog
class CommentsController < Admin::BaseController
crudify :blog_comment, crudify :blog_comment,
:title_attribute => :name, :title_attribute => :name,
:order => 'published_at DESC' :order => 'published_at DESC'
def index def index
@blog_comments = BlogComment.unmoderated @blog_comments = BlogComment.unmoderated
render :action => 'index' render :action => 'index'
end end
def approved
unless params[:id].present?
@blog_comments = BlogComment.approved
render :action => 'index'
else
@blog_comment = BlogComment.find(params[:id])
@blog_comment.approve!
flash[:notice] = t('approved', :scope => 'admin.blog.comments', :author => @blog_comment.name)
redirect_to :action => params[:return_to] || 'index'
end
end
def rejected
unless params[:id].present?
@blog_comments = BlogComment.rejected
render :action => 'index'
else
@blog_comment = BlogComment.find(params[:id])
@blog_comment.reject!
flash[:notice] = t('rejected', :scope => 'admin.blog.comments', :author => @blog_comment.name)
redirect_to :action => params[:return_to] || 'index'
end
end
def approved
unless params[:id].present?
@blog_comments = BlogComment.approved
render :action => 'index'
else
@blog_comment = BlogComment.find(params[:id])
@blog_comment.approve!
flash[:notice] = t('approved', :scope => 'admin.blog.comments', :author => @blog_comment.name)
redirect_to :action => params[:return_to] || 'index'
end end
end end
def rejected
unless params[:id].present?
@blog_comments = BlogComment.rejected
render :action => 'index'
else
@blog_comment = BlogComment.find(params[:id])
@blog_comment.reject!
flash[:notice] = t('rejected', :scope => 'admin.blog.comments', :author => @blog_comment.name)
redirect_to :action => params[:return_to] || 'index'
end
end
end end

View file

@ -1,86 +1,90 @@
class Admin::Blog::PostsController < Admin::BaseController module Admin
module Blog
class PostsController < Admin::BaseController
crudify :blog_post, crudify :blog_post,
:title_attribute => :title, :title_attribute => :title,
:order => 'published_at DESC' :order => 'published_at DESC'
def uncategorized def uncategorized
@blog_posts = BlogPost.uncategorized.paginate({ @blog_posts = BlogPost.uncategorized.paginate({
:page => params[:page], :page => params[:page],
:per_page => BlogPost.per_page :per_page => BlogPost.per_page
}) })
end end
def tags def tags
op = case ActiveRecord::Base.connection.adapter_name.downcase op = case ActiveRecord::Base.connection.adapter_name.downcase
when 'postgresql' when 'postgresql'
'~*' '~*'
else
'LIKE'
end
wildcard = case ActiveRecord::Base.connection.adapter_name.downcase
when 'postgresql'
'.*'
else
'%'
end
@tags = BlogPost.tag_counts_on(:tags).where(
["tags.name #{op} ?", "#{wildcard}#{params[:term].to_s.downcase}#{wildcard}"]
).map { |tag| {:id => tag.id, :value => tag.name}}
render :json => @tags.flatten
end
def create
# if the position field exists, set this object as last object, given the conditions of this class.
if BlogPost.column_names.include?("position")
params[:blog_post].merge!({
:position => ((BlogPost.maximum(:position, :conditions => "")||-1) + 1)
})
end
if BlogPost.column_names.include?("user_id")
params[:blog_post].merge!({
:user_id => current_user.id
})
end
if (@blog_post = BlogPost.create(params[:blog_post])).valid?
(request.xhr? ? flash.now : flash).notice = t(
'refinery.crudify.created',
:what => "'#{@blog_post.title}'"
)
unless from_dialog?
unless params[:continue_editing] =~ /true|on|1/
redirect_back_or_default(admin_blog_posts_url)
else
unless request.xhr?
redirect_to :back
else
render :partial => "/shared/message"
end
end
else else
'LIKE' render :text => "<script>parent.window.location = '#{admin_blog_posts_url}';</script>"
end end
wildcard = case ActiveRecord::Base.connection.adapter_name.downcase
when 'postgresql'
'.*'
else
'%'
end
@tags = BlogPost.tag_counts_on(:tags).where(
["tags.name #{op} ?", "#{wildcard}#{params[:term].to_s.downcase}#{wildcard}"]
).map { |tag| {:id => tag.id, :value => tag.name}}
render :json => @tags.flatten
end
def create
# if the position field exists, set this object as last object, given the conditions of this class.
if BlogPost.column_names.include?("position")
params[:blog_post].merge!({
:position => ((BlogPost.maximum(:position, :conditions => "")||-1) + 1)
})
end
if BlogPost.column_names.include?("user_id")
params[:blog_post].merge!({
:user_id => current_user.id
})
end
if (@blog_post = BlogPost.create(params[:blog_post])).valid?
(request.xhr? ? flash.now : flash).notice = t(
'refinery.crudify.created',
:what => "'#{@blog_post.title}'"
)
unless from_dialog?
unless params[:continue_editing] =~ /true|on|1/
redirect_back_or_default(admin_blog_posts_url)
else else
unless request.xhr? unless request.xhr?
redirect_to :back render :action => 'new'
else else
render :partial => "/shared/message" render :partial => "/shared/admin/error_messages",
:locals => {
:object => @blog_post,
:include_object_name => true
}
end end
end end
else
render :text => "<script>parent.window.location = '#{admin_blog_posts_url}';</script>"
end end
else
unless request.xhr? before_filter :find_all_categories,
render :action => 'new' :only => [:new, :edit, :create, :update]
else
render :partial => "/shared/admin/error_messages", protected
:locals => { def find_all_categories
:object => @blog_post, @blog_categories = BlogCategory.find(:all)
:include_object_name => true
}
end end
end end
end end
before_filter :find_all_categories,
:only => [:new, :edit, :create, :update]
protected
def find_all_categories
@blog_categories = BlogCategory.find(:all)
end
end end

View file

@ -1,39 +1,43 @@
class Admin::Blog::SettingsController < Admin::BaseController module Admin
module Blog
class SettingsController < Admin::BaseController
def notification_recipients def notification_recipients
@recipients = BlogComment::Notification.recipients @recipients = BlogComment::Notification.recipients
if request.post? if request.post?
BlogComment::Notification.recipients = params[:recipients] BlogComment::Notification.recipients = params[:recipients]
flash[:notice] = t('updated', :scope => 'admin.blog.settings.notification_recipients' flash[:notice] = t('updated', :scope => 'admin.blog.settings.notification_recipients'
:recipients => BlogComment::Notification.recipients) :recipients => BlogComment::Notification.recipients)
unless request.xhr? or from_dialog? unless request.xhr? or from_dialog?
redirect_back_or_default(admin_blog_posts_path) redirect_back_or_default(admin_blog_posts_path)
else else
render :text => "<script type='text/javascript'>parent.window.location = '#{admin_blog_posts_path}';</script>", render :text => "<script type='text/javascript'>parent.window.location = '#{admin_blog_posts_path}';</script>",
:layout => false :layout => false
end
end
end end
def moderation
enabled = BlogComment::Moderation.toggle!
unless request.xhr?
redirect_back_or_default(admin_blog_posts_path)
else
render :json => {:enabled => enabled},
:layout => false
end
end
def comments
enabled = BlogComment.toggle!
unless request.xhr?
redirect_back_or_default(admin_blog_posts_path)
else
render :json => {:enabled => enabled},
:layout => false
end
end
end end
end end
def moderation
enabled = BlogComment::Moderation.toggle!
unless request.xhr?
redirect_back_or_default(admin_blog_posts_path)
else
render :json => {:enabled => enabled},
:layout => false
end
end
def comments
enabled = BlogComment.toggle!
unless request.xhr?
redirect_back_or_default(admin_blog_posts_path)
else
render :json => {:enabled => enabled},
:layout => false
end
end
end end

View file

@ -1,11 +1,13 @@
class Blog::CategoriesController < BlogController module Blog
class CategoriesController < BlogController
def show
@category = BlogCategory.find(params[:id])
@blog_posts = @category.posts.live.includes(:comments, :categories).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end
def show
@category = BlogCategory.find(params[:id])
@blog_posts = @category.posts.live.includes(:comments, :categories).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end end
end end

View file

@ -1,101 +1,103 @@
class Blog::PostsController < BlogController module Blog
class PostsController < BlogController
before_filter :find_all_blog_posts, :except => [:archive] before_filter :find_all_blog_posts, :except => [:archive]
before_filter :find_blog_post, :only => [:show, :comment, :update_nav] before_filter :find_blog_post, :only => [:show, :comment, :update_nav]
before_filter :find_tags before_filter :find_tags
respond_to :html, :js, :rss respond_to :html, :js, :rss
def index def index
respond_with (@blog_posts) do |format| respond_with (@blog_posts) do |format|
format.html format.html
format.rss format.rss
end
end end
end
def show def show
@blog_comment = BlogComment.new @blog_comment = BlogComment.new
respond_with (@blog_post) do |format| respond_with (@blog_post) do |format|
format.html { present(@blog_post) } format.html { present(@blog_post) }
format.js { render :partial => 'post', :layout => false } format.js { render :partial => 'post', :layout => false }
end
end end
end
def comment def comment
if (@blog_comment = @blog_post.comments.create(params[:blog_comment])).valid? if (@blog_comment = @blog_post.comments.create(params[:blog_comment])).valid?
if BlogComment::Moderation.enabled? or @blog_comment.ham? if BlogComment::Moderation.enabled? or @blog_comment.ham?
begin begin
Blog::CommentMailer.notification(@blog_comment, request).deliver Blog::CommentMailer.notification(@blog_comment, request).deliver
rescue rescue
logger.warn "There was an error delivering a blog comment notification.\n#{$!}\n" logger.warn "There was an error delivering a blog comment notification.\n#{$!}\n"
end
end
if BlogComment::Moderation.enabled?
flash[:notice] = t('thank_you_moderated', :scope => 'blog.posts.comments')
redirect_to blog_post_url(params[:id])
else
flash[:notice] = t('thank_you', :scope => 'blog.posts.comments')
redirect_to blog_post_url(params[:id],
:anchor => "comment-#{@blog_comment.to_param}")
end
else
render :action => 'show'
end
end
def archive
if params[:month].present?
date = "#{params[:month]}/#{params[:year]}"
@archive_date = Time.parse(date)
@date_title = @archive_date.strftime('%B %Y')
@blog_posts = BlogPost.live.by_archive(@archive_date).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
else
date = "01/#{params[:year]}"
@archive_date = Time.parse(date)
@date_title = @archive_date.strftime('%Y')
@blog_posts = BlogPost.live.by_year(@archive_date).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end
respond_with (@blog_posts)
end
def tagged
@tag = ActsAsTaggableOn::Tag.find(params[:tag_id])
@tag_name = @tag.name
@blog_posts = BlogPost.tagged_with(@tag_name).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end
protected
def find_blog_post
unless (@blog_post = BlogPost.find(params[:id])).try(:live?)
if refinery_user? and current_user.authorized_plugins.include?("refinerycms_blog")
@blog_post = BlogPost.find(params[:id])
else
error_404
end end
end end
if BlogComment::Moderation.enabled?
flash[:notice] = t('thank_you_moderated', :scope => 'blog.posts.comments')
redirect_to blog_post_url(params[:id])
else
flash[:notice] = t('thank_you', :scope => 'blog.posts.comments')
redirect_to blog_post_url(params[:id],
:anchor => "comment-#{@blog_comment.to_param}")
end
else
render :action => 'show'
end end
end
def archive def find_all_blog_posts
if params[:month].present? @blog_posts = BlogPost.live.includes(:comments, :categories).paginate({
date = "#{params[:month]}/#{params[:year]}"
@archive_date = Time.parse(date)
@date_title = @archive_date.strftime('%B %Y')
@blog_posts = BlogPost.live.by_archive(@archive_date).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
else
date = "01/#{params[:year]}"
@archive_date = Time.parse(date)
@date_title = @archive_date.strftime('%Y')
@blog_posts = BlogPost.live.by_year(@archive_date).paginate({
:page => params[:page], :page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10) :per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
}) })
end end
respond_with (@blog_posts)
end
def tagged def find_tags
@tag = ActsAsTaggableOn::Tag.find(params[:tag_id]) @tags = BlogPost.tag_counts_on(:tags)
@tag_name = @tag.name
@blog_posts = BlogPost.tagged_with(@tag_name).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end
protected
def find_blog_post
unless (@blog_post = BlogPost.find(params[:id])).try(:live?)
if refinery_user? and current_user.authorized_plugins.include?("refinerycms_blog")
@blog_post = BlogPost.find(params[:id])
else
error_404
end
end end
end
def find_all_blog_posts
@blog_posts = BlogPost.live.includes(:comments, :categories).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end end
def find_tags
@tags = BlogPost.tag_counts_on(:tags)
end
end end

View file

@ -1,11 +1,13 @@
class Blog::CommentMailer < ActionMailer::Base module Blog
class CommentMailer < ActionMailer::Base
def notification(comment, request)
subject BlogComment::Notification.subject
recipients BlogComment::Notification.recipients
from "\"#{RefinerySetting[:site_name]}\" <no-reply@#{request.domain(RefinerySetting.find_or_set(:tld_length, 1))}>"
sent_on Time.now
@comment = comment
end
def notification(comment, request)
subject BlogComment::Notification.subject
recipients BlogComment::Notification.recipients
from "\"#{RefinerySetting[:site_name]}\" <no-reply@#{request.domain(RefinerySetting.find_or_set(:tld_length, 1))}>"
sent_on Time.now
@comment = comment
end end
end end