Sha256: cf3ee473592e6dfb01ca1854ebb26f7aa781091601a6865a1ae1354054d58ada

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

class Admin::PostsController < ApplicationController
  include ActionControllerMixin
  before_filter :authenticate_user! if Object.const_defined? 'Devise'
  before_filter :scope_blog
  before_filter :scope_post, :only => [:destroy, :edit, :show, :update]

  # CRUD ===========================================================================================
  def index
    params[:labels] = {
      :slug => 'URL',
      :state         => 'Status',
      :updated_at    => 'Last Modified'
    }
    params[:by] ||= 'publication_date'
    params[:dir] ||= 'desc'
    @posts = @blog.posts.sort_by{ |p| p[params[:by]] || Time.zone.now }
    @posts.reverse! if params[:dir] == 'desc'
  end

  def show
  end

  def new
    @post = @blog.posts.build
  end

  def create
    handle_date_time params, :post, :publication_date
    @post = @blog.posts.build params[:post]

    if @post.save
      @post.publish! if params[:commit] == 'Publish' || params[:post][:state] == 'published'
      flash[:notice] = 'Successfully created post.'
      redirect_to @post.path
    else
      render :action => 'new'
    end
  end

  def edit
  end

  def update
    handle_date_time params, :post, :publication_date

    if params[:commit] == 'Preview'
      @post = @blog.posts.new params[:post]
      render :action => 'preview', :layout => 'application'
    else
      if @post.update_attributes params[:post]
        @post.publish! if params[:commit] == "Publish"
        @post.unpublish! if params[:commit] == "Save as Draft"
        flash[:notice] = 'Successfully updated post.'
        redirect_to @post.path
      else
        render :action => 'edit'
      end
    end
  end

  def destroy
    @post.destroy
    flash[:notice] = 'Successfully destroyed post.'
    redirect_to admin_blog_posts_url(@blog)
  end

  private

  def scope_blog
    @blog = Blog.first
  end

  def scope_post
    @post = @blog.posts.find params[:id]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blog_logic-1.4.13 app/controllers/admin/posts_controller.rb
blog_logic-1.4.12 app/controllers/admin/posts_controller.rb