Class: Trestle::Scopes::Scope
- Inherits:
-
Object
- Object
- Trestle::Scopes::Scope
- Defined in:
- lib/trestle/scopes/scope.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #active?(params) ⇒ Boolean
- #apply(collection) ⇒ Object
- #count(collection) ⇒ Object
- #count? ⇒ Boolean
- #default? ⇒ Boolean
-
#initialize(admin, name, label: nil, group: nil, default: false, count: true, &block) ⇒ Scope
constructor
A new instance of Scope.
- #label ⇒ Object
- #to_param ⇒ Object
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
#block ⇒ Object (readonly)
Returns the value of attribute block.
4 5 6 |
# File 'lib/trestle/scopes/scope.rb', line 4 def block @block end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
4 5 6 |
# File 'lib/trestle/scopes/scope.rb', line 4 def group @group end |
#name ⇒ Object (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
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
23 24 25 |
# File 'lib/trestle/scopes/scope.rb', line 23 def count? @count end |
#default? ⇒ Boolean
19 20 21 |
# File 'lib/trestle/scopes/scope.rb', line 19 def default? @default end |
#label ⇒ Object
15 16 17 |
# File 'lib/trestle/scopes/scope.rb', line 15 def label @label || default_label end |
#to_param ⇒ Object
11 12 13 |
# File 'lib/trestle/scopes/scope.rb', line 11 def to_param name unless default? end |