<commit_before>require 'inflecto'
require 'end_view/form/form_helpers'

module EndView
  class FormFor
    include FormHelpers

    def initialize(record, form_authenticity_token, opts = {})
      @record = record
      @form_authenticity_token = form_authenticity_token
      @model_name = opts[:model_name]
      @model_id = opts[:model_id]
      @action = opts[:action]
      @form_method = opts[:form_method]
      @form_url = opts[:form_url]
    end

    def form_opts(opts = {})
      super.merge(class: form_class, id: form_id).merge(opts)
    end

    def label_opts(attribute, opts = {})
      { for: attribute_id(attribute) }.merge(opts)
    end

    def input_opts(attribute, opts = {})
      { id: attribute_id(attribute),
        name: attribute_name(attribute),
        type: 'text',
        value: attribute_value(attribute) }.merge(opts)
    end

    attr_accessor :record, :form_authenticity_token
    attr_writer :model_name, :model_id, :action, :form_method, :form_url

    def model_name
      @model_name ||= Inflecto.underscore(Inflecto.demodulize(record.class.to_s))
    end

    def model_id
      @model_id ||= (record.respond_to?(:to_key) && record.to_key.first) || nil
    end

    def action
      @action ||= model_id ? 'edit' : 'new'
    end

    def form_method
      @form_method ||= model_id ? 'patch' : 'post'
    end

    def form_url
      @form_url ||= "/#{Inflecto.pluralize(model_name)}/#{model_id}"
    end

    def form_class
      @form_class ||= "#{action}_#{model_name}"
    end

    def form_id
      @form_id ||= [action, model_name, model_id].compact.join('_')
    end

    def attribute_id(attribute)
      "#{model_name}_#{attribute}"
    end

    def attribute_name(attribute)
      "#{model_name}[#{attribute}]"
    end

    def attribute_value(attribute)
      (record.respond_to?(attribute) && record.send(attribute)) || nil
    end
  end
end
<commit_msg>Handles ActiveRecord responding to_key and returning nil<commit_after>require 'inflecto'
require 'end_view/form/form_helpers'

module EndView
  class FormFor
    include FormHelpers

    def initialize(record, form_authenticity_token, opts = {})
      @record = record
      @form_authenticity_token = form_authenticity_token
      @model_name = opts[:model_name]
      @model_id = opts[:model_id]
      @action = opts[:action]
      @form_method = opts[:form_method]
      @form_url = opts[:form_url]
    end

    def form_opts(opts = {})
      super.merge(class: form_class, id: form_id).merge(opts)
    end

    def label_opts(attribute, opts = {})
      { for: attribute_id(attribute) }.merge(opts)
    end

    def input_opts(attribute, opts = {})
      { id: attribute_id(attribute),
        name: attribute_name(attribute),
        type: 'text',
        value: attribute_value(attribute) }.merge(opts)
    end

    attr_accessor :record, :form_authenticity_token
    attr_writer :model_name, :model_id, :action, :form_method, :form_url

    def model_name
      @model_name ||= Inflecto.underscore(Inflecto.demodulize(record.class.to_s))
    end

    def model_id
      @model_id ||= ((record.respond_to?(:to_key) && record.to_key) || []).first
    end

    def action
      @action ||= model_id ? 'edit' : 'new'
    end

    def form_method
      @form_method ||= model_id ? 'patch' : 'post'
    end

    def form_url
      @form_url ||= "/#{Inflecto.pluralize(model_name)}/#{model_id}"
    end

    def form_class
      @form_class ||= "#{action}_#{model_name}"
    end

    def form_id
      @form_id ||= [action, model_name, model_id].compact.join('_')
    end

    def attribute_id(attribute)
      "#{model_name}_#{attribute}"
    end

    def attribute_name(attribute)
      "#{model_name}[#{attribute}]"
    end

    def attribute_value(attribute)
      (record.respond_to?(attribute) && record.send(attribute)) || nil
    end
  end
end

{'fraction_non_alphanumeric': 0.12538860103626942, 'fraction_numerical': 0.0, 'mean_word_length': 3.1294117647058823, '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': '4527633', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1294, 'n_words': 254}