lib/trestle/scopes/scope.rb in trestle-0.10.0 vs lib/trestle/scopes/scope.rb in trestle-0.10.1

- old
+ new

@@ -1,32 +1,29 @@ module Trestle class Scopes class Scope - attr_reader :name, :options, :block + attr_reader :name, :group, :block - def initialize(admin, name, options={}, &block) - @admin, @name, @options, @block = admin, name, options, block + 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 def to_param name unless default? end def label - @options[:label] || @admin.t("scopes.#{name}", default: name.to_s.humanize.titleize) + @label || default_label end - def group - @options[:group] - end - def default? - @options[:default] == true + @default end def count? - @options[:count] != false + @count end def apply(collection) if @block if @block.arity == 1 @@ -38,10 +35,11 @@ collection.public_send(name) end end def count(collection) + return unless count? @admin.count(@admin.merge_scopes(collection, apply(collection))) end def active?(params) active_scopes = Array(params[:scope]) @@ -49,9 +47,14 @@ if active_scopes.any? active_scopes.include?(to_param.to_s) else default? end + end + + protected + def default_label + @admin.t("scopes.#{name}", default: name.to_s.humanize.titleize) end end end end