<commit_before># frozen_string_literal: true
require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdmin
  module Config
    module Actions
      class New < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :collection do
          true
        end

        register_instance_option :http_methods do
          [:get, :post] # NEW / CREATE
        end

        register_instance_option :controller do
          proc do
            if request.get? # NEW

              @object = @abstract_model.new
              @authorization_adapter && @authorization_adapter.attributes_for(:new, @abstract_model).each do |name, value|
                @object.send("#{name}=", value)
              end
              # rubocop:disable Lint/AssignmentInCondition
              if object_params = params[@abstract_model.param_key]
                sanitize_params_for!(request.xhr? ? :modal : :create)
                @object.set_attributes(@object.attributes.merge(object_params.to_h))
              end
              # rubocop:enable Lint/AssignmentInCondition
              respond_to do |format|
                format.html { render @action.template_name }
                format.js   { render @action.template_name, layout: false }
              end

            elsif request.post? # CREATE

              @modified_assoc = []
              @object = @abstract_model.new
              sanitize_params_for!(request.xhr? ? :modal : :create)

              @object.set_attributes(params[@abstract_model.param_key])
              @authorization_adapter && @authorization_adapter.attributes_for(:create, @abstract_model).each do |name, value|
                @object.send("#{name}=", value)
              end

              if @object.save
                @auditing_adapter && @auditing_adapter.create_object(@object, @abstract_model, _current_user)

                # NOTE: Hacky hack hack: retrieve TargetAudienceFiltersOffers if object is a cloned offer
                regex = request.referer.match(%r{/offer/(\d+)/clone})
                if regex && regex[1] && Offer.where(id: regex[1]).any?
                  source_offer = Offer.where(id: regex[1]).first
                  source_offer.target_audience_filters_offers.map do |tafo|
                    attributes = tafo.attributes.except(
                      'id', 'offer_id', 'created_at', 'updated_at'
                    )<commit_msg>Hotfix for clarat-org/clarat#1156 - TargetAudienceFiltersOffer Hack only for offers<commit_after># frozen_string_literal: true
require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdmin
  module Config
    module Actions
      class New < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :collection do
          true
        end

        register_instance_option :http_methods do
          [:get, :post] # NEW / CREATE
        end

        register_instance_option :controller do
          proc do
            if request.get? # NEW

              @object = @abstract_model.new
              @authorization_adapter && @authorization_adapter.attributes_for(:new, @abstract_model).each do |name, value|
                @object.send("#{name}=", value)
              end
              # rubocop:disable Lint/AssignmentInCondition
              if object_params = params[@abstract_model.param_key]
                sanitize_params_for!(request.xhr? ? :modal : :create)
                @object.set_attributes(@object.attributes.merge(object_params.to_h))
              end
              # rubocop:enable Lint/AssignmentInCondition
              respond_to do |format|
                format.html { render @action.template_name }
                format.js   { render @action.template_name, layout: false }
              end

            elsif request.post? # CREATE

              @modified_assoc = []
              @object = @abstract_model.new
              sanitize_params_for!(request.xhr? ? :modal : :create)

              @object.set_attributes(params[@abstract_model.param_key])
              @authorization_adapter && @authorization_adapter.attributes_for(:create, @abstract_model).each do |name, value|
                @object.send("#{name}=", value)
              end

              if @object.save
                @auditing_adapter && @auditing_adapter.create_object(@object, @abstract_model, _current_user)

                # NOTE: Hacky hack hack: retrieve TargetAudienceFiltersOffers if object is a cloned offer
                regex = request.referer.match(%r{/offer/(\d+)/clone})
                if regex && regex[1] && Offer.where(id: regex[1]).any? && @object.class == Offer
                  source_offer = Offer.where(id: regex[1]).first
                  source_offer.target_audience_filters_offers.map do |tafo|
                    attributes = tafo.attributes.except(
                      'id', 'offer_id', 'created_at', 'updated_at'
                    )
{'fraction_non_alphanumeric': 0.0967232530596131, 'fraction_numerical': 0.0019739439399921043, 'mean_word_length': 2.080243161094225, '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': '6604638', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1326, 'n_words': 267}