summaryrefslogtreecommitdiff
path: root/app/controllers/posts/features_controller.rb
blob: 96512972b0ae77750cc5dbff72c17979ffd57f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

class Posts::FeaturesController < ApplicationController
  before_action :load_post
  before_action :check_auth

  def create
    tag = Tag.find_tag_by_name('featured image')
    @post.features.create!(user: current_user)
    @post.add_tags [tag]
    @post.save!

    TagChange.create!(request_attributes.merge(added: true, post: @post, tag: tag))

    flash[:notice] = t('posts.feature.success')

    redirect_to short_post_path(@post)
  end

  private

  def load_post
    @post = Post.find_by!(id: params[:post_id], hidden_from_users: false)
  end

  def check_auth
    authorize! :feature, @post
  end
end