diff options
Diffstat (limited to 'app')
26 files changed, 644 insertions, 620 deletions
diff --git a/app/controllers/admin/blog/categories_controller.rb b/app/controllers/admin/blog/categories_controller.rb deleted file mode 100644 index 6933c44..0000000 --- a/app/controllers/admin/blog/categories_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Admin - module Blog - class CategoriesController < Admin::BaseController - - crudify :blog_category, - :title_attribute => :title, - :order => 'title ASC' - - end - end -end diff --git a/app/controllers/admin/blog/comments_controller.rb b/app/controllers/admin/blog/comments_controller.rb deleted file mode 100644 index 1868206..0000000 --- a/app/controllers/admin/blog/comments_controller.rb +++ /dev/null @@ -1,40 +0,0 @@ -module Admin - module Blog - class CommentsController < Admin::BaseController - - crudify :blog_comment, - :title_attribute => :name, - :order => 'published_at DESC' - - def index - @blog_comments = BlogComment.unmoderated - render :action => 'index' - 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 - - end - end -end
\ No newline at end of file diff --git a/app/controllers/admin/blog/posts_controller.rb b/app/controllers/admin/blog/posts_controller.rb deleted file mode 100644 index c8dfc13..0000000 --- a/app/controllers/admin/blog/posts_controller.rb +++ /dev/null @@ -1,97 +0,0 @@ -module Admin - module Blog - class PostsController < Admin::BaseController - - - crudify :blog_post, - :title_attribute => :title, - :order => 'published_at DESC' - - def uncategorized - @blog_posts = BlogPost.uncategorized.paginate({ - :page => params[:page], - :per_page => BlogPost.per_page - }) - end - - def tags - op = case ActiveRecord::Base.connection.adapter_name.downcase - 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 - render :text => "<script>parent.window.location = '#{admin_blog_posts_url}';</script>" - end - else - unless request.xhr? - render :action => 'new' - else - render :partial => "/shared/admin/error_messages", - :locals => { - :object => @blog_post, - :include_object_name => true - } - end - end - end - - before_filter :find_all_categories, - :only => [:new, :edit, :create, :update] - - before_filter :check_category_ids, :only => :update - - protected - def find_all_categories - @blog_categories = BlogCategory.find(:all) - end - - def check_category_ids - params[:blog_post][:category_ids] ||= [] - end - end - end -end
\ No newline at end of file diff --git a/app/controllers/admin/blog/settings_controller.rb b/app/controllers/admin/blog/settings_controller.rb deleted file mode 100644 index 5f2b3be..0000000 --- a/app/controllers/admin/blog/settings_controller.rb +++ /dev/null @@ -1,53 +0,0 @@ -module Admin - module Blog - class SettingsController < Admin::BaseController - - def notification_recipients - @recipients = BlogComment::Notification.recipients - - if request.post? - BlogComment::Notification.recipients = params[:recipients] - flash[:notice] = t('updated', :scope => 'admin.blog.settings.notification_recipients', - :recipients => BlogComment::Notification.recipients) - unless request.xhr? or from_dialog? - redirect_back_or_default(admin_blog_posts_path) - else - render :text => "<script type='text/javascript'>parent.window.location = '#{admin_blog_posts_path}';</script>", - :layout => false - 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 - - def teasers - enabled = BlogPost.teaser_enabled_toggle! - unless request.xhr? - redirect_back_or_default(admin_blog_posts_path) - else - render :json => {:enabled => enabled}, - :layout => false - end - end - - end - end -end
\ No newline at end of file diff --git a/app/controllers/blog/categories_controller.rb b/app/controllers/blog/categories_controller.rb deleted file mode 100644 index efda778..0000000 --- a/app/controllers/blog/categories_controller.rb +++ /dev/null @@ -1,13 +0,0 @@ -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 - - end -end
\ No newline at end of file diff --git a/app/controllers/blog/posts_controller.rb b/app/controllers/blog/posts_controller.rb deleted file mode 100644 index edac5ab..0000000 --- a/app/controllers/blog/posts_controller.rb +++ /dev/null @@ -1,105 +0,0 @@ -module Blog - class PostsController < BlogController - - before_filter :find_all_blog_posts, :except => [:archive] - before_filter :find_blog_post, :only => [:show, :comment, :update_nav] - before_filter :find_tags - - respond_to :html, :js, :rss - - def index - # Rss feeders are greedy. Let's give them every blog post instead of paginating. - (@blog_posts = BlogPost.live.includes(:comments, :categories).all) if request.format.rss? - respond_with (@blog_posts) do |format| - format.html - format.rss - end - end - - def show - @blog_comment = BlogComment.new - - respond_with (@blog_post) do |format| - format.html { present(@blog_post) } - format.js { render :partial => 'post', :layout => false } - end - end - - def comment - if (@blog_comment = @blog_post.comments.create(params[:blog_comment])).valid? - if BlogComment::Moderation.enabled? or @blog_comment.ham? - begin - Blog::CommentMailer.notification(@blog_comment, request).deliver - rescue - 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 - - 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 - - def find_tags - @tags = BlogPost.tag_counts_on(:tags) - end - - end -end
\ No newline at end of file diff --git a/app/controllers/blog_controller.rb b/app/controllers/blog_controller.rb deleted file mode 100644 index f51d5bb..0000000 --- a/app/controllers/blog_controller.rb +++ /dev/null @@ -1,16 +0,0 @@ -class BlogController < ApplicationController - - helper :blog_posts - before_filter :find_page, :find_all_blog_categories - -protected - - def find_page - @page = Page.find_by_link_url("/blog") - end - - def find_all_blog_categories - @blog_categories = BlogCategory.all - end - -end diff --git a/app/controllers/refinery/admin/blog/categories_controller.rb b/app/controllers/refinery/admin/blog/categories_controller.rb new file mode 100644 index 0000000..7196e8f --- /dev/null +++ b/app/controllers/refinery/admin/blog/categories_controller.rb @@ -0,0 +1,13 @@ +module Refinery + module Admin + module Blog + class CategoriesController < ::Admin::BaseController + + crudify :'refinery/blog_category', + :title_attribute => :title, + :order => 'title ASC' + + end + end + end +end diff --git a/app/controllers/refinery/admin/blog/comments_controller.rb b/app/controllers/refinery/admin/blog/comments_controller.rb new file mode 100644 index 0000000..9f78caa --- /dev/null +++ b/app/controllers/refinery/admin/blog/comments_controller.rb @@ -0,0 +1,42 @@ +module Refinery + module Admin + module Blog + class CommentsController < ::Admin::BaseController + + crudify :'refinery/blog_comment', + :title_attribute => :name, + :order => 'published_at DESC' + + def index + @blog_comments = Refinery::BlogComment.unmoderated + render :action => 'index' + end + + def approved + unless params[:id].present? + @blog_comments = Refinery::BlogComment.approved + render :action => 'index' + else + @blog_comment = Refinery::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 = Refinery::BlogComment.rejected + render :action => 'index' + else + @blog_comment = Refinery::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 + end +end diff --git a/app/controllers/refinery/admin/blog/posts_controller.rb b/app/controllers/refinery/admin/blog/posts_controller.rb new file mode 100644 index 0000000..a934a15 --- /dev/null +++ b/app/controllers/refinery/admin/blog/posts_controller.rb @@ -0,0 +1,99 @@ +module Refinery + module Admin + module Blog + class PostsController < ::Admin::BaseController + + + crudify :'refinery/blog_post', + :title_attribute => :title, + :order => 'published_at DESC' + + def uncategorized + @blog_posts = Refinery::BlogPost.uncategorized.paginate({ + :page => params[:page], + :per_page => Refinery::BlogPost.per_page + }) + end + + def tags + op = case ActiveRecord::Base.connection.adapter_name.downcase + when 'postgresql' + '~*' + else + 'LIKE' + end + wildcard = case ActiveRecord::Base.connection.adapter_name.downcase + when 'postgresql' + '.*' + else + '%' + end + @tags = Refinery::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 Refinery::BlogPost.column_names.include?("position") + params[:blog_post].merge!({ + :position => ((Refinery::BlogPost.maximum(:position, :conditions => "")||-1) + 1) + }) + end + + if Refinery::BlogPost.column_names.include?("user_id") + params[:blog_post].merge!({ + :user_id => current_user.id + }) + end + + if (@blog_post = Refinery::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 + render :text => "<script>parent.window.location = '#{admin_blog_posts_url}';</script>" + end + else + unless request.xhr? + render :action => 'new' + else + render :partial => "/shared/admin/error_messages", + :locals => { + :object => @blog_post, + :include_object_name => true + } + end + end + end + + before_filter :find_all_categories, + :only => [:new, :edit, :create, :update] + + before_filter :check_category_ids, :only => :update + + protected + def find_all_categories + @blog_categories = Refinery::BlogCategory.find(:all) + end + + def check_category_ids + params[:blog_post][:category_ids] ||= [] + end + end + end + end +end diff --git a/app/controllers/refinery/admin/blog/settings_controller.rb b/app/controllers/refinery/admin/blog/settings_controller.rb new file mode 100644 index 0000000..469a1dc --- /dev/null +++ b/app/controllers/refinery/admin/blog/settings_controller.rb @@ -0,0 +1,55 @@ +module Refinery + module Admin + module Blog + class SettingsController < ::Admin::BaseController + + def notification_recipients + @recipients = Refinery::BlogComment::Notification.recipients + + if request.post? + Refinery::BlogComment::Notification.recipients = params[:recipients] + flash[:notice] = t('updated', :scope => 'admin.blog.settings.notification_recipients', + :recipients => Refinery::BlogComment::Notification.recipients) + unless request.xhr? or from_dialog? + redirect_back_or_default(admin_blog_posts_path) + else + render :text => "<script type='text/javascript'>parent.window.location = '#{admin_blog_posts_path}';</script>", + :layout => false + end + end + end + + def moderation + enabled = Refinery::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 = Refinery::BlogComment.toggle! + unless request.xhr? + redirect_back_or_default(admin_blog_posts_path) + else + render :json => {:enabled => enabled}, + :layout => false + end + end + + def teasers + enabled = Refinery::BlogPost.teaser_enabled_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 diff --git a/app/controllers/refinery/blog/categories_controller.rb b/app/controllers/refinery/blog/categories_controller.rb new file mode 100644 index 0000000..2c8c4c5 --- /dev/null +++ b/app/controllers/refinery/blog/categories_controller.rb @@ -0,0 +1,15 @@ +module Refinery + module Blog + class CategoriesController < BlogController + + def show + @category = Refinery::BlogCategory.find(params[:id]) + @blog_posts = @category.posts.live.includes(:comments, :categories).paginate({ + :page => params[:page], + :per_page => Refinery::Setting.find_or_set(:blog_posts_per_page, 10) + }) + end + + end + end +end diff --git a/app/controllers/refinery/blog/posts_controller.rb b/app/controllers/refinery/blog/posts_controller.rb new file mode 100644 index 0000000..bdf53f0 --- /dev/null +++ b/app/controllers/refinery/blog/posts_controller.rb @@ -0,0 +1,107 @@ +module Refinery + module Blog + class PostsController < BlogController + + before_filter :find_all_blog_posts, :except => [:archive] + before_filter :find_blog_post, :only => [:show, :comment, :update_nav] + before_filter :find_tags + + respond_to :html, :js, :rss + + def index + # Rss feeders are greedy. Let's give them every blog post instead of paginating. + (@blog_posts = Refinery::BlogPost.live.includes(:comments, :categories).all) if request.format.rss? + respond_with (@blog_posts) do |format| + format.html + format.rss + end + end + + def show + @blog_comment = Refinery::BlogComment.new + + respond_with (@blog_post) do |format| + format.html { present(@blog_post) } + format.js { render :partial => 'post', :layout => false } + end + end + + def comment + if (@blog_comment = @blog_post.comments.create(params[:blog_comment])).valid? + if Refinery::BlogComment::Moderation.enabled? or @blog_comment.ham? + begin + Refinery::Blog::CommentMailer.notification(@blog_comment, request).deliver + rescue + logger.warn "There was an error delivering a blog comment notification.\n#{$!}\n" + end + end + + if Refinery::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 => Refinery::Setting.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 = Refinery::live.by_year(@archive_date).paginate({ + :page => params[:page], + :per_page => Refinery::Setting.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 = Refinery::BlogPost.tagged_with(@tag_name).paginate({ + :page => params[:page], + :per_page => Refinery::Setting.find_or_set(:blog_posts_per_page, 10) + }) + end + + protected + + def find_blog_post + unless (@blog_post = Refinery::BlogPost.find(params[:id])).try(:live?) + if refinery_user? and current_user.authorized_plugins.include?("refinerycms_blog") + @blog_post = Refinery::BlogPost.find(params[:id]) + else + error_404 + end + end + end + + def find_all_blog_posts + @blog_posts = Refinery::BlogPost.live.includes(:comments, :categories).paginate({ + :page => params[:page], + :per_page => Refinery::Setting.find_or_set(:blog_posts_per_page, 10) + }) + end + + def find_tags + @tags = Refinery::BlogPost.tag_counts_on(:tags) + end + + end + end +end diff --git a/app/controllers/refinery/blog_controller.rb b/app/controllers/refinery/blog_controller.rb new file mode 100644 index 0000000..6bec0a5 --- /dev/null +++ b/app/controllers/refinery/blog_controller.rb @@ -0,0 +1,18 @@ +module Refinery + class BlogController < ::ApplicationController + + helper :blog_posts + before_filter :find_page, :find_all_blog_categories + + protected + + def find_page + @page = Refinery::Page.find_by_link_url("/blog") + end + + def find_all_blog_categories + @blog_categories = Refinery::BlogCategory.all + end + + end +end diff --git a/app/mailers/blog/comment_mailer.rb b/app/mailers/blog/comment_mailer.rb deleted file mode 100644 index 3710ea1..0000000 --- a/app/mailers/blog/comment_mailer.rb +++ /dev/null @@ -1,13 +0,0 @@ -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 - - end -end
\ No newline at end of file diff --git a/app/mailers/refinery/blog/comment_mailer.rb b/app/mailers/refinery/blog/comment_mailer.rb new file mode 100644 index 0000000..f36ea29 --- /dev/null +++ b/app/mailers/refinery/blog/comment_mailer.rb @@ -0,0 +1,15 @@ +module Refinery + 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 + + end + end +end diff --git a/app/models/blog/comment_mailer.rb b/app/models/blog/comment_mailer.rb deleted file mode 100644 index acef313..0000000 --- a/app/models/blog/comment_mailer.rb +++ /dev/null @@ -1 +0,0 @@ -require File.expand_path('../../../mailers/blog/comment_mailer', __FILE__) diff --git a/app/models/blog_category.rb b/app/models/blog_category.rb deleted file mode 100644 index b66a94d..0000000 --- a/app/models/blog_category.rb +++ /dev/null @@ -1,19 +0,0 @@ -class BlogCategory < ActiveRecord::Base - - has_many :categorizations, :dependent => :destroy - has_many :posts, :through => :categorizations, :source => :blog_post - - acts_as_indexed :fields => [:title] - - validates :title, :presence => true, :uniqueness => true - - has_friendly_id :title, :use_slug => true, - :default_locale => (::Refinery::I18n.default_frontend_locale rescue :en), - :approximate_ascii => RefinerySetting.find_or_set(:approximate_ascii, false, :scoping => 'blog'), - :strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => 'blog') - - def post_count - posts.select(&:live?).count - end - -end diff --git a/app/models/blog_comment.rb b/app/models/blog_comment.rb deleted file mode 100644 index 6216cc2..0000000 --- a/app/models/blog_comment.rb +++ /dev/null @@ -1,131 +0,0 @@ -class BlogComment < ActiveRecord::Base - - attr_accessible :name, :email, :message - - filters_spam :author_field => :name, - :email_field => :email, - :message_field => :body - - belongs_to :post, :class_name => 'BlogPost', :foreign_key => 'blog_post_id' - - acts_as_indexed :fields => [:name, :email, :message] - - alias_attribute :message, :body - - validates :name, :message, :presence => true - validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } - - scope :unmoderated, :conditions => {:state => nil} - scope :approved, :conditions => {:state => 'approved'} - scope :rejected, :conditions => {:state => 'rejected'} - - def avatar_url(options = {}) - options = {:size => 60} - require 'digest/md5' - size = ("?s=#{options[:size]}" if options[:size]) - "http://gravatar.com/avatar/#{Digest::MD5.hexdigest(self.email.to_s.strip.downcase)}#{size}.jpg" - end - - def approve! - self.update_attribute(:state, 'approved') - end - - def reject! - self.update_attribute(:state, 'rejected') - end - - def rejected? - self.state == 'rejected' - end - - def approved? - self.state == 'approved' - end - - def unmoderated? - 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' - end - end - - module Moderation - class << self - def enabled? - RefinerySetting.find_or_set(:comment_moderation, true, { - :scoping => 'blog', - :restricted => false - }) - end - - def toggle! - new_value = { - :value => !BlogComment::Moderation.enabled?, - :scoping => 'blog', - :restricted => false - } - if RefinerySetting.respond_to?(:set) - RefinerySetting.set(:comment_moderation, new_value) - else - RefinerySetting[:comment_moderation] = new_value - end - end - end - end - - module Notification - class << self - def recipients - RefinerySetting.find_or_set(:comment_notification_recipients, (Role[:refinery].users.first.email rescue ''), - { - :scoping => 'blog', - :restricted => false - }) - end - - def recipients=(emails) - new_value = { - :value => emails, - :scoping => 'blog', - :restricted => false - } - if RefinerySetting.respond_to?(:set) - RefinerySetting.set(:comment_notification_recipients, new_value) - else - RefinerySetting[:comment_notification_recipients] = new_value - end - end - - def subject - RefinerySetting.find_or_set(:comment_notification_subject, "New inquiry from your website", { - :scoping => 'blog', - :restricted => false - }) - end - - def subject=(subject_line) - new_value = { - :value => subject_line, - :scoping => 'blog', - :restricted => false - } - if RefinerySetting.respond_to?(:set) - RefinerySetting.set(:comment_notification_subject, new_value) - else - RefinerySetting[:comment_notification_subject] = new_value - end - end - end - end - -end diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb deleted file mode 100644 index e03be99..0000000 --- a/app/models/blog_post.rb +++ /dev/null @@ -1,114 +0,0 @@ -require 'acts-as-taggable-on' -require 'seo_meta' - -class BlogPost < ActiveRecord::Base - - 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 => 'User', :foreign_key => :user_id, :readonly => true - - has_many :comments, :class_name => 'BlogComment', :dependent => :destroy - acts_as_taggable - - has_many :categorizations, :dependent => :destroy - has_many :categories, :through => :categorizations, :source => :blog_category - - acts_as_indexed :fields => [:title, :body] - - validates :title, :presence => true, :uniqueness => true - validates :body, :presence => true - - has_friendly_id :friendly_id_source, :use_slug => true, - :default_locale => (::Refinery::I18n.default_frontend_locale rescue :en), - :approximate_ascii => RefinerySetting.find_or_set(:approximate_ascii, false, :scoping => 'blog'), - :strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => 'blog') - - scope :by_archive, lambda { |archive_date| - 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, lambda { where(['published_at <= ?', Time.now.beginning_of_month]) } - - 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 - BlogPost.next(self).first - end - - def prev - BlogPost.previous(self).first - 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 - }.compact - end - - def friendly_id_source - custom_url.present? ? custom_url : title - 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 teasers_enabled? - RefinerySetting.find_or_set(:teasers_enabled, true, { - :scoping => 'blog' - }) - end - - def teaser_enabled_toggle! - currently = RefinerySetting.find_or_set(:teasers_enabled, true, { - :scoping => 'blog' - }) - RefinerySetting.set(:teasers_enabled, {:value => !currently, :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 - def key - RefinerySetting.find_or_set(:share_this_key, BlogPost::ShareThis::DEFAULT_KEY, { - :scoping => 'blog' - }) - end - - def enabled? - key = BlogPost::ShareThis.key - key.present? and key != BlogPost::ShareThis::DEFAULT_KEY - end - end - end - -end diff --git a/app/models/categorization.rb b/app/models/categorization.rb deleted file mode 100644 index 1735ea9..0000000 --- a/app/models/categorization.rb +++ /dev/null @@ -1,7 +0,0 @@ -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 diff --git a/app/models/refinery/blog/comment_mailer.rb b/app/models/refinery/blog/comment_mailer.rb new file mode 100644 index 0000000..7013355 --- /dev/null +++ b/app/models/refinery/blog/comment_mailer.rb @@ -0,0 +1 @@ +require File.expand_path('../../../../mailers/refinery/blog/comment_mailer', __FILE__) diff --git a/app/models/refinery/blog_category.rb b/app/models/refinery/blog_category.rb new file mode 100644 index 0000000..7a35e67 --- /dev/null +++ b/app/models/refinery/blog_category.rb @@ -0,0 +1,21 @@ +module Refinery + class BlogCategory < ActiveRecord::Base + + has_many :categorizations, :dependent => :destroy + has_many :posts, :through => :categorizations, :source => :blog_post + + acts_as_indexed :fields => [:title] + + validates :title, :presence => true, :uniqueness => true + + has_friendly_id :title, :use_slug => true, + :default_locale => (::Refinery::I18n.default_frontend_locale rescue :en), + :approximate_ascii => Refinery::Setting.find_or_set(:approximate_ascii, false, :scoping => 'blog'), + :strip_non_ascii => Refinery::Setting.find_or_set(:strip_non_ascii, false, :scoping => 'blog') + + def post_count + posts.select(&:live?).count + end + + end +end diff --git a/app/models/refinery/blog_comment.rb b/app/models/refinery/blog_comment.rb new file mode 100644 index 0000000..e5869a8 --- /dev/null +++ b/app/models/refinery/blog_comment.rb @@ -0,0 +1,133 @@ +module Refinery + class BlogComment < ActiveRecord::Base + + attr_accessible :name, :email, :message + + filters_spam :author_field => :name, + :email_field => :email, + :message_field => :body + + belongs_to :post, :class_name => 'Refinery::BlogPost', :foreign_key => 'blog_post_id' + + acts_as_indexed :fields => [:name, :email, :message] + + alias_attribute :message, :body + + validates :name, :message, :presence => true + validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } + + scope :unmoderated, :conditions => {:state => nil} + scope :approved, :conditions => {:state => 'approved'} + scope :rejected, :conditions => {:state => 'rejected'} + + def avatar_url(options = {}) + options = {:size => 60} + require 'digest/md5' + size = ("?s=#{options[:size]}" if options[:size]) + "http://gravatar.com/avatar/#{Digest::MD5.hexdigest(self.email.to_s.strip.downcase)}#{size}.jpg" + end + + def approve! + self.update_attribute(:state, 'approved') + end + + def reject! + self.update_attribute(:state, 'rejected') + end + + def rejected? + self.state == 'rejected' + end + + def approved? + self.state == 'approved' + end + + def unmoderated? + self.state.nil? + end + + def self.toggle! + currently = Refinery::Setting.find_or_set(:comments_allowed, true, { + :scoping => 'blog' + }) + Refinery::Setting.set(:comments_allowed, {:value => !currently, :scoping => 'blog'}) + end + + before_create do |comment| + unless BlogComment::Moderation.enabled? + comment.state = comment.ham? ? 'approved' : 'rejected' + end + end + + module Moderation + class << self + def enabled? + Refinery::Setting.find_or_set(:comment_moderation, true, { + :scoping => 'blog', + :restricted => false + }) + end + + def toggle! + new_value = { + :value => !BlogComment::Moderation.enabled?, + :scoping => 'blog', + :restricted => false + } + if Refinery::Setting.respond_to?(:set) + Refinery::Setting.set(:comment_moderation, new_value) + else + Refinery::Setting[:comment_moderation] = new_value + end + end + end + end + + module Notification + class << self + def recipients + Refinery::Setting.find_or_set(:comment_notification_recipients, (Refinery::Role[:refinery].users.first.email rescue ''), + { + :scoping => 'blog', + :restricted => false + }) + end + + def recipients=(emails) + new_value = { + :value => emails, + :scoping => 'blog', + :restricted => false + } + if Refinery::Setting.respond_to?(:set) + Refinery::Setting.set(:comment_notification_recipients, new_value) + else + Refinery::Setting[:comment_notification_recipients] = new_value + end + end + + def subject + Refinery::Setting.find_or_set(:comment_notification_subject, "New inquiry from your website", { + :scoping => 'blog', + :restricted => false + }) + end + + def subject=(subject_line) + new_value = { + :value => subject_line, + :scoping => 'blog', + :restricted => false + } + if Refinery::Setting.respond_to?(:set) + Refinery::Setting.set(:comment_notification_subject, new_value) + else + Refinery::Setting[:comment_notification_subject] = new_value + end + end + end + end + + end +end
\ No newline at end of file diff --git a/app/models/refinery/blog_post.rb b/app/models/refinery/blog_post.rb new file mode 100644 index 0000000..12ce6f2 --- /dev/null +++ b/app/models/refinery/blog_post.rb @@ -0,0 +1,116 @@ +require 'acts-as-taggable-on' +require 'seo_meta' + +module Refinery + class BlogPost < ActiveRecord::Base + + 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::BlogComment', :dependent => :destroy + acts_as_taggable + + has_many :categorizations, :dependent => :destroy + has_many :categories, :through => :categorizations, :source => :blog_category + + acts_as_indexed :fields => [:title, :body] + + validates :title, :presence => true, :uniqueness => true + validates :body, :presence => true + + has_friendly_id :friendly_id_source, :use_slug => true, + :default_locale => (::Refinery::I18n.default_frontend_locale rescue :en), + :approximate_ascii => Refinery::Setting.find_or_set(:approximate_ascii, false, :scoping => 'blog'), + :strip_non_ascii => Refinery::Setting.find_or_set(:strip_non_ascii, false, :scoping => 'blog') + + scope :by_archive, lambda { |archive_date| + 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, lambda { where(['published_at <= ?', Time.now.beginning_of_month]) } + + 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 + BlogPost.next(self).first + end + + def prev + BlogPost.previous(self).first + 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::BlogCategory.find(c_id.to_i) rescue nil + }.compact + end + + def friendly_id_source + custom_url.present? ? custom_url : title + 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? + Refinery::Setting.find_or_set(:comments_allowed, true, { + :scoping => 'blog' + }) + end + + def teasers_enabled? + Refinery::Setting.find_or_set(:teasers_enabled, true, { + :scoping => 'blog' + }) + end + + def teaser_enabled_toggle! + currently = Refinery::Setting.find_or_set(:teasers_enabled, true, { + :scoping => 'blog' + }) + Refinery::Setting.set(:teasers_enabled, {:value => !currently, :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 + def key + Refinery::Setting.find_or_set(:share_this_key, BlogPost::ShareThis::DEFAULT_KEY, { + :scoping => 'blog' + }) + end + + def enabled? + key = BlogPost::ShareThis.key + key.present? and key != BlogPost::ShareThis::DEFAULT_KEY + end + end + end + + end +end diff --git a/app/models/refinery/categorization.rb b/app/models/refinery/categorization.rb new file mode 100644 index 0000000..c4feaec --- /dev/null +++ b/app/models/refinery/categorization.rb @@ -0,0 +1,9 @@ +module Refinery + class Categorization < ActiveRecord::Base + + set_table_name 'refinery_blog_categories_blog_posts' + belongs_to :blog_post + belongs_to :blog_category + + end +end
\ No newline at end of file |