Class: Trestle::Scope
- Inherits:
-
Object
- Object
- Trestle::Scope
- Defined in:
- lib/trestle/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
- #default? ⇒ Boolean
-
#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.
5 6 7 |
# File 'lib/trestle/scope.rb', line 5 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.
3 4 5 |
# File 'lib/trestle/scope.rb', line 3 def block @block end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/trestle/scope.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/trestle/scope.rb', line 3 def @options end |
Instance Method Details
#active?(params) ⇒ Boolean
37 38 39 40 41 42 43 44 45 |
# File 'lib/trestle/scope.rb', line 37 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
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/trestle/scope.rb', line 21 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
33 34 35 |
# File 'lib/trestle/scope.rb', line 33 def count(collection) @admin.count(@admin.merge_scopes(collection, apply(collection))) end |
#default? ⇒ Boolean
17 18 19 |
# File 'lib/trestle/scope.rb', line 17 def default? @options[:default] == true end |
#label ⇒ Object
13 14 15 |
# File 'lib/trestle/scope.rb', line 13 def label @options[:label] || I18n.t("admin.scopes.#{name}", default: name.to_s.humanize.titleize) end |
#to_param ⇒ Object
9 10 11 |
# File 'lib/trestle/scope.rb', line 9 def to_param name end |