<commit_before>module Cms9
  class PostDefinitionsController < Cms9::ApplicationController
    def index
      @posts = PostDefinition.order('created_at desc').page(params[:page]).per(20)
    end

    def new
      @post = PostDefinition.new
    end

    def edit
      @post = PostDefinition.find(params[:id])
    end

    def create
      @post = PostDefinition.new(post_definition_params)

      if @post.save
        @field = PostField.new({
          'name': 'Title',
          'field_type': 'text',
          'required': true,
          'post_definition_id': @post.id
        })
        if @field.save
          redirect_to edit_post_definition_path(@post.id)
        end
      else
        render 'new'
      end
    end

    def update
      @post = PostDefinition.find(params[:id])

      if @post.update(post_definition_params)
        redirect_to post_definitions_path
      else
        render 'edit'
      end
    end
<commit_msg>- update validation for post types with same name<commit_after>module Cms9
  class PostDefinitionsController < Cms9::ApplicationController
    def index
      @posts = PostDefinition.order('created_at desc').page(params[:page]).per(20)
    end

    def new
      @post = PostDefinition.new
    end

    def edit
      @post = PostDefinition.find(params[:id])
    end

    def create
      @post = PostDefinition.new(post_definition_params)

      if PostDefinition.where(name: @post[:name]).blank?

        if @post.save
          @field = PostField.new({
            'name': 'Title',
            'field_type': 'text',
            'required': true,
            'post_definition_id': @post.id
          })
          if @field.save
            redirect_to edit_post_definition_path(@post.id)
          end
        else
          render 'new'
        end

      else
        @post.errors.add(:name, " Post Type already exist")
        render :new
      end

    end

    def update
      @post = PostDefinition.find(params[:id])

      if PostDefinition.where(name: @post[:name]).blank?

        if @post.update(post_definition_params)
          redirect_to post_definitions_path
        else
          render 'edit'
        end

      else
        @post.errors.add(:name, " Post Type already exist")
        render :edit
      end
    end

{'fraction_non_alphanumeric': 0.0969298245614035, 'fraction_numerical': 0.0035087719298245615, 'mean_word_length': 2.0211920529801324, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '670667', 'n_tokens_mistral': 666, 'n_tokens_neox': 643, 'n_words': 152}