Sha256: 255936bfdb4dae6c7f0378e98ebd288763c9a176bee364596e3114ebe3fa8198

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

module Trestle
  class Scopes
    class Scope
      attr_reader :name, :options, :block

      def initialize(admin, name, options={}, &block)
        @admin, @name, @options, @block = admin, name, options, block
      end

      def to_param
        name unless default?
      end

      def label
        @options[:label] || @admin.t("scopes.#{name}", default: name.to_s.humanize.titleize)
      end

      def group
        @options[:group]
      end

      def default?
        @options[:default] == true
      end

      def count?
        @options[:count] != false
      end

      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

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

      def active?(params)
        active_scopes = Array(params[:scope])

        if active_scopes.any?
          active_scopes.include?(to_param.to_s)
        else
          default?
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.10.0 lib/trestle/scopes/scope.rb
trestle-0.10.0.pre2 lib/trestle/scopes/scope.rb
trestle-0.10.0.pre lib/trestle/scopes/scope.rb