<commit_before>module Extensions::ActionView::Helpers::FormHelper
  def self.included(module_)
    module_.alias_method_chain :form_for, :resource
  end

  def form_for_with_resource(record, options, &proc)
    Extensions::ActionView::Helpers::FormHelper.form_for_with_resource_option(self, record, options)

    form_for_without_resource(record, options, &proc)
  end

  # Handles the :resource option in form_for.
  def self.form_for_with_resource_option(form_helper, record, options)
    case options[:resource]
    when Symbol
      fail ArgumentError, ':resource and :url cannot be specified simultaneously' if options[:url]
      helper = url_helper_for_resource(record, options.delete(:resource))
      options[:url] = form_helper.send(helper, *record)
    when nil
    else
      fail ArgumentError, 'Resource must be a symbol with the stem of route helper'
    end
  end

  # Gets the URL for the record. This follows the pluralisation rules for Rails routes, where the
  # CREATE action is plural, and the PUT action is singular.
  #
  # @param record [Array<ActiveRecord::Base>|ActionRecord::Base] The record to build the route from.
  # @param helper [Symbol] The symbol with the stem of the URL helper.
  # @return [Symbol] The appropriate URL helper to call.
  def self.url_helper_for_resource(record, helper)
    resource = record
    resource = resource.last if resource.is_a?(Array)

    inflect_path(helper, resource.new_record?)
  end

  # Inflects the path according to the plurality specified.
  #
  # @param stem [Symbol] The path to inflect.
  # @param plural [Boolean] Whether to make the path plural.
  # @return [String] The stem, in the given plurality.
  def self.inflect_path(stem, plural)
    components = stem.to_s.underscore.split('_')
    suffix =
      if ['path', 'url'].include?(components.last)
        components.pop
      else
        'path'
      end
    name = components.pop
    name =
      if plural
        name.pluralize
      else
        name.singularize
      end
    components.push(name, suffix)
    components.join('_').to_sym
  end
end<commit_msg>Break the inflect_path helper into smaller functions.<commit_after>module Extensions::ActionView::Helpers::FormHelper
  def self.included(module_)
    module_.alias_method_chain :form_for, :resource
  end

  def form_for_with_resource(record, options, &proc)
    Extensions::ActionView::Helpers::FormHelper.form_for_with_resource_option(self, record, options)

    form_for_without_resource(record, options, &proc)
  end

  # Handles the :resource option in form_for.
  def self.form_for_with_resource_option(form_helper, record, options)
    case options[:resource]
    when Symbol
      fail ArgumentError, ':resource and :url cannot be specified simultaneously' if options[:url]
      helper = url_helper_for_resource(record, options.delete(:resource))
      options[:url] = form_helper.send(helper, *record)
    when nil
    else
      fail ArgumentError, 'Resource must be a symbol with the stem of route helper'
    end
  end

  # Gets the URL for the record. This follows the pluralisation rules for Rails routes, where the
  # CREATE action is plural, and the PUT action is singular.
  #
  # @param record [Array<ActiveRecord::Base>|ActionRecord::Base] The record to build the route from.
  # @param helper [Symbol] The symbol with the stem of the URL helper.
  # @return [Symbol] The appropriate URL helper to call.
  def self.url_helper_for_resource(record, helper)
    resource = record
    resource = resource.last if resource.is_a?(Array)

    inflect_path(helper, resource.new_record?)
  end

  # Inflects the path according to the plurality specified.
  #
  # @param stem [Symbol] The path to inflect.
  # @param plural [Boolean] Whether to make the path plural.
  # @return [String] The stem, in the given plurality.
  def self.inflect_path(stem, plural)
    components = stem.to_s.underscore.split('_')
    components, suffix = parse_path_components(components)

    name = components.pop
    name = plural ? name.pluralize : name.singularize

    components.push(name, suffix)
    components.join('_').to_sym
  end

  # Splits the path helper into the suffix (_path, or _url), and the resource involved.
  #
  # @param [String] The components of the path helper.
  # @return Array<[String], String)> The list of components, with the suffix removed, followed by
  #                                  the suffix.
  def self.parse_path_components(components)
    if ['path', 'url'].include?(components.last)
      [components, components.pop]
    else
      [components, 'path']
    end
  end
end
{'fraction_non_alphanumeric': 0.0878524945770065, 'fraction_numerical': 0.0, 'mean_word_length': 3.7051020408163264, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948191', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1316, 'n_words': 438}