diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/refinery/blog/backend.js | 73 | ||||
-rw-r--r-- | app/controllers/refinery/blog/categories_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/refinery/blog/posts_controller.rb | 14 | ||||
-rw-r--r-- | app/helpers/refinery/blog/controller_helper.rb | 43 | ||||
-rw-r--r-- | app/models/refinery/blog/comment.rb | 28 | ||||
-rw-r--r-- | app/models/refinery/blog/post.rb | 51 | ||||
-rw-r--r-- | app/views/refinery/blog/admin/posts/_form.html.erb | 2 | ||||
-rw-r--r-- | app/views/refinery/shared/admin/_autocomplete.html.erb | 45 |
8 files changed, 136 insertions, 122 deletions
diff --git a/app/assets/javascripts/refinery/blog/backend.js b/app/assets/javascripts/refinery/blog/backend.js index 77529c5..d4869c1 100644 --- a/app/assets/javascripts/refinery/blog/backend.js +++ b/app/assets/javascripts/refinery/blog/backend.js @@ -48,68 +48,23 @@ $(document).ready(function(){ e.preventDefault(); }); - - $(function() { - $('#page-tabs').tabs(); - $('#copy_body_link').click(function(event) { - // Find the WYMEditor that maps to the custom_teaser field - var teaserTextArea = $('#post_custom_teaser')[0]; - var teaserEditor = null; - $.each(WYMeditor.INSTANCES, function(index, editor) { - if (editor._element[0] == teaserTextArea) { - teaserEditor = editor; - } - }); - - if (teaserEditor) { - teaserEditor.html($('#post_body').attr('value')); + $('#page-tabs').tabs(); + $('#copy_body_link').click(function(event) { + // Find the WYMEditor that maps to the custom_teaser field + var teaserTextArea = $('#post_custom_teaser')[0]; + var teaserEditor = null; + $.each(WYMeditor.INSTANCES, function(index, editor) { + if (editor._element[0] == teaserTextArea) { + teaserEditor = editor; } - - event.preventDefault(); }); - }); - - function split( val ) { - return val.split( /,\s*/ ); - } - function extractLast( term ) { - return split( term ).pop(); - } + if (teaserEditor) { + teaserEditor.html($('#post_body').attr('value')); + } - page_options.init(false, '', '') + event.preventDefault(); + }); - $('<%= dom_id %>').bind( "keydown", function( event ) { - if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) { - event.preventDefault() - } - }).autocomplete({ - source: function( request, response ) { - $.getJSON( "<%= url %>", { - term: extractLast( request.term ) - }, response ); - }, - search: function() { - // custom minLength - var term = extractLast( this.value ); - if ( term.length < 2 ) { - return false; - } - }, - focus: function() { - // prevent value inserted on focus - return false; - }, - select: function( event, ui ) { - var terms = split( this.value ); - // remove the current input - terms.pop(); - // add the selected item - terms.push( ui.item.value ); - // add placeholder to get the comma-and-space at the end - terms.push( "" ); - this.value = terms.join( ", " ); - return false; - } - }) + page_options.init(false, '', ''); }); diff --git a/app/controllers/refinery/blog/categories_controller.rb b/app/controllers/refinery/blog/categories_controller.rb index 23a835a..b5ab574 100644 --- a/app/controllers/refinery/blog/categories_controller.rb +++ b/app/controllers/refinery/blog/categories_controller.rb @@ -3,7 +3,7 @@ module Refinery class CategoriesController < BlogController def show - @category = Refinery::Blog::Category.find(params[:id]) + @category = Refinery::Blog::Category.friendly.find(params[:id]) @posts = @category.posts.live.includes(:comments, :categories).with_globalize.page(params[:page]) end diff --git a/app/controllers/refinery/blog/posts_controller.rb b/app/controllers/refinery/blog/posts_controller.rb index 05cd2d6..20ac12b 100644 --- a/app/controllers/refinery/blog/posts_controller.rb +++ b/app/controllers/refinery/blog/posts_controller.rb @@ -2,7 +2,7 @@ module Refinery module Blog class PostsController < BlogController - before_filter :find_all_blog_posts, :except => [:archive] + before_filter :paginate_all_blog_posts, :except => [:archive] before_filter :find_blog_post, :only => [:show, :comment, :update_nav] before_filter :find_tags @@ -10,9 +10,12 @@ module Refinery def index if request.format.rss? - @posts = Post.live.includes(:comments, :categories) - # limit rss feed for services (like feedburner) who have max size - @posts = Post.recent(params["max_results"]) if params["max_results"].present? + @posts = if params["max_results"].present? + # limit rss feed for services (like feedburner) who have max size + Post.recent(params["max_results"]) + else + Post.newest_first.live.includes(:comments, :categories) + end end respond_with (@posts) do |format| format.html @@ -34,7 +37,8 @@ module Refinery end def comment - if (@comment = @post.comments.create(params[:comment])).valid? + @comment = @post.comments.create(params[:comment]) + if @comment.valid? if Comment::Moderation.enabled? or @comment.ham? begin CommentMailer.notification(@comment, request).deliver diff --git a/app/helpers/refinery/blog/controller_helper.rb b/app/helpers/refinery/blog/controller_helper.rb index 87d5447..75c351a 100644 --- a/app/helpers/refinery/blog/controller_helper.rb +++ b/app/helpers/refinery/blog/controller_helper.rb @@ -4,26 +4,37 @@ module Refinery protected - def find_blog_post - unless (@post = Refinery::Blog::Post.with_globalize.find(params[:id])).try(:live?) - if refinery_user? and current_refinery_user.authorized_plugins.include?("refinerycms_blog") - @post = Refinery::Blog::Post.find(params[:id]) - else - error_404 - end + def find_blog_post + @post = all_blog_posts.friendly.find(params[:id]) + unless @post.try(:live?) + if refinery_user? && current_refinery_user.authorized_plugins.include?("refinerycms_blog") + @post = Post.friendly.find(params[:id]) + else + error_404 end end + end - def find_all_blog_posts - @posts = Refinery::Blog::Post.live.includes(:comments, :categories).with_globalize.page(params[:page]) - end + def find_all_blog_posts + @posts = all_blog_posts.live + end - def find_tags - @tags = Refinery::Blog::Post.tag_counts_on(:tags) - end - def find_all_blog_categories - @categories = Refinery::Blog::Category.translated - end + def paginate_all_blog_posts + @posts = find_all_blog_posts.page(params[:page]) + end + + def find_tags + @tags = Post.tag_counts_on(:tags) + end + + def find_all_blog_categories + @categories = Category.translated + end + + private + def all_blog_posts + Post.newest_first.includes(:comments, :categories).with_globalize + end end end end diff --git a/app/models/refinery/blog/comment.rb b/app/models/refinery/blog/comment.rb index 7661e81..ae35a59 100644 --- a/app/models/refinery/blog/comment.rb +++ b/app/models/refinery/blog/comment.rb @@ -4,28 +4,26 @@ module Refinery attr_accessible :name, :email, :message - filters_spam :author_field => :name, - :email_field => :email, - :message_field => :body + filters_spam author_field: :name, email_field: :email, message_field: :body - belongs_to :post, :foreign_key => 'blog_post_id' + belongs_to :post, foreign_key: 'blog_post_id' alias_attribute :message, :body - validates :name, :message, :presence => true - validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } + validates :name, :message, presence: true + validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i } class << self def unmoderated - where(:state => nil) + where(state: nil) end def approved - where(:state => 'approved') + where(state: 'approved') end def rejected - where(:state => 'rejected') + where(state: 'rejected') end end @@ -55,7 +53,7 @@ module Refinery currently = Refinery::Setting.find_or_set(:comments_allowed, true, { :scoping => 'blog' }) - Refinery::Setting.set(:comments_allowed, {:value => !currently, :scoping => 'blog'}) + Refinery::Setting.set(:comments_allowed, {value: !currently, scoping: 'blog'}) end before_create do |comment| @@ -68,16 +66,16 @@ module Refinery class << self def enabled? Refinery::Setting.find_or_set(:comment_moderation, true, { - :scoping => 'blog', - :restricted => false + scoping: 'blog', + restricted: false }) end def toggle! new_value = { - :value => !Blog::Comment::Moderation.enabled?, - :scoping => 'blog', - :restricted => false + value: !Blog::Comment::Moderation.enabled?, + scoping: 'blog', + restricted: false } Refinery::Setting.set(:comment_moderation, new_value) end diff --git a/app/models/refinery/blog/post.rb b/app/models/refinery/blog/post.rb index f0278f4..11aa5b8 100644 --- a/app/models/refinery/blog/post.rb +++ b/app/models/refinery/blog/post.rb @@ -12,9 +12,7 @@ module Refinery is_seo_meta if self.table_exists? - default_scope :order => 'published_at DESC' - - belongs_to :author, :class_name => Refinery::Blog.user_class.to_s, :foreign_key => :user_id, :readonly => true + belongs_to :author, proc{ readonly(true) }, :class_name => Refinery::Blog.user_class.to_s, :foreign_key => :user_id has_many :comments, :dependent => :destroy, :foreign_key => :blog_post_id acts_as_taggable @@ -37,18 +35,15 @@ module Refinery attr_accessible :source_url, :source_url_title attr_accessor :locale - - class Translation - is_seo_meta - attr_accessible :browser_title, :meta_description, :locale - end + class Translation + is_seo_meta + attr_accessible :browser_title, :meta_description, :locale + end # Delegate SEO Attributes to globalize3 translation seo_fields = ::SeoMeta.attributes.keys.map{|a| [a, :"#{a}="]}.flatten delegate(*(seo_fields << {:to => :translation})) - before_save { |m| m.translation.save } - self.per_page = Refinery::Blog.posts_per_page def next @@ -60,7 +55,7 @@ module Refinery end def live? - !draft and published_at <= Time.now + !draft && published_at <= Time.now end def friendly_id_source @@ -79,7 +74,8 @@ module Refinery end end # A join implies readonly which we don't really want. - joins(:translations).where(globalized_conditions).where(conditions).readonly(false) + where(conditions).joins(:translations).where(globalized_conditions) + .readonly(false) end def find_by_slug_or_id(slug_or_id) @@ -91,44 +87,49 @@ module Refinery end def by_month(date) - where(:published_at => date.beginning_of_month..date.end_of_month) + newest_first.where(:published_at => date.beginning_of_month..date.end_of_month) end - def by_archive(date) - Refinery.deprecate("Refinery::Blog::Post.by_archive(date)", {:replacement => "Refinery::Blog::Post.by_month(date)", :when => 2.2 }) - by_month(date) + def by_year(date) + newest_first.where(:published_at => date.beginning_of_year..date.end_of_year).with_globalize end - def by_year(date) - where(:published_at => date.beginning_of_year..date.end_of_year).with_globalize + def newest_first + order("published_at DESC") end def published_dates_older_than(date) - published_before(date).select(:published_at).map(&:published_at) + newest_first.published_before(date).select(:published_at).map(&:published_at) end def recent(count) - live.limit(count) + newest_first.live.limit(count) end def popular(count) - unscoped.order("access_count DESC").limit(count).with_globalize + order("access_count DESC").limit(count).with_globalize end def previous(item) - published_before(item.published_at).first + newest_first.published_before(item.published_at).first end def uncategorized - live.includes(:categories).where(Refinery::Blog::Categorization.table_name => { :blog_category_id => nil }) + newest_first.live.includes(:categories).where( + Refinery::Blog::Categorization.table_name => { :blog_category_id => nil } + ) end def next(current_record) - where(["published_at > ? and draft = ?", current_record.published_at, false]).reorder('published_at ASC').with_globalize.first + where(arel_table[:published_at].gt(current_record.published_at)) + .where(:draft => false) + .order('published_at ASC').with_globalize.first end def published_before(date=Time.now) - where("published_at < ? and draft = ?", date, false).with_globalize + where(arel_table[:published_at].lt(date)) + .where(:draft => false) + .with_globalize end alias_method :live, :published_before diff --git a/app/views/refinery/blog/admin/posts/_form.html.erb b/app/views/refinery/blog/admin/posts/_form.html.erb index f9666ba..b2a31e1 100644 --- a/app/views/refinery/blog/admin/posts/_form.html.erb +++ b/app/views/refinery/blog/admin/posts/_form.html.erb @@ -112,5 +112,5 @@ <% content_for :stylesheets, stylesheet_link_tag('refinery/blog/backend') %> <% content_for :javascripts, javascript_include_tag('refinery/blog/backend') %> <%= render 'refinery/shared/admin/autocomplete', - :dom_id => '#blog_post_tag_list', + :dom_id => '#post_tag_list', :url => refinery.tags_blog_admin_posts_url %> diff --git a/app/views/refinery/shared/admin/_autocomplete.html.erb b/app/views/refinery/shared/admin/_autocomplete.html.erb index 97d4ac8..1131f89 100644 --- a/app/views/refinery/shared/admin/_autocomplete.html.erb +++ b/app/views/refinery/shared/admin/_autocomplete.html.erb @@ -1 +1,46 @@ <% content_for :stylesheets, stylesheet_link_tag("refinery/blog/ui-lightness/jquery-ui-1.8.13.custom") %> +<% content_for :javascripts do %> +<script> + function split(val) { + return val.split(/,\s*/); + } + function extractLast(term) { + return split(term).pop(); + } + $(document).ready(function(){ + $('<%= dom_id %>').bind("keydown", function(event) { + if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) { + event.preventDefault() + } + }).autocomplete({ + source: function(request, response) { + $.getJSON("<%= url %>", { + term: extractLast(request.term) + }, response); + }, + search: function() { + // custom minLength + var term = extractLast(this.value); + if (term.length < 2) { + return false; + } + }, + focus: function() { + // prevent value inserted on focus + return false; + }, + select: function(event, ui) { + var terms = split(this.value); + // remove the current input + terms.pop(); + // add the selected item + terms.push(ui.item.value); + // add placeholder to get the comma-and-space at the end + terms.push(""); + this.value = terms.join(", "); + return false; + } + }); + }) +</script> +<% end %> |