<commit_before>require 'action_view'

module Ransack
  module Helpers
    class FormBuilder < ::ActionView::Helpers::FormBuilder
      def label(method, *args, &block)
        options = args.extract_options!
        text = args.first
        i18n = options[:i18n] || {}
        text ||= object.translate(method, i18n.reverse_merge(:include_associations => true)) if object.respond_to? :translate
        super(method, text, options, &block)
      end

      def submit(value=nil, options={})
        value, options = nil, value if value.is_a?(Hash)
        value ||= Translate.word(:search).titleize
        super(value, options)
      end

      def attribute_select(options = {}, html_options = {})
        raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
        options[:include_blank] = true unless options.has_key?(:include_blank)
        bases = [''] + association_array(options[:associations])
        if bases.size > 1
          @template.grouped_collection_select(
            @object_name, :name, searchable_attribute_collection_for_bases(bases), :last, :first, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        else
          collection = searchable_attribute_collection_for_base(bases.first)
          @template.collection_select(
            @object_name, :name, collection, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        end
      end

      def sort_select(options = {}, html_options = {})
        raise ArgumentError, "sort_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
        options[:include_blank] = true unless options.has_key?(:include_blank)
        bases = [''] + association_array(options[:associations])
        if bases.size > 1
          @template.grouped_collection_select(
            @object_name, :name, sortable_attribute_collection_for_bases(bases), :last, :first, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          ) + @template.collection_select(
            @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        else
          collection = sortable_attribute_collection_for_base(bases.first)
          @template.collection_select(
            @object_name, :name, collection, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          ) + @template.collection_select(
            @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        end
      end

      def sort_fields(*args, &block)
        search_fields(:s, args, block)<commit_msg>Remove direction select from sort_select form builder and put it into its own sort_direction helper method.<commit_after>require 'action_view'

module Ransack
  module Helpers
    class FormBuilder < ::ActionView::Helpers::FormBuilder
      def label(method, *args, &block)
        options = args.extract_options!
        text = args.first
        i18n = options[:i18n] || {}
        text ||= object.translate(method, i18n.reverse_merge(:include_associations => true)) if object.respond_to? :translate
        super(method, text, options, &block)
      end

      def submit(value=nil, options={})
        value, options = nil, value if value.is_a?(Hash)
        value ||= Translate.word(:search).titleize
        super(value, options)
      end

      def attribute_select(options = {}, html_options = {})
        raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
        options[:include_blank] = true unless options.has_key?(:include_blank)
        bases = [''] + association_array(options[:associations])
        if bases.size > 1
          @template.grouped_collection_select(
            @object_name, :name, searchable_attribute_collection_for_bases(bases), :last, :first, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        else
          collection = searchable_attribute_collection_for_base(bases.first)
          @template.collection_select(
            @object_name, :name, collection, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        end
      end

      def sort_select(options = {}, html_options = {})
        raise ArgumentError, "sort_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
        options[:include_blank] = true unless options.has_key?(:include_blank)
        bases = [''] + association_array(options[:associations])
        if bases.size > 1
          @template.grouped_collection_select(
            @object_name, :name, sortable_attribute_collection_for_bases(bases), :last, :first, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        else
          collection = sortable_attribute_collection_for_base(bases.first)
          @template.collection_select(
            @object_name, :name, collection, :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        end
      end

      def sort_direction(options = {}, html_options = {})
        raise ArgumentError, "sort_direction must be called inside a search FormBuilder!" unless object.respond_to?(:context)
        bases = [''] + association_array(options[:associations])
        if bases.size > 1
          @template.collection_select(
            @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        else
          collection = sortable_attribute_collection_for_base(bases.first)
          @template.collection_select(
            @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last,
            objectify_options(options), @default_options.merge(html_options)
          )
        end
      end

      def sort_fields(*args, &block)
        search_fields(:s, args, block)
{'fraction_non_alphanumeric': 0.11455773955773955, 'fraction_numerical': 0.0026105651105651105, 'mean_word_length': 3.0680824484697062, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '6941770', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1793, 'n_words': 373}