<commit_before>module CobblesHelper
  class Cobble
    cattr_reader :current

    attr_reader :name, :block, :id, :title, :content
    attr_accessor :position

    def initialize(cobbler, name, options = {}, &block)
      @cobbler = cobbler
      @name = name
      @id = options[:id] || name.to_s.parameterize.dasherize
      @title = options[:title] || @name.tl(default: ["attributes.#{@name}".to_sym, @name.to_s.humanize])
      @@current = self
      @content = @cobbler.template.capture(&block)
      @position = options[:position] || 1000
      @@current = nil
    end

    def others
      @cobbler.items.select { |c| c.id != @id }
    end
  end

  class Cobbler
    attr_reader :items, :template, :name

    def initialize(template, name, options = {})
      @template = template
      @name = name
      @items = []
      @order = options[:order] || []
    end

    def cobble(name, options = {}, &block)
      if @items.detect { |i| i.name.to_s == name.to_s }
        raise "Already taken. You already use #{name.inspect}"
      end
      raise "Need a block for #{name} in cobbler #{@name}" unless block_given?
      @items << Cobble.new(self, name, options, &block)
    end

    def any?
      @items.any?
    end

    def sort!
      @order.each_with_index do |name, index|
        @items.select do |cobble|
          cobble.id.to_s == name.to_s
        end.each do |cobble|
          cobble.position = index + 1
        end
      end
      @items.sort! do |a, b|
        a.position <=> b.position
      end
    end

    def each(&block)
      @items.each(&block)
    end
  end

  # Cobbles are a simple layout with all cobble in one list.
  # List is sortable and cobbles are hideable/collapseable
  def cobbles(options = {}, &_block)
    name = options[:name] || "#{controller_name}-#{action_name}".to_sym
    config = YAML.load(current_user.preference("cobbler.#{name}", {}.to_yaml).value).deep_symbolize_keys
    cobbler = Cobbler.new(self, name, order: config[:order])
    yield cobbler
    if cobbler.any?
      cobbler.sort!
      render 'cobbles', cobbler: cobbler
    end
  end

  def cobble_toolbar(options = {}, &block)
    content_for("cobble_#{Cobble.current.id}_main_toolbar".to_sym, toolbar(options.merge(wrap: false), &block))
    nil
  end

  def cobble_meta_toolbar(&block)
    id = Cobble.current.id
    content_for("cobble_#{id}_meta_toolbar".to_sym, &block)
  end

  def cobble_list(name, options = {}, &block)
    id = Cobble.current.id
    list(name, options.deep_merge(content_for: {
                                    settings:   "cobble_#{id}_meta_toolbar".to_sym,
                                    pagination: "cobble_#{id}_meta_toolbar".to_sym,
                                    actions:    "cobble_#{id}_main_toolbar".to_sym
                                  }), &block)<commit_msg>Adds a way to inject cobble fragment in a view<commit_after>module CobblesHelper
  class Cobble
    cattr_reader :current

    attr_reader :name, :block, :id, :title, :content
    attr_accessor :position

    def initialize(cobbler, name, options = {}, &block)
      @cobbler = cobbler
      @name = name
      @id = options[:id] || name.to_s.parameterize.dasherize
      @title = options[:title] || @name.tl(default: ["attributes.#{@name}".to_sym, @name.to_s.humanize])
      @@current = self
      @content = @cobbler.template.capture(&block)
      @position = options[:position] || 1000
      @@current = nil
    end

    def others
      @cobbler.items.select { |c| c.id != @id }
    end
  end

  class Cobbler
    attr_reader :items, :template, :name

    def initialize(template, name, options = {})
      @template = template
      @name = name
      @items = []
      @order = options[:order] || []
    end

    def cobble(name, options = {}, &block)
      if @items.detect { |i| i.name.to_s == name.to_s }
        raise "Already taken. You already use #{name.inspect}"
      end
      raise "Need a block for #{name} in cobbler #{@name}" unless block_given?
      @items << Cobble.new(self, name, options, &block)
    end

    def any?
      @items.any?
    end

    def sort!
      @order.each_with_index do |name, index|
        @items.select do |cobble|
          cobble.id.to_s == name.to_s
        end.each do |cobble|
          cobble.position = index + 1
        end
      end
      @items.sort! do |a, b|
        a.position <=> b.position
      end
    end

    def each(&block)
      @items.each(&block)
    end


    def view_addons(options = {})
      return nil unless options[:controller].present?

      options[:action] ||= :index
      options[:context] = :cobble

      Ekylibre::Plugin.find_addons(options).each do |addon|
        @template.render partial: addon, locals: {c: self}
      end
    end

  end

  # Cobbles are a simple layout with all cobble in one list.
  # List is sortable and cobbles are hideable/collapseable
  def cobbles(options = {}, &_block)
    name = options[:name] || "#{controller_name}-#{action_name}".to_sym
    config = YAML.load(current_user.preference("cobbler.#{name}", {}.to_yaml).value).deep_symbolize_keys
    cobbler = Cobbler.new(self, name, order: config[:order])
    yield cobbler

    cobbler.view_addons controller: controller_name, action: action_name

    if cobbler.any?
      cobbler.sort!
      render 'cobbles', cobbler: cobbler
    end
  end

  def cobble_toolbar(options = {}, &block)
    content_for("cobble_#{Cobble.current.id}_main_toolbar".to_sym, toolbar(options.merge(wrap: false), &block))
    nil
  end

  def cobble_meta_toolbar(&block)
    id = Cobble.current.id
    content_for("cobble_#{id}_meta_toolbar".to_sym, &block)
  end

  def cobble_list(name, options = {}, &block)
    id = Cobble.current.id
    list(name, options.deep_merge(content_for: {
                                    settings:   "cobble_#{id}_meta_toolbar".to_sym,
                                    pagination: "cobble_#{id}_meta_toolbar".to_sym,
                                    actions:    "cobble_#{id}_main_toolbar".to_sym
                                  }), &block)
{'fraction_non_alphanumeric': 0.12730870712401055, 'fraction_numerical': 0.0016490765171503958, 'mean_word_length': 2.422686230248307, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4712549', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1980, 'n_words': 456}