File size: 2,817 Bytes
5010ad7 5d27770 5010ad7 5d27770 5010ad7 5d27770 5010ad7 5d27770 5010ad7 5d27770 5010ad7 | 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | module Admin
class PostsController < AdminController
before_action :set_post, only: [ :show, :edit, :update, :destroy, :publish, :schedule, :restore ]
def index
@pagy, @posts = pagy(policy_scope(Post).kept.includes(:user, :category, :post_translations))
end
def trash
@pagy, @posts = pagy(policy_scope(Post).discarded.includes(:user, :category, :post_translations))
end
def show
authorize @post
end
def new
@post = Post.new
authorize @post
end
def create
@post = current_user.posts.build(post_params.except(:title_translations))
authorize @post
set_title_translations
set_content_translations
if @post.save
redirect_to admin_posts_path, notice: t("admin.posts.created")
else
render :new, status: :unprocessable_entity
end
end
def edit
authorize @post
end
def update
authorize @post
@post.assign_attributes(post_params.except(:title_translations))
set_title_translations
set_content_translations
if @post.save
redirect_to admin_posts_path, notice: t("admin.posts.updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
authorize @post
@post.discard!
redirect_to admin_posts_path, notice: t("admin.posts.destroyed")
end
def restore
authorize @post, :update?
@post.undiscard!
redirect_to admin_posts_path, notice: t("admin.posts.restored", default: "Post restored successfully")
end
def publish
authorize @post, :publish?
@post.update!(status: "published", published_at: Time.current)
redirect_to admin_posts_path, notice: t("admin.posts.published")
end
def schedule
authorize @post, :schedule?
@post.update!(status: "scheduled", scheduled_at: params[:scheduled_at])
redirect_to admin_posts_path, notice: t("admin.posts.scheduled")
end
private
def set_post
@post = Post.friendly.find(params[:id])
end
def post_params
params.require(:post).permit(:category_id, :cloudinary_public_id, :status,
:published_at, :scheduled_at, tag_ids: [])
end
def set_title_translations
return unless params[:post][:title_translations].present?
params[:post][:title_translations].each do |locale, title|
next unless I18n.available_locales.include?(locale.to_sym)
Mobility.with_locale(locale) { @post.title = title }
end
end
def set_content_translations
I18n.available_locales.each do |locale|
content_key = "content_#{locale}"
next unless params[:post][content_key].present?
@post.send(:"#{content_key}=", params[:post][content_key])
end
end
end
end
|