class Admin::PostsController < Admin::ApplicationController include BlogUrlHelpers respond_to :html, :xml, :json before_action :load_blog before_action :find_post, :only => [:show, :edit, :update, :destroy] def index @posts = SpudPost.for_blog(params[:blog_key]).ordered.paginate(:page => params[:page], :per_page => 15) if params[:search] @posts = @posts.search(params[:search]) end respond_with @posts end def edit respond_with @post end def update params[:spud_post][:updated_at] = Time.now() if @post.update_attributes(post_params) flash[:notice] = 'Post was successfully updated.' end respond_with @post, :location => admin_posts_path end def new @categories = SpudPostCategory.ordered @post = SpudPost.new(:published_at => Time.zone.now, :spud_user_id => current_user.id) respond_with @post end def create @categories = SpudPostCategory.ordered @post = SpudPost.new(post_params) @post.blog_key = params[:blog_key] if @post.save flash[:notice] = 'Post was successfully created.' end respond_with @post, :location => admin_posts_path end def destroy if @post.destroy flash[:notice] = 'Post was successfully deleted.' end respond_with @post, :location => admin_posts_path end private def find_post @post = SpudPost.find(params[:id]) if @post.blank? flash[:error] = "Post not found!" redirect_to admin_posts_path and return false end end def post_params permitted = [:published_at, :title, :content, :spud_user_id, :url_name, :visible, :comments_enabled, :meta_keywords, :meta_description, :content_format, :category_ids => []] if Spud::Blog.permitted_attributes.present? permitted = permitted + Spud::Blog.permitted_attributes end params.require(:spud_post).permit(permitted) end def load_blog @config = SpudBlogConfig.find(params[:blog_key]) if @config.blank? redirect_to admin_root_path return false else self.class.belongs_to_spud_app "#{@config.name} Posts".parameterize.underscore.to_sym add_breadcrumb "#{@config.name} Posts", admin_posts_path(blog_key: @config.key) end end end