Class: ActiveAdmin::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, method = nil, options = {}, &block) ⇒ Scope

Create a Scope

Examples:

Scope.new(:published)
# => Scope with name 'Published' and scope method :published

Scope.new('Published', :public)
# => Scope with name 'Published' and scope method :public

Scope.new(:published, show_count: :async)
# => Scope with name 'Published' that queries its count asynchronously

Scope.new(:published, show_count: false)
# => Scope with name 'Published' that does not display a count

Scope.new 'Published', :public, if: proc { current_admin_user.can? :manage, resource_class } do |articles|
  articles.where published: true
end
# => Scope with name 'Published' and scope method :public, optionally displaying the scope per the :if block

Scope.new('Published') { |articles| articles.where(published: true) }
# => Scope with name 'Published' using a block to scope

Scope.new ->{Date.today.strftime '%A'}, :published_today
# => Scope with dynamic title using the :published_today scope method

Scope.new :published, nil, group: :status
# => Scope with the group :status


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_admin/scope.rb', line 37

def initialize(name, method = nil, options = {}, &block)
  @name = name
  @scope_method = method.try(:to_sym)

  if name.is_a? Proc
    raise "A string/symbol is required as the second argument if your label is a proc." unless method
    @id = method.to_s.parameterize(separator: "_")
  else
    @scope_method ||= name.to_sym
    @id = name.to_s.parameterize(separator: "_")
  end

  @scope_method = nil if @scope_method == :all
  if block
    @scope_method = nil
    @scope_block = block
  end

  @localizer = options[:localizer]
  @show_count = options.fetch(:show_count, true)
  @display_if_block = options[:if] || proc { true }
  @default_block = options[:default] || proc { false }
  @group = options[:group].try(:to_sym)
end

Instance Attribute Details

#default_blockObject (readonly)

Returns the value of attribute default_block.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def default_block
  @default_block
end

#display_if_blockObject (readonly)

Returns the value of attribute display_if_block.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def display_if_block
  @display_if_block
end

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def id
  @id
end

#scope_blockObject (readonly)

Returns the value of attribute scope_block.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def scope_block
  @scope_block
end

#scope_methodObject (readonly)

Returns the value of attribute scope_method.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def scope_method
  @scope_method
end

#show_countObject (readonly)

Returns the value of attribute show_count.



5
6
7
# File 'lib/active_admin/scope.rb', line 5

def show_count
  @show_count
end

Instance Method Details

#async_count?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_admin/scope.rb', line 70

def async_count?
  @show_count == :async
end

#nameObject



62
63
64
65
66
67
68
# File 'lib/active_admin/scope.rb', line 62

def name
  case @name
  when String then @name
  when Symbol then @localizer ? @localizer.t(@name, scope: "scopes") : @name.to_s.titleize
  else @name
  end
end