<commit_before>class Admin::SidebarController < Admin::BaseController
  def index
    @sidebars = Sidebars::SidebarController.available_sidebars.inject({}) do |hash,sidebar|
      hash[sidebar.short_name]=sidebar
      hash
    end

    @active = Sidebar.find_all_staged.select {|s| @sidebars[s.controller] }
    @available = @sidebars.values.sort { |a,b| a.name <=> b.name }
  end

  def show_available
    render :partial => 'availables', :object => available
  end

  def set_active

    # Get all available plugins
    availablemap = available.inject({}) do |hash, item|
      hash[item.short_name] = item
      hash
    end

    # Get all already active plugins
    activemap = Sidebar.find_all_staged.inject({}) do |hash, item|
      hash[item.html_id] = item
      hash
    end

    # Figure out which plugins are referenced by the params[:active] array and
    # lay them out in a easy accessible sequencial array
    @active = params[:active].inject([]) do |array, name|
      if availablemap.has_key?(name)
        newitem = Sidebar.new
        newitem.controller = name
        newitem.config= availablemap[name].default_config

        array.push newitem
      elsif activemap.has_key?(name)
        array.push activemap[name]
      end
      array
    end

    # Update the staged_position of all the sidebar items in accordance with
    # the params[:active] array
    Sidebar.transaction do
      Sidebar.update_all('staged_position=null')
      @active.each_index do |i|
        @active[i].staged_position=i
        @active[i].save
      end
      Sidebar.purge
    end

    render :partial => 'actives', :object => @active
  end

  def nothing
    render :nothing => true
  end

  def remove
    sidebar = Sidebar.find(params[:id])
    sidebar.staged_position = nil
    sidebar.save

    render :nothing => true
  end

  def publish
    Sidebar.transaction do
      params[:configure].each do |id, attribs|
        sb = Sidebar.find(id)
        sb.config = attribs
        sb.save
        sb.publish
        sb.save
      end
      Sidebar.purge
    end
    render :partial=>'publish'
  end
<commit_msg>Tentative fix for #786. There's still more work to do on sidebars...<commit_after>class Admin::SidebarController < Admin::BaseController
  def index
    @sidebars = Sidebars::SidebarController.available_sidebars.inject({}) do |hash,sidebar|
      hash[sidebar.short_name]=sidebar
      hash
    end
    # Reset the staged position based on the active position.
    Sidebar.delete_all('active_position is null')
    @active = Sidebar.find(:all).select do |sb|
      sb.staged_position = sb.active_position
      sb.save
      @sidebars[sb.controller]
    end
    @available = @sidebars.values.sort { |a,b| a.name <=> b.name }
  end

  def show_available
    render :partial => 'availables', :object => available
  end

  def set_active

    # Get all available plugins
    availablemap = available.inject({}) do |hash, item|
      hash[item.short_name] = item
      hash
    end

    # Get all already active plugins
    activemap = Sidebar.find_all_staged.inject({}) do |hash, item|
      hash[item.html_id] = item
      hash
    end

    # Figure out which plugins are referenced by the params[:active] array and
    # lay them out in a easy accessible sequencial array
    @active = params[:active].inject([]) do |array, name|
      if availablemap.has_key?(name)
        newitem = Sidebar.new
        newitem.controller = name
        newitem.config= availablemap[name].default_config

        array.push newitem
      elsif activemap.has_key?(name)
        array.push activemap[name]
      end
      array
    end

    # Update the staged_position of all the sidebar items in accordance with
    # the params[:active] array
    Sidebar.transaction do
      Sidebar.update_all('staged_position=null')
      @active.each_index do |i|
        @active[i].staged_position=i
        @active[i].save
      end
      Sidebar.purge
    end

    render :partial => 'actives', :object => @active
  end

  def nothing
    render :nothing => true
  end

  def remove
    sidebar = Sidebar.find(params[:id])
    sidebar.staged_position = nil
    sidebar.save

    render :nothing => true
  end

  def publish
    Sidebar.transaction do
      params[:configure].each do |id, attribs|
        sb = Sidebar.find(id)
        sb.config = attribs
        sb.save
      end
      Sidebar.find(:all).each do |sb|
        sb.publish
        sb.save
      end
      Sidebar.purge
    end
    render :partial=>'publish'
  end

{'fraction_non_alphanumeric': 0.09569060773480663, 'fraction_numerical': 0.0006629834254143647, 'mean_word_length': 2.5806962025316458, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '1358300', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1354, 'n_words': 372}