Sha256: 7187a737ca192d267d2cf1ab95fb310df1e4bdce51483263056d43357641e24a

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

class Admin::PostTypesController < Admin::BaseController
  before_action :set_post_type, only: [:edit, :update, :destroy]

  def index
    @post_types = PostType.all
    @post_type = PostType.new
  end

  def edit
    @post_types = PostType.all
  end

  def create
    @post_type = PostType.new(post_type_params)

    if @post_type.save
      flash[:notice] = I18n.t("admin.base.successfully_created",
                              name: PostType.model_name.human)
      redirect_to admin_post_types_url
    else
      render :index
    end
  end

  def update
    if @post_type.update(post_type_params)
      flash[:notice] = I18n.t("admin.base.successfully_updated",
                              name: PostType.model_name.human)
      redirect_to admin_post_types_url
    else
      render :edit
    end
  end

  def destroy
    # Reset all Articles from the PostType we're destroying to the default PostType
    # Wrap it in a transaction for safety
    @post_type.transaction do
      Article.where(post_type: @post_type.permalink).find_each do |article|
        article.post_type = "read"
        article.save
      end
      @post_type.destroy
    end
    flash[:notice] = I18n.t("admin.base.successfully_deleted",
                            name: PostType.model_name.human)
    redirect_to admin_post_types_url
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_post_type
    @post_type = PostType.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def post_type_params
    params.require(:post_type).permit(:name, :description)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
publify_core-10.0.2 app/controllers/admin/post_types_controller.rb
publify_core-10.0.1 app/controllers/admin/post_types_controller.rb