<commit_before>module Admin::FormHelper

  def build_form(fields, form)

    options = { :form => form }

    returning(String.new) do |html|

      html << (error_messages_for :item, :header_tag => 'h3')

      fields.each do |key, value|

        if template = @resource[:class].typus_template(key)
          html << typus_template_field(key, template, options)
          next
        end

        html << case value
                when :belongs_to  then typus_belongs_to_field(key, options)
                when :tree        then typus_tree_field(key, :form => options[:form])
                when :boolean, :date, :datetime, :string, :text, :time,
                     :file, :password, :selector, :rich_text
                  typus_template_field(key, value, options)
                else
                  typus_template_field(key, :string, options)
                end
      end

    end

  end

  def typus_belongs_to_field(attribute, options)

    form = options[:form]

    ##
    # We only can pass parameters to 'new' and 'edit', so this hack makes
    # the work to replace the current action.
    #
    params[:action] = (params[:action] == 'create') ? 'new' : params[:action]

    back_to = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id])

    related = @resource[:class].reflect_on_association(attribute.to_sym).class_name.constantize
    related_fk = @resource[:class].reflect_on_association(attribute.to_sym).primary_key_name

    confirm = [ _("Are you sure you want to leave this page?"),
                _("If you have made any changes to the fields without clicking the Save/Update entry button, your changes will be lost."), 
                _("Click OK to continue, or click Cancel to stay on this page.") ]

    returning(String.new) do |html|

      message = link_to _("Add"), { :controller => "admin/#{related.class_name.tableize}", 
                                    :action => 'new', 
                                    :back_to => back_to, 
                                    :selected => related_fk }, 
                                    :confirm => confirm.join("\n\n") if @current_user.can?('create', related)

      if related.respond_to?(:roots)
        html << typus_tree_field(related_fk, :items => related.roots, 
                                             :attribute_virtual => related_fk, 
                                             :form => form)
      else
        values = related.find(:all, :order => related.typus_order_by).collect { |p| [p.to_label, p.id] }
        options = { :include_blank => true }
        html_options = { :disabled => attribute_disabled?(attribute) }
        label_text = @resource[:class].human_attribute_name(attribute)
        html << <<-HTML
<li>
  #{form.label label_text, "#{attribute.humanize} <small>#{message}</small>"}
  #{form.select related_fk, values, options, html_options }
</li>
        HTML
      end

    end

  end

  # OPTIMIZE
  def typus_tree_field(attribute, *args)

    options = args.extract_options!
    options[:items] ||= @resource[:class].roots
    options[:attribute_virtual] ||= 'parent_id'

    form = options[:form]

    values = expand_tree_into_select_field(options[:items], options[:attribute_virtual])

    label_text = @resource[:class].human_attribute_name(attribute)

    <<-HTML
<li>
  #{form.label label_text}<commit_msg>Fixed label. (Reported by <NAME>)<commit_after>module Admin::FormHelper

  def build_form(fields, form)

    options = { :form => form }

    returning(String.new) do |html|

      html << (error_messages_for :item, :header_tag => 'h3')

      fields.each do |key, value|

        if template = @resource[:class].typus_template(key)
          html << typus_template_field(key, template, options)
          next
        end

        html << case value
                when :belongs_to  then typus_belongs_to_field(key, options)
                when :tree        then typus_tree_field(key, :form => options[:form])
                when :boolean, :date, :datetime, :string, :text, :time,
                     :file, :password, :selector, :rich_text
                  typus_template_field(key, value, options)
                else
                  typus_template_field(key, :string, options)
                end
      end

    end

  end

  def typus_belongs_to_field(attribute, options)

    form = options[:form]

    ##
    # We only can pass parameters to 'new' and 'edit', so this hack makes
    # the work to replace the current action.
    #
    params[:action] = (params[:action] == 'create') ? 'new' : params[:action]

    back_to = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id])

    related = @resource[:class].reflect_on_association(attribute.to_sym).class_name.constantize
    related_fk = @resource[:class].reflect_on_association(attribute.to_sym).primary_key_name

    confirm = [ _("Are you sure you want to leave this page?"),
                _("If you have made any changes to the fields without clicking the Save/Update entry button, your changes will be lost."), 
                _("Click OK to continue, or click Cancel to stay on this page.") ]

    returning(String.new) do |html|

      message = link_to _("Add"), { :controller => "admin/#{related.class_name.tableize}", 
                                    :action => 'new', 
                                    :back_to => back_to, 
                                    :selected => related_fk }, 
                                    :confirm => confirm.join("\n\n") if @current_user.can?('create', related)

      if related.respond_to?(:roots)
        html << typus_tree_field(related_fk, :items => related.roots, 
                                             :attribute_virtual => related_fk, 
                                             :form => form)
      else
        values = related.find(:all, :order => related.typus_order_by).collect { |p| [p.to_label, p.id] }
        options = { :include_blank => true }
        html_options = { :disabled => attribute_disabled?(attribute) }
        label_text = @resource[:class].human_attribute_name(attribute)
        html << <<-HTML
<li>
  #{form.label related_fk, "#{label_text} <small>#{message}</small>"}
  #{form.select related_fk, values, options, html_options }
</li>
        HTML
      end

    end

  end

  # OPTIMIZE
  def typus_tree_field(attribute, *args)

    options = args.extract_options!
    options[:items] ||= @resource[:class].roots
    options[:attribute_virtual] ||= 'parent_id'

    form = options[:form]

    values = expand_tree_into_select_field(options[:items], options[:attribute_virtual])

    label_text = @resource[:class].human_attribute_name(attribute)

    <<-HTML
<li>
  #{form.label label_text}
{'fraction_non_alphanumeric': 0.12231063955201886, 'fraction_numerical': 0.0002947244326554671, 'mean_word_length': 2.302676399026764, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7535205', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1900, 'n_words': 465}