class PostsController < ApplicationController include BlogUrlHelpers respond_to :html, :json, :rss before_action :load_blog before_action :load_page_number, :only => [:index, :archive, :category] before_action :find_post, :only => [:show, :create_comment] def index @posts = SpudPost.for_user(current_user).visible.for_blog(params[:blog_key]).ordered.paginate(:page => @page_number, :per_page => Spud::Blog.config.posts_per_page) if params[:search] @posts = @posts.search(params[:search]) end respond_with @posts do |format| format.html{ render view_for_action('index') } end end # The sole purpose of this action is to redirect from a POST to an seo-friendly url def filter if !params[:category_url_name].blank? && !params[:archive_date].blank? redirect_to post_category_archive_path(params[:category_url_name], params[:archive_date]) elsif !params[:category_url_name].blank? redirect_to post_category_path(params[:category_url_name]) elsif !params[:archive_date].blank? redirect_to post_archive_path(params[:archive_date]) else redirect_to posts_path end end def category if @post_category = SpudPostCategory.find_by_url_name(params[:category_url_name]) @posts = @post_category.posts.for_user(current_user).visible.for_blog(params[:blog_key]).from_archive(params[:archive_date]).paginate(:page => @page_number, :per_page => Spud::Blog.config.posts_per_page) else redirect_to posts_path return end respond_with @posts do |format| format.html { render view_for_action 'index' } end end def archive @posts = SpudPost.for_user(current_user).visible.for_blog(params[:blog_key]).from_archive(params[:archive_date]).paginate(:page => @page_number, :per_page => Spud::Blog.config.posts_per_page) respond_with @posts do |format| format.html { render view_for_action 'index' } end end def show if @post.comments_enabled @comment = SpudPostComment.new() end respond_with @post do |format| format.html { render view_for_action 'show' } end end def create_comment unless params[:comment_validation].blank? # trap spam bots render :nothing => true return end @comment = @post.comments.new(comment_params) @comment.user_agent = request.env["HTTP_USER_AGENT"] @comment.user_ip = request.remote_ip @comment.referrer = request.referrer @comment.approved = Spud::Blog.default_comment_approval @comment.permalink = post_path(@post.url_name) if @comment.save if @comment.approved? flash[:notice] = 'Your comment has been posted.' else flash[:notice] = 'Your comment has been saved and will appear after being reviewed by an administrator.' end @comment = SpudPostComment.new() end respond_with @comment do |format| format.html { render view_for_action 'show' } end end private def find_post @post = SpudPost.for_user(current_user).for_blog(params[:blog_key]).where(:url_name => params[:id]).first if @post.blank? || @post.is_private? raise Spud::NotFoundError.new(:item => 'post') end end def comment_params params.require(:spud_post_comment).permit(:author, :content) end def load_page_number page = 1 if params[:page].present? page = params[:page].to_i if page.to_s != params[:page].to_s if(page > 1) redirect_to posts_path(:page => page), :status => :moved_permanently and return else redirect_to posts_path(:page => nil), :status => :moved_permanently and return end end end @page_number = page end def load_blog @config = SpudBlogConfig.find(params[:blog_key]) if @config.blank? raise Spud::NotFoundError(:item => 'blog') return false else self.class.layout(@config.layout) end end def view_for_action(action) if File.exist?(Rails.root.join("app/views/#{@config.key}/#{action}.html.erb")) return "#{@config.key}/#{action}" else return action end end end