<commit_before>module Aerogel::Forms

# FormObject represents a model object associated with current form context.
# It provides access to model object fields and helper methods to form proper html.
#
# Example:
#   form for: objectA  <- FormObject
#     nested form for: objectB  <- FormObject
#     list of nested form for collection: objectC1, objectC2..., objecCN
#
class FormObject < Aerogel::Render::BlockHelper

  attr_accessor :object, :parent, :relation, :options, :style, :field_prefix

  RESERVED_FIELDS = ['id', '_id']

  DEFAULT_OPTIONS = {
    :style => :standard
  }

  def initialize( object, parent = nil, relation = nil, options = {}, &block )
    super( &block )
    self.object = object
    self.parent = parent
    self.relation = relation
    @options = DEFAULT_OPTIONS.dup.deep_merge( options )
    @style = @options[:style].to_sym
    @first_input = nil
    @field_prefix = options[:field_prefix] || make_field_prefix
  end

  def field( name, options = {} ) # -> Field
    f = data_field name, options
    unless @first_input || options.key?( :readonly )
      @first_input = f
      f.options[:autofocus] = nil
    end
    erb template( :field, f.options ), locals: { field: f, form_builder: self }, layout: false
  end

  # Renders fields listed in the arguments.
  #
  def fields( *args )
    args = object.attribute_names - RESERVED_FIELDS if args.size == 0
    output = ''
    args.each do |f|
      output += field f
    end
    output<commit_msg>Default style for nested form objects is inherited from parent<commit_after>module Aerogel::Forms

# FormObject represents a model object associated with current form context.
# It provides access to model object fields and helper methods to form proper html.
#
# Example:
#   form for: objectA  <- FormObject
#     nested form for: objectB  <- FormObject
#     list of nested form for collection: objectC1, objectC2..., objecCN
#
class FormObject < Aerogel::Render::BlockHelper

  attr_accessor :object, :parent, :relation, :options, :style, :field_prefix

  RESERVED_FIELDS = ['id', '_id']

  DEFAULT_OPTIONS = {
    :style => :standard
  }

  def initialize( object, parent = nil, relation = nil, options = {}, &block )
    super( &block )
    self.object = object
    self.parent = parent
    self.relation = relation
    @options = DEFAULT_OPTIONS.dup.deep_merge( options )
    @style = ( !parent.nil? && parent.style ) || @options[:style].to_sym
    @first_input = nil
    @field_prefix = options[:field_prefix] || make_field_prefix
  end

  def field( name, options = {} ) # -> Field
    f = data_field name, options
    unless @first_input || options.key?( :readonly )
      @first_input = f
      f.options[:autofocus] = nil
    end
    erb template( :field, f.options ), locals: { field: f, form_builder: self }, layout: false
  end

  # Renders fields listed in the arguments.
  #
  def fields( *args )
    args = object.attribute_names - RESERVED_FIELDS if args.size == 0
    output = ''
    args.each do |f|
      output += field f
    end
    output
{'fraction_non_alphanumeric': 0.1098322920092075, 'fraction_numerical': 0.0019730351857941467, 'mean_word_length': 3.2605042016806722, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '5915301', 'n_tokens_mistral': 940, 'n_tokens_neox': 913, 'n_words': 296}