<commit_before># frozen_string_literal: true

module GobiertoAdmin
  module GobiertoCommon
    module CustomFields
      class CustomFieldsController < GobiertoCommon::CustomFields::BaseController
        before_action :check_permissions!
        before_action :check_class, except: :create_option

        def index
          set_class_name

          @custom_field_form = form_class.new(
            resource_name: params[:module_resource_name],
            instance_class_name: params[:instance_type],
            instance_id: params[:instance_id]
          )
          @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
          @class_human_name = @class_name.constantize.model_name.human(count: 2)

          @localized_custom_fields = current_site.custom_fields.where(class_name: @class_name, instance: @custom_field_form.instance).localized.sorted
          @not_localized_custom_fields = current_site.custom_fields.where(class_name: @class_name, instance: @custom_field_form.instance).not_localized.sorted
        end

        def new
          @custom_field_form = form_class.new(
            site_id: current_site.id,
            resource_name: params[:module_resource_name],
            instance_class_name: params[:instance_type],
            instance_id: params[:instance_id]
          )
          @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
          set_options
        end

        def create
          @custom_field_form = form_class.new(custom_field_params.merge(site_id: current_site.id, resource_name: params[:module_resource_name]))
          if @custom_field_form.save
            touch_instance
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: @custom_field_form.resource_param,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              notice: t(".success")
            )
          else
            set_options
            @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
            render :new
          end
        end

        def create_option
          @new_option_translations = new_option_translations
          generate_key
          render layout: false
        end

        def edit
          find_custom_field

          @custom_field_form = form_class.new(@custom_field.attributes.except(*ignored_custom_field_attributes).merge(site_id: current_site.id))
          @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
          set_options
        end

        def update
          find_custom_field

          @custom_field_form = form_class.new(custom_field_params.merge(id: @custom_field.id, site_id: current_site.id))
          if @custom_field_form.save
            touch_instance
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: @custom_field_form.resource_param,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              notice: t(".success")
            )
          else
            set_options
            @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
            render :edit
          end
        end

        def destroy
          find_custom_field

          @custom_field_form = form_class.new(id: @custom_field.id, site_id: current_site.id)
          module_resource_name = @custom_field_form.resource_param

          if @custom_field.destroy
            touch_instance
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: module_resource_name,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              notice: t(".success")
            )
          else
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: module_resource_name,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              alert: t(".failed")
            )
          end
        end

        private

        def touch_instance
          return unless @custom_field_form&.instance&.present?

          @custom_field_form.instance.touch
        end

        def check_permissions!
          raise_module_not_allowed unless current_admin.can_edit_custom_fields?
        end

        def custom_field_params
          params.require(:custom_field).permit(
            :field_type,
            :uid,
            :vocabulary_id,
            :vocabulary_type,
            :plugin_type,
            :date_type,
            :instance_class_name,
            :instance_id,
            :plugin_configuration,
            :multiple,
            name_translations: [*I18n.available_locales],
            options_translations: {}
          )
        end<commit_msg>Improve method code<commit_after># frozen_string_literal: true

module GobiertoAdmin
  module GobiertoCommon
    module CustomFields
      class CustomFieldsController < GobiertoCommon::CustomFields::BaseController
        before_action :check_permissions!
        before_action :check_class, except: :create_option

        def index
          set_class_name

          @custom_field_form = form_class.new(
            resource_name: params[:module_resource_name],
            instance_class_name: params[:instance_type],
            instance_id: params[:instance_id]
          )
          @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
          @class_human_name = @class_name.constantize.model_name.human(count: 2)

          @localized_custom_fields = current_site.custom_fields.where(class_name: @class_name, instance: @custom_field_form.instance).localized.sorted
          @not_localized_custom_fields = current_site.custom_fields.where(class_name: @class_name, instance: @custom_field_form.instance).not_localized.sorted
        end

        def new
          @custom_field_form = form_class.new(
            site_id: current_site.id,
            resource_name: params[:module_resource_name],
            instance_class_name: params[:instance_type],
            instance_id: params[:instance_id]
          )
          @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
          set_options
        end

        def create
          @custom_field_form = form_class.new(custom_field_params.merge(site_id: current_site.id, resource_name: params[:module_resource_name]))
          if @custom_field_form.save
            touch_instance
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: @custom_field_form.resource_param,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              notice: t(".success")
            )
          else
            set_options
            @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
            render :new
          end
        end

        def create_option
          @new_option_translations = new_option_translations
          generate_key
          render layout: false
        end

        def edit
          find_custom_field

          @custom_field_form = form_class.new(@custom_field.attributes.except(*ignored_custom_field_attributes).merge(site_id: current_site.id))
          @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
          set_options
        end

        def update
          find_custom_field

          @custom_field_form = form_class.new(custom_field_params.merge(id: @custom_field.id, site_id: current_site.id))
          if @custom_field_form.save
            touch_instance
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: @custom_field_form.resource_param,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              notice: t(".success")
            )
          else
            set_options
            @decorated_instance = ::GobiertoAdmin::BaseResourceDecorator.new(@custom_field_form.instance) if @custom_field_form.instance
            render :edit
          end
        end

        def destroy
          find_custom_field

          @custom_field_form = form_class.new(id: @custom_field.id, site_id: current_site.id)
          module_resource_name = @custom_field_form.resource_param

          if @custom_field.destroy
            touch_instance
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: module_resource_name,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              notice: t(".success")
            )
          else
            redirect_to(
              admin_common_custom_fields_module_resource_custom_fields_path(
                module_resource_name: module_resource_name,
                instance_type: @custom_field_form.instance_class_name,
                instance_id: @custom_field_form.instance_id
              ),
              alert: t(".failed")
            )
          end
        end

        private

        def touch_instance
          @custom_field_form&.instance&.touch
        end

        def check_permissions!
          raise_module_not_allowed unless current_admin.can_edit_custom_fields?
        end

        def custom_field_params
          params.require(:custom_field).permit(
            :field_type,
            :uid,
            :vocabulary_id,
            :vocabulary_type,
            :plugin_type,
            :date_type,
            :instance_class_name,
            :instance_id,
            :plugin_configuration,
            :multiple,
            name_translations: [*I18n.available_locales],
            options_translations: {}
          )
        end
{'fraction_non_alphanumeric': 0.06530835000909586, 'fraction_numerical': 0.0005457522284882663, 'mean_word_length': 2.2443198583652997, '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': '2501957', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2928, 'n_words': 426}