<commit_before>module ActiveScaffold
  module Helpers
    module ControllerHelpers
      def self.included(controller)
        if controller.respond_to? :helper_method
          controller.class_eval do
            helper_method :params_for, :conditions_from_params, :render_parent?,
                          :main_path_to_return, :render_parent_options,
                          :render_parent_action, :nested_singular_association?,
                          :main_form_controller, :build_associated,
                          :generate_temporary_id, :generated_id
          end
        end
      end

      include ActiveScaffold::Helpers::IdHelpers

      def generate_temporary_id(record = nil, generated_id = nil)
        (generated_id || (Time.now.to_f * 1000).to_i.to_s).tap do |id|
          (@temporary_ids ||= {})[record.class.name] = id if record
        end
      end

      def generated_id(record)
        @temporary_ids[record.class.name] if record && @temporary_ids
      end

      # These params should not propagate:
      # :adapter and :position are one-use rendering arguments.
      # :sort, :sort_direction, and :page are arguments that stored in the session, if store_user_settings is enabled
      # and wow. no we don't want to propagate :record.
      # :commit is a special rails variable for form buttons
      # :_method is a special rails variable to simulate put, patch and delete actions.
      # :dont_close is submit button which avoids closing form.
      # :auto_pagination is used when all records are loaded automatically with multiple request.
      # :iframe is used to simulate ajax forms loading form in iframe.
      # :associated_id used in add_existing
      # :authenticity_token is sent on some ajax requests
      # :_added is sent on checkbox-list with update_columns
      # :_removed is sent on checkbox-list with update_columns
      # :_popstate sent when loading previous page from history, after using history.pushState
      # :_ jQuery param added for GET requests with cache disabled
      BLACKLIST_PARAMS = %i[adapter position sort sort_direction page record commit _method dont_close auto_pagination
                            iframe associated_id authenticity_token _added _removed _popstate _].freeze

      def params_for(options = {})
        unless @params_for
          @params_for = {}
          params.except(*BLACKLIST_PARAMS).each do |key, value|
            @params_for[key.to_sym] = copy_param(value)
          end
          @params_for[:controller] = '/' + @params_for[:controller].to_s unless @params_for[:controller].to_s.first(1) == '/' # for namespaced controllers
          @params_for.delete(:id) if @params_for[:id].nil?
        end

        url_options = @params_for.merge(options)
        if !active_scaffold_config.store_user_settings && controller_requested(url_options[:controller]) == controller_path
          url_options[:search] ||= copy_param search_params if respond_to?(:search_params) && search_params.present?
          url_options[:page] ||= params[:page]
          if active_scaffold_config.list.user.user_sorting?
            column, direction = active_scaffold_config.list.user.sorting.first
            url_options[:sort] ||= column.name
            url_options[:sort_direction] ||= direction
          end
        end
        url_options
      end

      def controller_requested(controller)
        if controller.to_s.first(1) == '/'
          controller[1..-1]
        else
          path = controller_path.split('/')[0..-2]
          path << controller
          path.join('/')
        end
      end

      def copy_param(value)
        if controller_params? value
          params_hash value
        else
          value.duplicable? ? value.clone : value<commit_msg>fix params_for when list action is not enabled<commit_after>module ActiveScaffold
  module Helpers
    module ControllerHelpers
      def self.included(controller)
        if controller.respond_to? :helper_method
          controller.class_eval do
            helper_method :params_for, :conditions_from_params, :render_parent?,
                          :main_path_to_return, :render_parent_options,
                          :render_parent_action, :nested_singular_association?,
                          :main_form_controller, :build_associated,
                          :generate_temporary_id, :generated_id
          end
        end
      end

      include ActiveScaffold::Helpers::IdHelpers

      def generate_temporary_id(record = nil, generated_id = nil)
        (generated_id || (Time.now.to_f * 1000).to_i.to_s).tap do |id|
          (@temporary_ids ||= {})[record.class.name] = id if record
        end
      end

      def generated_id(record)
        @temporary_ids[record.class.name] if record && @temporary_ids
      end

      # These params should not propagate:
      # :adapter and :position are one-use rendering arguments.
      # :sort, :sort_direction, and :page are arguments that stored in the session, if store_user_settings is enabled
      # and wow. no we don't want to propagate :record.
      # :commit is a special rails variable for form buttons
      # :_method is a special rails variable to simulate put, patch and delete actions.
      # :dont_close is submit button which avoids closing form.
      # :auto_pagination is used when all records are loaded automatically with multiple request.
      # :iframe is used to simulate ajax forms loading form in iframe.
      # :associated_id used in add_existing
      # :authenticity_token is sent on some ajax requests
      # :_added is sent on checkbox-list with update_columns
      # :_removed is sent on checkbox-list with update_columns
      # :_popstate sent when loading previous page from history, after using history.pushState
      # :_ jQuery param added for GET requests with cache disabled
      BLACKLIST_PARAMS = %i[adapter position sort sort_direction page record commit _method dont_close auto_pagination
                            iframe associated_id authenticity_token _added _removed _popstate _].freeze

      def params_for(options = {})
        unless @params_for
          @params_for = {}
          params.except(*BLACKLIST_PARAMS).each do |key, value|
            @params_for[key.to_sym] = copy_param(value)
          end
          @params_for[:controller] = '/' + @params_for[:controller].to_s unless @params_for[:controller].to_s.first(1) == '/' # for namespaced controllers
          @params_for.delete(:id) if @params_for[:id].nil?
        end

        url_options = @params_for.merge(options)
        if !active_scaffold_config.store_user_settings && controller_requested(url_options[:controller]) == controller_path
          url_options[:search] ||= copy_param search_params if respond_to?(:search_params) && search_params.present?
          url_options[:page] ||= params[:page]
          if active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.user.user_sorting?
            column, direction = active_scaffold_config.list.user.sorting.first
            url_options[:sort] ||= column.name
            url_options[:sort_direction] ||= direction
          end
        end
        url_options
      end

      def controller_requested(controller)
        if controller.to_s.first(1) == '/'
          controller[1..-1]
        else
          path = controller_path.split('/')[0..-2]
          path << controller
          path.join('/')
        end
      end

      def copy_param(value)
        if controller_params? value
          params_hash value
        else
          value.duplicable? ? value.clone : value
{'fraction_non_alphanumeric': 0.08068107400130976, 'fraction_numerical': 0.0026195153896529143, 'mean_word_length': 2.5450324976787373, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1454338', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2047, 'n_words': 567}