Sha256: a1187e2c40968b0a975b4f7bd4ebc62a9bde38379a1ea209715d4cbe2def47aa

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

require 'active_admin/helpers/optional_display'

module ActiveAdmin
  module Sidebar

    def self.included(base)
      base.send :extend, ClassMethods
      base.class_inheritable_accessor :sidebar_sections
      base.sidebar_sections = []
    end

    module ClassMethods

      def sidebar(name, options = {}, &block)
        self.sidebar_sections << ActiveAdmin::Sidebar::Section.new(name, options, &block)
      end

      def clear_sidebar_sections!
        self.sidebar_sections = []
      end

      def sidebar_sections_for(action)
        sidebar_sections.select{|section| section.display_on?(action) }
      end
    end

    class Section
      include ActiveAdmin::OptionalDisplay
  
      attr_accessor :name, :options, :block

      def initialize(name, options = {}, &block)
        @name, @options, @block = name, options, block
        normalize_display_options!
      end

      # The id gets used for the div in the view
      def id
        name.to_s.downcase.underscore + '_sidebar_section'
      end

      # The title gets displayed within the section in the view
      def title
        name.to_s.titlecase
      end

      # If a block is not passed in, the name of the partial to render
      def partial_name
        options[:partial] || "#{name.to_s.downcase.gsub(' ', '_')}_sidebar"
      end
    end

    class Renderer < ActiveAdmin::Renderer
      def to_html(sidebar_sections)
        sidebar_sections.collect do |section|
          title   = content_tag :h3, section.title
          content = content_tag :div, sidebar_content(section)

          content_tag :div, :class => 'sidebar_section', :id => section.id do
            title + content
          end
        end.join
      end

      # If a block exists, render the block. Otherwise render a partial
      def sidebar_content(section)
        if section.block
          instance_eval(&section.block)
        else
          capture do
            render section.partial_name
          end
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeadmin-0.1.1 lib/active_admin/sidebar.rb
activeadmin-0.1.0 lib/active_admin/sidebar.rb