<commit_before>require 'rails/generators/generated_attribute'

module ExpressAdmin
  class SmartForm < ExpressTemplates::Components::Configurable
    include ExpressTemplates::Components::Capabilities::Resourceful

    emits -> {
      express_form(form_args) {
        hidden(:id)
        filter_by_name(editable_attributes).each do |attrib|
          form_field_for(attrib)
        end
        filter_by_name(has_many_through_associations).each do |assoc|
          select_collection(assoc.name, nil, nil, class: 'select2')
        end
        filter_by_name(timestamp_attributes).each do |timestamp|
          div {
            label {
              "#{timestamp.name.titleize}: #{resource.try(timestamp.name.to_sym)}"
            }
          }
        end
        submit(class: 'button')
      }
    }

    TIMESTAMPS = ['updated_at', 'created_at']

    def form_field_for(attrib)
      field_type_substitutions = {'text_area'       => 'textarea',
                                  'datetime_select' => 'datetime',
                                  'check_box'       => 'checkbox'}
      field_type = attrib.field_type.to_s.sub(/_field$/,'')
      if relation = attrib.name.match(/(\w+)_id$/).try(:[], 1)
          select(attrib.name, config["#{relation}_collection".to_sym], select2: true)<commit_msg>Fix smart_form select options to use named option<commit_after>require 'rails/generators/generated_attribute'

module ExpressAdmin
  class SmartForm < ExpressTemplates::Components::Configurable
    include ExpressTemplates::Components::Capabilities::Resourceful

    emits -> {
      express_form(form_args) {
        hidden(:id)
        filter_by_name(editable_attributes).each do |attrib|
          form_field_for(attrib)
        end
        filter_by_name(has_many_through_associations).each do |assoc|
          select_collection(assoc.name, nil, nil, class: 'select2')
        end
        filter_by_name(timestamp_attributes).each do |timestamp|
          div {
            label {
              "#{timestamp.name.titleize}: #{resource.try(timestamp.name.to_sym)}"
            }
          }
        end
        submit(class: 'button')
      }
    }

    TIMESTAMPS = ['updated_at', 'created_at']

    def form_field_for(attrib)
      field_type_substitutions = {'text_area'       => 'textarea',
                                  'datetime_select' => 'datetime',
                                  'check_box'       => 'checkbox'}
      field_type = attrib.field_type.to_s.sub(/_field$/,'')
      if relation = attrib.name.match(/(\w+)_id$/).try(:[], 1)
          # TODO: should allow select2 override
          select(attrib.name, options: config["#{relation}_collection".to_sym], select2: true)
{'fraction_non_alphanumeric': 0.11792975970425139, 'fraction_numerical': 0.002587800369685767, 'mean_word_length': 2.3867334167709635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '965170', 'n_tokens_mistral': 796, 'n_tokens_neox': 780, 'n_words': 128}