app/controllers/crowdblog/posts_controller.rb in crowdblog-0.0.16 vs app/controllers/crowdblog/posts_controller.rb in crowdblog-0.1.0
- old
+ new
@@ -1,48 +1,69 @@
-module Crowdblog
- class PostsController < Controller
+module Crowdblog
+ class PostsController < Crowdblog::ApplicationController
+ before_filter :authenticate_user!
+
respond_to :html, :json
cache_sweeper :post_sweeper
+ before_filter :load_post, :only => [ :edit, :update, :destroy ]
+
+ def new
+ @post = Post.new
+ @post.author = current_user
+ @post.save!
+ redirect_to edit_post_path(@post)
+ end
+
def index
- @posts = Post.scoped_for(current_user).all_posts_json
- respond_to do |format|
- format.json { render json: @posts }
- format.html
- end
+ @posts = Post.scoped_for(current_user).for_admin_index
+ respond_with @posts
end
def create
- @post = Post.new(params[:post])
+ @post = Post.new(post_params)
@post.author = current_user
@post.regenerate_permalink
- @post.save
- respond_with @post
+ if @post.save
+ respond_with @post, :location => crowdblog.posts_path
+ end
end
def destroy
- @post = Post.scoped_for(current_user).find(params[:id])
@post.destroy
- respond_with @post
+ respond_with @post, :location => crowdblog.posts_path
end
def show
@post = Post.includes(:assets).find(params[:id])
respond_to do |format|
format.json { render json: @post.to_json(include: :assets) }
end
end
+ def edit
+ end
+
def update
- @post = Post.scoped_for(current_user).find(params[:id])
- @post.update_attributes(params[:post], updated_by: current_user)
+ @post.update_attributes(post_params, updated_by: current_user)
if @post.allowed_to_update_permalink?
@post.regenerate_permalink
@post.save!
end
- @post.publish_if_allowed(params[:transition], current_user) if params[:transition]
+ @post.publish_if_allowed(post_params[:transition], current_user) if post_params[:transition]
- respond_with @post
+ respond_with @post do |format|
+ format.html { redirect_to crowdblog.posts_path }
+ end
+ end
+
+ private
+ def load_post
+ @post = Post.scoped_for(current_user).find(params[:id])
+ end
+
+ def post_params
+ params.require(:post).permit(:title, :body, :updated_by, :ready_for_review, :transition)
end
end
end