Class: Railspress::Admin::PostsController

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

Constant Summary collapse

ALLOWED_ATTACHMENTS =
%w[header_image].freeze

Instance Method Summary collapse

Instance Method Details

#createObject



33
34
35
36
37
38
39
40
# File 'app/controllers/railspress/admin/posts_controller.rb', line 33

def create
  @post = Post.new(post_params)
  if @post.save
    redirect_to admin_post_path(@post), notice: "Post created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



53
54
55
56
# File 'app/controllers/railspress/admin/posts_controller.rb', line 53

def destroy
  @post.destroy
  redirect_to admin_posts_path, notice: "Post deleted."
end

#editObject



42
43
# File 'app/controllers/railspress/admin/posts_controller.rb', line 42

def edit
end

#image_editorObject

GET /admin/posts/:id/image_editor/:attachment Returns the expanded image editor in a Turbo Frame Pass ?compact=true to get the compact view (for Cancel)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/railspress/admin/posts_controller.rb', line 63

def image_editor
  unless ALLOWED_ATTACHMENTS.include?(params[:attachment])
    raise ActionController::RoutingError, "Invalid attachment"
  end
  @attachment_name = params[:attachment].to_sym

  if params[:compact] == "true"
    render partial: "railspress/admin/shared/image_section_compact",
           locals: {
             record: @post,
             attachment_name: @attachment_name,
             label: "Main Image"
           }
  else
    # Ensure focal point is persisted before editing
    focal_point = @post.send("#{@attachment_name}_focal_point")
    focal_point.save! if focal_point.new_record?

    render partial: "railspress/admin/shared/image_section_editor",
           locals: {
             record: @post,
             attachment_name: @attachment_name,
             contexts: Railspress.image_contexts
           }
  end
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/railspress/admin/posts_controller.rb', line 7

def index
  @sort = params[:sort].presence || "created_at"
  @direction = params[:direction].presence || "desc"

  @posts = Post.includes(:category, :tags)
               .search(params[:q])
               .by_category(params[:category_id])
               .by_status(params[:status])
               .sorted_by(@sort, @direction)

  @total_count = @posts.count
  @page = [ params[:page].to_i, 1 ].max
  @total_pages = (@total_count.to_f / Post::PER_PAGE).ceil
  @posts = @posts.page(@page)

  @categories = Category.ordered
end

#newObject



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

def new
  @post = Post.new
  @post.author = current_author if authors_enabled?
end

#showObject



25
26
# File 'app/controllers/railspress/admin/posts_controller.rb', line 25

def show
end

#updateObject



45
46
47
48
49
50
51
# File 'app/controllers/railspress/admin/posts_controller.rb', line 45

def update
  if @post.update(post_params)
    redirect_to admin_post_path(@post), notice: "Post updated."
  else
    render :edit, status: :unprocessable_entity
  end
end