Class: Panda::CMS::Admin::PostsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/panda/cms/admin/posts_controller.rb

Instance Method Summary collapse

Methods included from Panda::CMS::ApplicationHelper

#active_link?, #block_link_to, #component, #level_indent, #menu_indent, #nav_class, #nav_highlight_colour_classes, #panda_cms_editor, #panda_cms_form_with, #selected_nav_highlight_colour_classes, #table_indent, #title_tag

Instance Method Details

#createObject

POST /admin/posts



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/panda/cms/admin/posts_controller.rb', line 34

def create
  @post = Panda::CMS::Post.new(post_params)
  @post.user_id = current_user.id
  @post.content = parse_content(post_params[:content]) # Parse the content before saving

  if @post.save
    Rails.logger.debug "Post saved successfully"
    redirect_to edit_admin_cms_post_path(@post.admin_param), notice: "The post was successfully created!"
  else
    Rails.logger.debug "Post save failed: #{@post.errors.full_messages.inspect}"
    flash.now[:error] = @post.errors.full_messages.join(", ")
    locals = setup_new_post_form(post: @post, preserved_content: post_params[:content])
    render :new, locals: locals, status: :unprocessable_entity
  end
end

#editObject

Loads the post editor



28
29
30
31
# File 'app/controllers/panda/cms/admin/posts_controller.rb', line 28

def edit
  add_breadcrumb post.title, edit_admin_cms_post_path(post.admin_param)
  render :edit, locals: {post: post}
end

#indexObject

Get all posts

Returns:

  • ActiveRecord::Collection A list of all posts



14
15
16
17
# File 'app/controllers/panda/cms/admin/posts_controller.rb', line 14

def index
  posts = Panda::CMS::Post.with_author.ordered
  render :index, locals: {posts: posts}
end

#newObject

Loads the add post form



21
22
23
24
# File 'app/controllers/panda/cms/admin/posts_controller.rb', line 21

def new
  locals = setup_new_post_form
  render :new, locals: locals
end

#updateObject

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/panda/cms/admin/posts_controller.rb', line 52

def update
  Rails.logger.debug "Current content: #{post.content.inspect}"
  Rails.logger.debug "New content from params: #{post_params[:content].inspect}"

  # Parse the content before updating
  update_params = post_params
  update_params[:content] = parse_content(post_params[:content])
  update_params[:user_id] = current_user.id
  if post.update(update_params)
    Rails.logger.debug "Post updated successfully"
    add_breadcrumb post.title, edit_admin_cms_post_path(post.admin_param)
    flash[:success] = "The post was successfully updated"
    redirect_to edit_admin_cms_post_path(post.admin_param), status: :see_other
  else
    Rails.logger.debug "Post update failed: #{post.errors.full_messages.inspect}"
    Rails.logger.debug "Preserving content: #{post_params[:content].inspect}"
    add_breadcrumb post.title.presence || "Edit Post", edit_admin_cms_post_path(post.admin_param)
    flash.now[:error] = post.errors.full_messages.join(", ")
    render :edit, locals: {
      post: post,
      preserved_content: post_params[:content]
    }, status: :unprocessable_entity
  end
end