Class: Trestle::Scopes::Scope

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Scope.


6
7
8
# File 'lib/trestle/scopes/scope.rb', line 6

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block


4
5
6
# File 'lib/trestle/scopes/scope.rb', line 4

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name


4
5
6
# File 'lib/trestle/scopes/scope.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


4
5
6
# File 'lib/trestle/scopes/scope.rb', line 4

def options
  @options
end

Instance Method Details

#active?(params) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
41
42
43
44
45
46
# File 'lib/trestle/scopes/scope.rb', line 38

def active?(params)
  active_scopes = Array(params[:scope])

  if active_scopes.any?
    active_scopes.include?(to_param.to_s)
  else
    default?
  end
end

#apply(collection) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/trestle/scopes/scope.rb', line 22

def apply(collection)
  if @block
    if @block.arity == 1
      @admin.instance_exec(collection, &@block)
    else
      @admin.instance_exec(&@block)
    end
  else
    collection.public_send(name)
  end
end

#count(collection) ⇒ Object


34
35
36
# File 'lib/trestle/scopes/scope.rb', line 34

def count(collection)
  @admin.count(@admin.merge_scopes(collection, apply(collection)))
end

#default?Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/trestle/scopes/scope.rb', line 18

def default?
  @options[:default] == true
end

#labelObject


14
15
16
# File 'lib/trestle/scopes/scope.rb', line 14

def label
  @options[:label] || I18n.t("admin.scopes.#{name}", default: name.to_s.humanize.titleize)
end

#to_paramObject


10
11
12
# File 'lib/trestle/scopes/scope.rb', line 10

def to_param
  name
end