Class: ActiveAdmin::Views::Scopes

Inherits:
Component show all
Defined in:
lib/active_admin/views/components/scopes.rb

Overview

Renders a collection of ActiveAdmin::Scope objects as a simple list with a seperator

Instance Method Summary collapse

Methods inherited from Component

#default_class_name, #initialize, #tag_name

Constructor Details

This class inherits a constructor from ActiveAdmin::Component

Instance Method Details

#build(scopes) ⇒ Object



9
10
11
12
13
# File 'lib/active_admin/views/components/scopes.rb', line 9

def build(scopes)
  scopes.each do |scope|
    build_scope(scope)
  end
end

#build_scope(scope) ⇒ Object (protected)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_admin/views/components/scopes.rb', line 17

def build_scope(scope)
  span :class => classes_for_scope(scope) do
    begin
      scope_name = I18n.t!("active_admin.scopes.#{scope.scope_method}")
    rescue I18n::MissingTranslationData
      scope_name = scope.name
    end

    if current_scope?(scope)
      em(scope_name)
    else
      a(scope_name, :href => url_for(params.merge(:scope => scope.id, :page => 1)))
    end
    text_node(" ")
    scope_count(scope)
    text_node(" ")
  end
end

#classes_for_scope(scope) ⇒ Object (protected)



36
37
38
39
40
# File 'lib/active_admin/views/components/scopes.rb', line 36

def classes_for_scope(scope)
  classes = ["scope", scope.id]
  classes << "selected" if current_scope?(scope)
  classes.join(" ")
end

#current_scope?(scope) ⇒ Boolean (protected)

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/active_admin/views/components/scopes.rb', line 42

def current_scope?(scope)
  if params[:scope]
    params[:scope] == scope.id
  else
    active_admin_config.default_scope == scope
  end
end

#get_scope_count(scope) ⇒ Object (protected)



56
57
58
59
60
61
62
# File 'lib/active_admin/views/components/scopes.rb', line 56

def get_scope_count(scope)
  if scope.scope_method
    scoping_class.send(scope.scope_method).count
  else
    instance_exec(scoping_class, &scope.scope_block).count
  end
end

#scope_count(scope) ⇒ Object (protected)



50
51
52
53
54
# File 'lib/active_admin/views/components/scopes.rb', line 50

def scope_count(scope)
  span :class => 'count' do
    "(" + get_scope_count(scope).to_s + ")"
  end
end

#scoping_classObject (protected)



64
65
66
# File 'lib/active_admin/views/components/scopes.rb', line 64

def scoping_class
  assigns["before_scope_collection"] || active_admin_config.resource
end