<commit_before>module Effective
  module Resources
    module Forms

      # Used by datatables
      def search_form_field(name, type = nil)
        case (type || sql_type(name))
        when :belongs_to
          { as: :select }.merge(search_form_field_collection(belongs_to(name)))
        when :belongs_to_polymorphic
          constant_pluralized = name.to_s.upcase
          constant = name.to_s.pluralize.upcase

          collection = (klass.const_get(constant) rescue nil) if defined?("#{klass.name}::#{constant}")
          collection ||= (klass.const_get(constant_pluralized) rescue nil) if defined?("#{klass.name}::#{constant_pluralized}")
          collection ||= {}

          { as: :select, polymorphic: true, collection: collection }
        when :has_and_belongs_to_many
          { as: :select }.merge(search_form_field_collection(has_and_belongs_to_many(name)))
        when :has_many
          { as: :select, multiple: true }.merge(search_form_field_collection(has_many(name)))
        when :has_one
          { as: :select, multiple: true }.merge(search_form_field_collection(has_one(name)))
        when :effective_addresses
          { as: :string }
        when :effective_roles
          { as: :select, collection: EffectiveRoles.roles }<commit_msg>better belongs to polymorphic search options<commit_after>module Effective
  module Resources
    module Forms

      # Used by datatables
      def search_form_field(name, type = nil)
        case (type || sql_type(name))
        when :belongs_to
          { as: :select }.merge(search_form_field_collection(belongs_to(name)))
        when :belongs_to_polymorphic
          constant_pluralized = name.to_s.upcase
          constant = name.to_s.pluralize.upcase

          collection = nil

          if klass.respond_to?(:name)
            collection ||= (klass.const_get(constant) rescue nil) if defined?("#{klass.name}::#{constant}")
            collection ||= (klass.const_get(constant_pluralized) rescue nil) if defined?("#{klass.name}::#{constant_pluralized}")
          end

          { as: :select, polymorphic: true, collection: collection }.compact
        when :has_and_belongs_to_many
          { as: :select }.merge(search_form_field_collection(has_and_belongs_to_many(name)))
        when :has_many
          { as: :select, multiple: true }.merge(search_form_field_collection(has_many(name)))
        when :has_one
          { as: :select, multiple: true }.merge(search_form_field_collection(has_one(name)))
        when :effective_addresses
          { as: :string }
        when :effective_roles
          { as: :select, collection: EffectiveRoles.roles }
{'fraction_non_alphanumeric': 0.10888468809073724, 'fraction_numerical': 0.0, 'mean_word_length': 3.0090909090909093, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3210242', 'n_tokens_mistral': 807, 'n_tokens_neox': 797, 'n_words': 160}