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.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #active?(params) ⇒ Boolean
- #apply(collection) ⇒ Object
- #count(collection) ⇒ Object
- #count? ⇒ Boolean
- #default? ⇒ Boolean
- #group ⇒ Object
-
#initialize(admin, name, options = {}, &block) ⇒ Scope
constructor
A new instance of Scope.
- #label ⇒ Object
- #to_param ⇒ Object
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, ={}, &block) @admin, @name, @options, @block = admin, name, , block 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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/trestle/scopes/scope.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/trestle/scopes/scope.rb', line 4 def @options end |
Instance Method Details
#active?(params) ⇒ Boolean
46 47 48 49 50 51 52 53 54 |
# File 'lib/trestle/scopes/scope.rb', line 46 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
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/trestle/scopes/scope.rb', line 30 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
42 43 44 |
# File 'lib/trestle/scopes/scope.rb', line 42 def count(collection) @admin.count(@admin.merge_scopes(collection, apply(collection))) end |
#count? ⇒ Boolean
26 27 28 |
# File 'lib/trestle/scopes/scope.rb', line 26 def count? @options[:count] != false end |
#default? ⇒ Boolean
22 23 24 |
# File 'lib/trestle/scopes/scope.rb', line 22 def default? @options[:default] == true end |
#group ⇒ Object
18 19 20 |
# File 'lib/trestle/scopes/scope.rb', line 18 def group @options[:group] end |
#label ⇒ Object
14 15 16 |
# File 'lib/trestle/scopes/scope.rb', line 14 def label @options[:label] || @admin.t("scopes.#{name}", default: name.to_s.humanize.titleize) end |
#to_param ⇒ Object
10 11 12 |
# File 'lib/trestle/scopes/scope.rb', line 10 def to_param name unless default? end |