Class: Pressroom::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pressroom/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/pressroom/posts_controller.rb', line 24

def create
  @post = Post.new(post_params)

  if @post.save
    redirect_to edit_post_path(@post), notice: t("pressroom.posts.created")
  else
    # Numeric rather than :unprocessable_entity: Rack deprecated that
    # name, and its replacement is not available on every Rack version
    # this gem supports.
    render :new, status: 422
  end
end

#destroyObject



45
46
47
48
# File 'app/controllers/pressroom/posts_controller.rb', line 45

def destroy
  @post.destroy!
  redirect_to posts_path, notice: t("pressroom.posts.destroyed")
end

#editObject



22
# File 'app/controllers/pressroom/posts_controller.rb', line 22

def edit; end

#indexObject



7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/pressroom/posts_controller.rb', line 7

def index
  @page = [params[:page].to_i, 1].max
  @per_page = Pressroom.config.admin_per_page

  scope = Post.includes(:category).recent
  scope = scope.where(status: params[:status]) if Post.statuses.key?(params[:status])

  @total = scope.count
  @posts = scope.offset((@page - 1) * @per_page).limit(@per_page)
end

#newObject



18
19
20
# File 'app/controllers/pressroom/posts_controller.rb', line 18

def new
  @post = Post.new
end

#publishObject

Manual publication, and scheduling, are the same action: the only difference is whether the timestamp is in the future. Nothing polls and nothing is enqueued -- Post.published compares against the clock.



53
54
55
56
57
58
# File 'app/controllers/pressroom/posts_controller.rb', line 53

def publish
  @post.update!(status: :published, published_at: requested_publish_time)

  notice = @post.published? ? t("pressroom.posts.published") : t("pressroom.posts.scheduled")
  redirect_to edit_post_path(@post), notice: notice
end

#unpublishObject



60
61
62
63
# File 'app/controllers/pressroom/posts_controller.rb', line 60

def unpublish
  @post.update!(status: :draft, published_at: nil)
  redirect_to edit_post_path(@post), notice: t("pressroom.posts.unpublished")
end

#updateObject



37
38
39
40
41
42
43
# File 'app/controllers/pressroom/posts_controller.rb', line 37

def update
  if @post.update(post_params)
    redirect_to edit_post_path(@post), notice: t("pressroom.posts.updated")
  else
    render :edit, status: 422
  end
end