<commit_before># Form helpers provide a number of methods to simplify the creation of HTML forms.
# They can work directly with models (bound) or standalone (unbound).
module Merb::Helpers::Form

  def _singleton_form_context
    self._default_builder = Merb::Helpers::Form::Builder::ResourcefulFormWithErrors unless self._default_builder
    @_singleton_form_context ||=
      self._default_builder.new(nil, nil, self)
  end

  def form_contexts
    @_form_contexts ||= []
  end

  def current_form_context
    form_contexts.last || _singleton_form_context
  end

  def _new_form_context(name, builder)
    if name.is_a?(String) || name.is_a?(Symbol)
      ivar = instance_variable_get("@#{name}")
    else
      ivar, name = name, name.class.to_s.snake_case
    end
    builder ||= current_form_context.class if current_form_context
    (builder || self._default_builder).new(ivar, name, self)
  end

  def with_form_context(name, builder)
    form_contexts.push(_new_form_context(name, builder))
    ret = yield
    form_contexts.pop
    ret
  end

  # Generates a form tag, which accepts a block that is not directly based on resource attributes
  #
  # ==== Parameters
  # attrs<Hash>:: HTML attributes
  #
  # ==== Returns
  # String:: HTML
  #
  # ==== Notes
  #  * Block helpers use the <%= =%> syntax
  #  * a multipart enctype is automatically set if the form contains a file upload field
  #
  # ==== Example
  #   <%= form :action => url(:controller => "foo", :action => "bar", :id => 1) do %>
  #     <%= text_field :name => "first_name", :label => "<NAME>" %>
  #     <%= submit "Create" %>
  #   <% end =%>
  #
  #   Generates the HTML:
  #
  #   <form action="/foo/bar/1" method="post">
  #     <label for="first_name">First Name</label>
  #     <input type="text" id="first_name" name="first_name" />
  #     <input type="submit" value="Create" />
  #   </form>
  def form(*args, &blk)
    _singleton_form_context.form(*args, &blk)
  end

  # Generates a resource specific form tag which accepts a block, this also provides automatic resource routing.
  #
  # ==== Parameters
  # name<Symbol>:: Model or Resource
  # attrs<Hash>:: HTML attributes
  #
  # ==== Returns
  # String:: HTML
  #
  # ==== Notes
  #  * Block helpers use the <%= =%> syntax
  #
  # ==== Example
  #   <%= form_for @person do %>
  #     <%= text_field :first_name, :label => "First Name" %>
  #     <%= text_field :last_name,  :label => "<NAME>" %>
  #     <%= submit "Create" %>
  #   <% end =%>
  #
  # The HTML generated for this would be:
  #
  #   <form action="/people" method="post">
  #     <label for="person_first_name">First Name</label>
  #     <input type="text" id="person_first_name" name="person[first_name]" />
  #     <label for="person_last_name">Last Name</label>
  #     <input type="text" id="person_last_name" name="person[last_name]" />
  #     <input type="submit" value="Create" />
  #   </form>
  def form_for(name, attrs = {}, &blk)
    with_form_context(name, attrs.delete(:builder)) do
      current_form_context.form(attrs, &blk)
    end
  end
  
  # Creates a scope around a specific resource object like form_for, but doesnt create the form tags themselves.
  # This makes fields_for suitable for specifying additional resource objects in the same form. 
  #
  # ==== Examples
  #   <%= form_for @person do %>
  #     <%= text_field :first_name, :label => "First Name" %>
  #     <%= text_field :last_name,  :label => "<NAME>" %>
  #     <%= fields_for :permission do %>
  #       <%= check_box :is_admin, :label => "Administrator" %>
  #     <% end =%>
  #     <%= submit "Create" %>
  #   <% end =%>
  def fields_for(name, attrs = {}, &blk)
    attrs ||= {}
    with_form_context(name, attrs.delete(:builder)) do
      yield
    end
  end

  # Provides the ability to create quick fieldsets as blocks for your forms.
  #
  # ==== Parameters
  # attrs<Hash>:: HTML attributes and options
  #<commit_msg>Doc patch<commit_after># Form helpers provide a number of methods to simplify the creation of HTML forms.
# They can work directly with models (bound) or standalone (unbound).
module Merb::Helpers::Form

  def _singleton_form_context
    self._default_builder = Merb::Helpers::Form::Builder::ResourcefulFormWithErrors unless self._default_builder
    @_singleton_form_context ||=
      self._default_builder.new(nil, nil, self)
  end

  def form_contexts
    @_form_contexts ||= []
  end

  def current_form_context
    form_contexts.last || _singleton_form_context
  end

  def _new_form_context(name, builder)
    if name.is_a?(String) || name.is_a?(Symbol)
      ivar = instance_variable_get("@#{name}")
    else
      ivar, name = name, name.class.to_s.snake_case
    end
    builder ||= current_form_context.class if current_form_context
    (builder || self._default_builder).new(ivar, name, self)
  end

  def with_form_context(name, builder)
    form_contexts.push(_new_form_context(name, builder))
    ret = yield
    form_contexts.pop
    ret
  end

  # Generates a form tag, which accepts a block that is not directly based on resource attributes
  #
  # ==== Parameters
  # attrs<Hash>:: HTML attributes
  #
  # ==== Returns
  # String:: HTML
  #
  # ==== Notes
  #  * Block helpers use the <%= =%> syntax
  #  * a multipart enctype is automatically set if the form contains a file upload field
  #
  # ==== Example
  #   <%= form :action => url(:controller => "foo", :action => "bar", :id => 1) do %>
  #     <%= text_field :name => "first_name", :label => "<NAME>" %>
  #     <%= submit "Create" %>
  #   <% end =%>
  #
  #   Generates the HTML:
  #
  #   <form action="/foo/bar/1" method="post">
  #     <label for="first_name">First Name</label>
  #     <input type="text" id="first_name" name="first_name" />
  #     <input type="submit" value="Create" />
  #   </form>
  def form(*args, &blk)
    _singleton_form_context.form(*args, &blk)
  end

  # Generates a resource specific form tag which accepts a block, this also provides automatic resource routing.
  #
  # ==== Parameters
  # name<Symbol>:: Model or Resource
  # attrs<Hash>:: HTML attributes
  #
  # ==== Returns
  # String:: HTML
  #
  # ==== Notes
  #  * Block helpers use the <%= =%> syntax
  #
  # ==== Example
  #   <%= form_for @person do %>
  #     <%= text_field :first_name, :label => "First Name" %>
  #     <%= text_field :last_name,  :label => "<NAME>" %>
  #     <%= submit "Create" %>
  #   <% end =%>
  #
  # The HTML generated for this would be:
  #
  #   <form action="/people" method="post">
  #     <label for="person_first_name">First Name</label>
  #     <input type="text" id="person_first_name" name="person[first_name]" />
  #     <label for="person_last_name">Last Name</label>
  #     <input type="text" id="person_last_name" name="person[last_name]" />
  #     <input type="submit" value="Create" />
  #   </form>
  def form_for(name, attrs = {}, &blk)
    with_form_context(name, attrs.delete(:builder)) do
      current_form_context.form(attrs, &blk)
    end
  end
  
  # Creates a scope around a specific resource object like form_for, but doesnt create the form tags themselves.
  # This makes fields_for suitable for specifying additional resource objects in the same form. 
  #
  # ==== Examples
  #   <%= form_for @person do %>
  #     <%= text_field :first_name, :label => "<NAME>" %>
  #     <%= text_field :last_name,  :label => "<NAME>" %>
  #     <%= fields_for @permission do %>
  #       <%= check_box :is_admin, :label => "Administrator" %>
  #     <% end =%>
  #     <%= submit "Create" %>
  #   <% end =%>
  def fields_for(name, attrs = {}, &blk)
    attrs ||= {}
    with_form_context(name, attrs.delete(:builder)) do
      yield
    end
  end

  # Provides the ability to create quick fieldsets as blocks for your forms.
  #
  # ==== Parameters
  # attrs<Hash>:: HTML attributes and options
  #
{'fraction_non_alphanumeric': 0.1472986748216106, 'fraction_numerical': 0.0005096839959225281, 'mean_word_length': 3.249593936112615, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 108, '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': '6806484', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2582, 'n_words': 670}