Sha256: c2fa15f54349634f1b9bd77e3911c5c38b6f99dcf2a7842444c0603bdd7d6951

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

module ActiveAdmin
  module Views

     # = Index as a Grid
     #
     # Sometimes you want to display the index screen for a set of resources as a grid
     # (possibly a grid of thumbnail images). To do so, use the :grid option for the
     # index block.
     #
     #     index :as => :grid do |product|
     #       link_to(image_tag(product.image_path), admin_products_path(product))
     #     end
     #
     # The block is rendered within a cell in the grid once for each resource in the
     # collection. The resource is passed into the block for you to use in the view.
     #
     # You can customize the number of colums that are rendered using the columns
     # option:
     #
     #     index :as => :grid, :columns => 5 do |product|
     #       link_to(image_tag(product.image_path), admin_products_path(product))
     #     end
     #
     class IndexAsGrid < ActiveAdmin::Component

      def build(page_presenter, collection)
        @page_presenter = page_presenter
        @collection = collection
        build_table
      end

      def number_of_columns
        @page_presenter[:columns] || default_number_of_columns
      end

      protected

      def build_table
        table :class => "index_grid" do
          collection.in_groups_of(number_of_columns).each do |group|
            build_row(group)
          end
        end
      end

      def build_row(group)
        tr do
          group.each do |item|
            item ? build_item(item) : build_empty_cell
          end
        end
      end

      def build_item(item)
        td :for => item do
          instance_exec(item, &@page_presenter.block)
        end
      end

      def build_empty_cell
        td '&nbsp;'.html_safe
      end

      def default_number_of_columns
        3
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
activeadmin-0.4.4 lib/active_admin/views/index_as_grid.rb
activeadmin-0.4.3 lib/active_admin/views/index_as_grid.rb
activeadmin-0.4.2 lib/active_admin/views/index_as_grid.rb
activeadmin-0.4.1 lib/active_admin/views/index_as_grid.rb
activeadmin-0.4.0 lib/active_admin/views/index_as_grid.rb
andrewroth_activeadmin-0.3.4.4 lib/active_admin/views/index_as_grid.rb
andrewroth_activeadmin-0.3.4.3 lib/active_admin/views/index_as_grid.rb
andrewroth_activeadmin-0.3.4.2 lib/active_admin/views/index_as_grid.rb
andrewroth_activeadmin-0.3.4.1 lib/active_admin/views/index_as_grid.rb