<commit_before>module BootstrapAdminHelper
  # =============================================================================
  # This re-implementation of link_to simply looks at the link label and if it
  # falls under certain conditions, then it tries to create a label and/or url
  # accordingly. Else works normally
  #
  # @param *args Same as the original link_to
  # @param &block Same as the original link_to
  #
  # @return [String] link markup
  #
  # @example Assuming locale is set to PT
  #  link_to :Show, @document
  #    # <a href='/documents/1'>Ver</a>
  #
  #  link_to Document
  #    # <a href='/documents'>Documentos</a>
  #
  #  link_to [:edit, @document]
  #    # <a href='/document/1/edit'>Editar Documento</a>
  #
  def link_to(*args, &block)
    super(*args, &block) if block_given?

    # When args[0] is a symbol, then just translate the symbol...
    if args[0].is_a? Symbol
      super(t(args[0]), *args[1..-1])

    # when arg[0] is a ActiveRecord class...
    elsif args[0].is_a?(Class) and args[0] < ActiveRecord::Base
      label = args[0].model_name.human.pluralize
      if args.length == 1
        controller_name = args[0].name.underscore.pluralize
        super(label, url_for(controller: controller_name, action: "index"))
      else
        super(label, *args[1..-1])
      end

    elsif args[0].is_a?(Array) && args[0][0].is_a?(Symbol) && args[0][1] < ActiveRecord::Base
      link_content = t("helpers.submit.#{args[0][0]}", :model => args[0][1].model_name.human)
      super(link_content, *args[1..-1])
    else
      super(*args)
    end
  end

  # =============================================================================
  # Builds the markup to properly display a field, including label and field value.
  #
  # If a block is given, then it uses the block as the field "value"
  # else it looks at what the field value is and if it is:
  #   * a boolean, builds a fake checkbox
  #   * an array, builds a ul list with each element of the array
  #   * else, just uses the string value
  #
  # @param item [ActiveRecord::Base] The item record
  # @param field [Symbol or String] The field to display
  # @param &block [Block] An optional block that yields markup
  #
  # @return [Markup] the necessary markup to display the field
  #
  # @example
  #   display_field @document, :title
  #     # <p><b>Title: </b>So Long, and Thanks For All the Fish</p>
  #
  #   display_field @document, :awesome?
  #     # <p><b>Awesome: </b><span class='checkbox true'><span class='icon'></span></span></p>
  #
  #   display_field @document, :authors
  #     # <p><b>Authors: </b><ul><li><NAME></li></ul></p>
  #
  #   display_field @document, :details do
  #     "#{@document.title} - #{@document.year}"
  #   end
  #     # <p><b>Title: </b>So Long, and Thanks For All the Fish - 1984</p>
  #
  def display_field item, field, &block
    content_tag :p do
      content_tag(:b) do
        item.class.human_attribute_name(field) + ": "
      end +
      if block_given?
        capture(&block)
      else
        val = render_field item, field
        if val.class.name =~ /(TrueClass|FalseClass)/
          content_tag(:span, :class => "checkbox #{val.to_s}") do
            content_tag(:span, :class=>"icon"){""}
          end
        elsif val.is_a? Array
          content_tag :ul do
            val.map do |uniq_val|
              content_tag(:li, uniq_val.to_s)
            end.join.html_safe
          end
        else
          val.to_s
        end
      end
    end.html_safe
  end

  # =============================================================================
  def render_field item, field
    if self.respond_to?(helper = "#{params[:action]}_#{field}") ||
       self.respond_to?(helper = "#{field}")
      send helper, item
    else
      item.send field
    end
  end

  # =============================================================================
  def render_form_field form, field
    if self.respond_to?(helper = "#{params[:action]}_form_#{field.name}") ||
       self.respond_to?(helper = "form_#{field.name}")
      send helper, form

    elsif field.association?
      form.association field.name

    else
      form.input field.name
    end
  end

  # =============================================================================
  def available_actions
    bootstrap_admin_config.available_actions
  end


  def bootstrap_url_for options = {}
    defaults = {:controller => params[:controller]}.merge options

    ctrl_namespace = defaults[:controller].split("/").first
    if instance_eval "defined? #{ctrl_namespace}"
      instance_eval("#{ctrl_namespace}.method :url_for").call defaults
    else
      main_app.url_for(defaults)
    end
  end
  alias_method :bootstrap_url, :bootstrap_url_for

  def bootstrap_form_url item
    item_name = item.class.name.underscore.gsub("/","_")
    [item, :as => item_name, :url => bootstrap_url_for(:action => (item.new_record? ? :create : :update), :id => item.id)]
  end

  # =============================================================================
  def index_actions_for item
    actions = []

    if available_actions.include? :show
      actions << link_to(:show, bootstrap_url_for(:action => :show, :id => item.id), class: 'btn')
    end

    if available_actions.include? :edit
      actions << link_to(:edit, bootstrap_url_for(:action => :edit, :id => item.id), class: 'btn')
    end<commit_msg>Fix for engines with shared namespace in bootstrap_url_for<commit_after>module BootstrapAdminHelper
  # =============================================================================
  # This re-implementation of link_to simply looks at the link label and if it
  # falls under certain conditions, then it tries to create a label and/or url
  # accordingly. Else works normally
  #
  # @param *args Same as the original link_to
  # @param &block Same as the original link_to
  #
  # @return [String] link markup
  #
  # @example Assuming locale is set to PT
  #  link_to :Show, @document
  #    # <a href='/documents/1'>Ver</a>
  #
  #  link_to Document
  #    # <a href='/documents'>Documentos</a>
  #
  #  link_to [:edit, @document]
  #    # <a href='/document/1/edit'>Editar Documento</a>
  #
  def link_to(*args, &block)
    super(*args, &block) if block_given?

    # When args[0] is a symbol, then just translate the symbol...
    if args[0].is_a? Symbol
      super(t(args[0]), *args[1..-1])

    # when arg[0] is a ActiveRecord class...
    elsif args[0].is_a?(Class) and args[0] < ActiveRecord::Base
      label = args[0].model_name.human.pluralize
      if args.length == 1
        controller_name = args[0].name.underscore.pluralize
        super(label, url_for(controller: controller_name, action: "index"))
      else
        super(label, *args[1..-1])
      end

    elsif args[0].is_a?(Array) && args[0][0].is_a?(Symbol) && args[0][1] < ActiveRecord::Base
      link_content = t("helpers.submit.#{args[0][0]}", :model => args[0][1].model_name.human)
      super(link_content, *args[1..-1])
    else
      super(*args)
    end
  end

  # =============================================================================
  # Builds the markup to properly display a field, including label and field value.
  #
  # If a block is given, then it uses the block as the field "value"
  # else it looks at what the field value is and if it is:
  #   * a boolean, builds a fake checkbox
  #   * an array, builds a ul list with each element of the array
  #   * else, just uses the string value
  #
  # @param item [ActiveRecord::Base] The item record
  # @param field [Symbol or String] The field to display
  # @param &block [Block] An optional block that yields markup
  #
  # @return [Markup] the necessary markup to display the field
  #
  # @example
  #   display_field @document, :title
  #     # <p><b>Title: </b>So Long, and Thanks For All the Fish</p>
  #
  #   display_field @document, :awesome?
  #     # <p><b>Awesome: </b><span class='checkbox true'><span class='icon'></span></span></p>
  #
  #   display_field @document, :authors
  #     # <p><b>Authors: </b><ul><li><NAME></li></ul></p>
  #
  #   display_field @document, :details do
  #     "#{@document.title} - #{@document.year}"
  #   end
  #     # <p><b>Title: </b>So Long, and Thanks For All the Fish - 1984</p>
  #
  def display_field item, field, &block
    content_tag :p do
      content_tag(:b) do
        item.class.human_attribute_name(field) + ": "
      end +
      if block_given?
        capture(&block)
      else
        val = render_field item, field
        if val.class.name =~ /(TrueClass|FalseClass)/
          content_tag(:span, :class => "checkbox #{val.to_s}") do
            content_tag(:span, :class=>"icon"){""}
          end
        elsif val.is_a? Array
          content_tag :ul do
            val.map do |uniq_val|
              content_tag(:li, uniq_val.to_s)
            end.join.html_safe
          end
        else
          val.to_s
        end
      end
    end.html_safe
  end

  # =============================================================================
  def render_field item, field
    if self.respond_to?(helper = "#{params[:action]}_#{field}") ||
       self.respond_to?(helper = "#{field}")
      send helper, item
    else
      item.send field
    end
  end

  # =============================================================================
  def render_form_field form, field
    if self.respond_to?(helper = "#{params[:action]}_form_#{field.name}") ||
       self.respond_to?(helper = "form_#{field.name}")
      send helper, form

    elsif field.association?
      form.association field.name

    else
      form.input field.name
    end
  end

  # =============================================================================
  def available_actions
    bootstrap_admin_config.available_actions
  end


  def bootstrap_url_for options = {}
    defaults = {:controller => params[:controller]}.merge options

    ctrl_namespace = defaults[:controller].split("/").first
    # Fix for engines with shared namespace for some reason main_app.url_for doesn't find the route without this "/" 
    # It consider the route inside of engine instead of main app. 
    defaults[:controller] = "/#{defaults[:controller]}"

    if instance_eval "defined? #{ctrl_namespace}"
      instance_eval("#{ctrl_namespace}.method :url_for").call defaults
    else
      main_app.url_for(defaults)
    end
  end
  alias_method :bootstrap_url, :bootstrap_url_for

  def bootstrap_form_url item
    item_name = item.class.name.underscore.gsub("/","_")
    [item, :as => item_name, :url => bootstrap_url_for(:action => (item.new_record? ? :create : :update), :id => item.id)]
  end

  # =============================================================================
  def index_actions_for item
    actions = []

    if available_actions.include? :show
      actions << link_to(:show, bootstrap_url_for(:action => :show, :id => item.id), class: 'btn')
    end

    if available_actions.include? :edit
      actions << link_to(:edit, bootstrap_url_for(:action => :edit, :id => item.id), class: 'btn')
    end
{'fraction_non_alphanumeric': 0.20546713425051705, 'fraction_numerical': 0.005395198273536552, 'mean_word_length': 3.1686656671664166, 'pattern_counts': {'":': 2, '<': 77, '<?xml version=': 0, '>': 89, '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': '4025058', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3357, 'n_words': 905}