Class: ActiveAdmin::Views::IndexAsSortable

Inherits:
Component
  • Object
show all
Defined in:
lib/active_admin/views/index_as_sortable.rb

Overview

Index as a Sortable List or Tree

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.index_nameObject



27
# File 'lib/active_admin/views/index_as_sortable.rb', line 27

def self.index_name; "sortable"; end

Instance Method Details

#actions(options = {}, &block) ⇒ Object

Adds links to View, Edit and Delete



74
75
76
77
78
# File 'lib/active_admin/views/index_as_sortable.rb', line 74

def actions(options = {}, &block)
  options = { :defaults => true }.merge(options)
  @default_actions = options[:defaults]
  @other_actions = block
end

#build(page_presenter, collection) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_admin/views/index_as_sortable.rb', line 6

def build(page_presenter, collection)
  @page_presenter = page_presenter
  @collection = if tree?
                  roots
                else
                  collection
                end
  @collection = @collection.sort_by do |a|
    a.send(options[:sorting_attribute]) || 1
  end

  @resource_name = ActiveAdmin::SortableTree::Compatibility.normalized_resource_name(active_admin_config.resource_name)

  # Call the block passed in. This will set the
  # title and body methods
  instance_eval &page_presenter.block if page_presenter.block

  add_class "index"
  build_list
end

#default_roots_collectionObject

Find the roots by calling the roots method directly on the resource. This effectively performs:

TreeNode.roots # => [#<TreeNode id:1>, ... ]

Returns collection of roots.



43
44
45
# File 'lib/active_admin/views/index_as_sortable.rb', line 43

def default_roots_collection
  resource_class.send(options[:roots_method])
end

#label(method = nil, &block) ⇒ Object

Setter method for the configuration of the label



66
67
68
69
70
71
# File 'lib/active_admin/views/index_as_sortable.rb', line 66

def label(method = nil, &block)
  if block_given? || method
    @label = block_given? ? block : method
  end
  @label
end

#optionsObject



29
30
31
# File 'lib/active_admin/views/index_as_sortable.rb', line 29

def options
  active_admin_config.dsl.sortable_options
end

#rootsObject



33
34
35
# File 'lib/active_admin/views/index_as_sortable.rb', line 33

def roots
  roots_collection || default_roots_collection
end

#roots_collectionObject

Use user-defined logic to find the root nodes. This executes a callable object within the context of the resource’s controller.

Example

options[:roots_collection] = proc { current_user.tree_nodes.roots }

Returns collection of roots.



55
56
57
58
59
# File 'lib/active_admin/views/index_as_sortable.rb', line 55

def roots_collection
  if (callable = options[:roots_collection])
    controller.instance_exec(&callable)
  end
end

#tree?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/active_admin/views/index_as_sortable.rb', line 61

def tree?
  !!options[:tree]
end