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, label: nil, group: nil, default: false, count: true, &block) ⇒ Scope

Returns a new instance of Scope.



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

def initialize(admin, name, label: nil, group: nil, default: false, count: true, &block)
  @admin, @name, @block = admin, name, block
  @label, @group, @default, @count = label, group, default, count
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

#groupObject (readonly)

Returns the value of attribute group.



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

def group
  @group
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#active?(params) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/trestle/scopes/scope.rb', line 44

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trestle/scopes/scope.rb', line 27

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



39
40
41
42
# File 'lib/trestle/scopes/scope.rb', line 39

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

#count?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/trestle/scopes/scope.rb', line 23

def count?
  @count
end

#default?Boolean

Returns:

  • (Boolean)


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

def default?
  @default
end

#labelObject



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

def label
  @label || default_label
end

#to_paramObject



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

def to_param
  name unless default?
end