<commit_before>require 'action_view/helpers'

module FormSan
  class FormBuilder < ActionView::Helpers::FormBuilder
    def fieldset(&block)
      @template.content_tag(:div, :class => 'fieldset', &block)
    end
    
    def fields(&block)
      @template.content_tag(:div, :class => 'fields', &block)
    end
    
    def error_messages
      unless @object.errors.count.zero?
        attributes_with_errors =  @object.errors.map { |attribute, _| attribute } - ['base']
        @template.content_tag(:p, :class => 'errors') do
          if attributes_with_errors.size > 1
            @template.concat "Sorry, there were problems with the #{attributes_with_errors.to_sentence}."
          elsif attributes_with_errors.size == 1
            @template.concat "Sorry, there was a problem with the #{attributes_with_errors.first}."
          else
            @template.concat "#{@object.class} #{@object.errors.on(:base)}."
          end
        end
      else
        ''
      end
    end
    
    def error_message(attribute)
      unless @object.errors.on(attribute).blank?
        @template.content_tag(:p, :class => 'notice') do
          @template.concat ERB::Util.html_escape(@object.errors.on(attribute).mb_chars.capitalize)
        end
      else
        ''
      end
    end
    
    def wrapped_label(attribute, *args)
      options = args.extract_options!
            
      label_text = args.first || attribute.to_s.humanize
      label_text << ' <span>(optional)</span>' if options[:optional]
      
      @template.content_tag(:div, :class => 'label') do
        @template.concat label(attribute, label_text)
      end
    end
    
    def field_with_extra_content(attribute, extra_content=nil, *args)
      options = args.extract_options!
      classes = @object.errors.on(attribute) ? 'invalid field' : 'field'
      
      @template.content_tag(:div, :class => classes) do
        @template.concat wrapped_label(attribute, args.first, :optional => options.delete(:optional))
        
        case input_type = options.delete(:type).to_s
        when 'textarea'
          @template.concat ActionView::Helpers::InstanceTag.new(@object_name, attribute, self, @object).to_text_area_tag(options)
        else
          @template.concat ActionView::Helpers::InstanceTag.new(@object_name, attribute, self, @object).to_input_field_tag(input_type, options)
        end
        
        @template.concat error_message(attribute)
        @template.concat extra_content unless extra_content.blank?
        @template.output_buffer # The block needs to return the current buffer
      end
    end
    
    def field(attribute, *args, &block)
      if block_given?
        @template.concat field_with_extra_content(attribute, @template.capture(&block), *args)
      else
        field_with_extra_content(attribute, nil, *args)
      end
    end
  end
end

class ActionView::Helpers::InstanceTag
  def error_wrapping(html_tag, has_error)
    html_tag
  end
end<commit_msg>Delegate concat and content_tag for readability.<commit_after>require 'action_view/helpers'

module FormSan
  class FormBuilder < ActionView::Helpers::FormBuilder
    attr_reader :template
    delegate :concat, :content_tag, :to => :template
    
    def fieldset(&block)
      content_tag(:div, :class => 'fieldset', &block)
    end
    
    def fields(&block)
      content_tag(:div, :class => 'fields', &block)
    end
    
    def error_messages
      unless @object.errors.count.zero?
        attributes_with_errors =  @object.errors.map { |attribute, _| attribute } - ['base']
        content_tag(:p, :class => 'errors') do
          if attributes_with_errors.size > 1
            concat "Sorry, there were problems with the #{attributes_with_errors.to_sentence}."
          elsif attributes_with_errors.size == 1
            concat "Sorry, there was a problem with the #{attributes_with_errors.first}."
          else
            concat "#{@object.class} #{@object.errors.on(:base)}."
          end
        end
      else
        ''
      end
    end
    
    def error_message(attribute)
      unless @object.errors.on(attribute).blank?
        content_tag(:p, :class => 'notice') do
          concat ERB::Util.html_escape(@object.errors.on(attribute).mb_chars.capitalize)
        end
      else
        ''
      end
    end
    
    def wrapped_label(attribute, *args)
      options = args.extract_options!
            
      label_text = args.first || attribute.to_s.humanize
      label_text << ' <span>(optional)</span>' if options[:optional]
      
      content_tag(:div, :class => 'label') do
        concat label(attribute, label_text)
      end
    end
    
    def field_with_extra_content(attribute, extra_content=nil, *args)
      options = args.extract_options!
      classes = @object.errors.on(attribute) ? 'invalid field' : 'field'
      
      content_tag(:div, :class => classes) do
        concat wrapped_label(attribute, args.first, :optional => options.delete(:optional))
        
        case input_type = options.delete(:type).to_s
        when 'textarea'
          concat ActionView::Helpers::InstanceTag.new(@object_name, attribute, self, @object).to_text_area_tag(options)
        else
          concat ActionView::Helpers::InstanceTag.new(@object_name, attribute, self, @object).to_input_field_tag(input_type, options)
        end
        
        concat error_message(attribute)
        concat extra_content unless extra_content.blank?
        @template.output_buffer # The block needs to return the current buffer
      end
    end
    
    def field(attribute, *args, &block)
      if block_given?
        concat field_with_extra_content(attribute, @template.capture(&block), *args)
      else
        field_with_extra_content(attribute, nil, *args)
      end
    end
  end
end

class ActionView::Helpers::InstanceTag
  def error_wrapping(html_tag, has_error)
    html_tag
  end
end
{'fraction_non_alphanumeric': 0.10694279409268376, 'fraction_numerical': 0.0006790018672551349, 'mean_word_length': 2.722046746683512, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 24, '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': '5582469', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1697, 'n_words': 375}