<commit_before>module RailsAdminNestedSet
  module Helper
    def rails_admin_nested_set(tree, opts= {})
      if @abstract_model.model == Product
        tree = tree.to_a.sort_by { |m| m.galleries_id.present? ? m.galleries_id : m.lft }
      else
        tree = tree.to_a.sort_by { |m| m.lft }
      end
      roots = tree.select{|elem| elem.parent_id.nil?}
      id = "ns_#{rand(100_000_000..999_999_999)}"
      content = content_tag(:ol, rails_admin_nested_set_builder(roots, tree), id: id, class: 'dd-list')
      js = "rails_admin_nested_set({id: '#{id}', max_depth: #{max_depth}, update_url: '#{nested_set_path(model_name: @abstract_model)}'});"
      content + content_tag(:script, js.html_safe, type: 'text/javascript')
    end

    def g_link(node, fv, on, badge, meth)
      link_to(
        fv.html_safe,
        toggle_path(model_name: @abstract_model, id: node.id, method: meth, on: on.to_s),
        class: 'badge ' + badge,
        onclick: 'var $t = $(this); $t.html("<i class=\"fa fa-spinner fa-spin\"></i>"); $.ajax({type: "POST", url: $t.attr("href"), data: {ajax:true}, success: function(r) { $t.attr("href", r.href); $t.attr("class", r.class); $t.text(r.text); $t.parent().attr("title", r.text); }, error: function(e) { alert(e.responseText); }}); return false;'
      )
    end

    def rails_admin_nested_set_builder(nodes, tree)
      nodes.map do |node|
        li_classes = 'dd-item dd3-item'

        content_tag :li, class: li_classes, :'data-id' => node.id do
          output = content_tag :div, 'drag', class: 'dd-handle dd3-handle'
          output+= content_tag :div, class: 'dd3-content clearfix' do
            content = ''.html_safe

            toggle_fields.each do |tf|
              if node.respond_to?(tf) && respond_to?(:toggle_path)
                content += case node.enabled
                  when nil
                    g_link(node, '&#x2718;', 0, 'badge-important', tf) + g_link(node, '&#x2713;', 1, 'badge-success', tf)
                  when false
                    g_link(node, '&#x2718;', 1, 'badge-important', tf)
                  when true
                    g_link(node, '&#x2713', 0, 'badge-success', tf)
                  else
                    %{<span class="badge">-</span>}
                end.html_safe
              end
            end

            content += link_to @model_config.with(object: node).object_label, edit_path(@abstract_model, node.id)
            if @abstract_model.model == Product
              content += content_tag(:span, "#{@abstract_model.model.find(node.id).gallery.try(name)}", style: "margin-left: 150px;")
            end
            content += content_tag(:div, action_links(node), class: 'pull-right links')
            
            thumbnail_fields.each do |mth|
              if node.respond_to?(mth)
                img = if paperclip?
                  node.send(mth).url(thumbnail_size)
                elsif carrierwave?
                  node.send(mth, thumbnail_size)<commit_msg>bug fix<commit_after>module RailsAdminNestedSet
  module Helper
    def rails_admin_nested_set(tree, opts= {})
      if @abstract_model.model == Product
        tree = tree.to_a.sort_by { |m| m.galleries_id.present? ? m.galleries_id : m.lft }
      else
        tree = tree.to_a.sort_by { |m| m.lft }
      end
      roots = tree.select{|elem| elem.parent_id.nil?}
      id = "ns_#{rand(100_000_000..999_999_999)}"
      content = content_tag(:ol, rails_admin_nested_set_builder(roots, tree), id: id, class: 'dd-list')
      js = "rails_admin_nested_set({id: '#{id}', max_depth: #{max_depth}, update_url: '#{nested_set_path(model_name: @abstract_model)}'});"
      content + content_tag(:script, js.html_safe, type: 'text/javascript')
    end

    def g_link(node, fv, on, badge, meth)
      link_to(
        fv.html_safe,
        toggle_path(model_name: @abstract_model, id: node.id, method: meth, on: on.to_s),
        class: 'badge ' + badge,
        onclick: 'var $t = $(this); $t.html("<i class=\"fa fa-spinner fa-spin\"></i>"); $.ajax({type: "POST", url: $t.attr("href"), data: {ajax:true}, success: function(r) { $t.attr("href", r.href); $t.attr("class", r.class); $t.text(r.text); $t.parent().attr("title", r.text); }, error: function(e) { alert(e.responseText); }}); return false;'
      )
    end

    def rails_admin_nested_set_builder(nodes, tree)
      nodes.map do |node|
        li_classes = 'dd-item dd3-item'

        content_tag :li, class: li_classes, :'data-id' => node.id do
          output = content_tag :div, 'drag', class: 'dd-handle dd3-handle'
          output+= content_tag :div, class: 'dd3-content clearfix' do
            content = ''.html_safe

            toggle_fields.each do |tf|
              if node.respond_to?(tf) && respond_to?(:toggle_path)
                content += case node.enabled
                  when nil
                    g_link(node, '&#x2718;', 0, 'badge-important', tf) + g_link(node, '&#x2713;', 1, 'badge-success', tf)
                  when false
                    g_link(node, '&#x2718;', 1, 'badge-important', tf)
                  when true
                    g_link(node, '&#x2713', 0, 'badge-success', tf)
                  else
                    %{<span class="badge">-</span>}
                end.html_safe
              end
            end

            content += link_to @model_config.with(object: node).object_label, edit_path(@abstract_model, node.id)
            if @abstract_model.model == Product
              product = @abstract_model.model.find(node.id)
              content += content_tag(:span, "#{product.gallery.name}", style: "margin-left: 150px;") if product.galleries_id.present?
            end
            content += content_tag(:div, action_links(node), class: 'pull-right links')
            
            thumbnail_fields.each do |mth|
              if node.respond_to?(mth)
                img = if paperclip?
                  node.send(mth).url(thumbnail_size)
                elsif carrierwave?
                  node.send(mth, thumbnail_size)
{'fraction_non_alphanumeric': 0.15001659475605708, 'fraction_numerical': 0.014603385330235646, 'mean_word_length': 2.280892759934676, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '3851808', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1961, 'n_words': 414}