app/controllers/storytime/dashboard/posts_controller.rb in storytime-0.0.1 vs app/controllers/storytime/dashboard/posts_controller.rb in storytime-0.0.2
- old
+ new
@@ -13,20 +13,21 @@
def index
authorize @posts
end
def new
- @post = current_user.posts.new
+ @post = current_user.storytime_posts.new
+ @post.post_type = current_post_type
authorize @post
end
def edit
authorize @post
end
def create
- @post = current_user.posts.new(post_params)
+ @post = current_user.storytime_posts.new(post_params)
@post.draft_user_id = current_user.id
authorize @post
if @post.save
redirect_to edit_dashboard_post_path(@post), notice: I18n.t('flash.posts.create.success')
@@ -52,20 +53,26 @@
@post.destroy
flash[:notice] = I18n.t('flash.posts.destroy.success') unless request.xhr?
respond_with [:dashboard, @post]
end
- private
- def set_post
- @post = Post.find(params[:id])
- end
+ private
- def load_posts
- @posts = policy_scope(Storytime::Post).page(params[:page]).per(10)
- end
+ def current_post_type
+ @post_type ||= PostType.find_by(name: params[:post_type] || (@post && @post.post_type.name) || PostType::DEFAULT_TYPE_NAME)
+ end
+ helper_method :current_post_type
- def post_params
- params.require(:post).permit(*policy(@post || current_user.posts.new).permitted_attributes)
- end
+ def set_post
+ @post = Post.friendly.find(params[:id])
+ end
+
+ def load_posts
+ @posts = policy_scope(Storytime::Post).where(post_type_id: current_post_type.id).order(created_at: :desc).page(params[:page]).per(10)
+ end
+
+ def post_params
+ params.require(:post).permit(*policy(@post || current_user.storytime_posts.new).permitted_attributes)
+ end
end
end
end