Sha256: 2f9b8cc9ff3243d484afd7b6f00d64dfa482c80e2e3b50d7b96de4d0f51be351

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 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
      end

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

      def default?
        @options[:default] == true
      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

6 entries across 6 versions & 1 rubygems

Version Path
trestle-0.9.3 lib/trestle/scopes/scope.rb
trestle-0.9.2 lib/trestle/scopes/scope.rb
trestle-0.9.1 lib/trestle/scopes/scope.rb
trestle-0.9.0 lib/trestle/scopes/scope.rb
trestle-0.8.13 lib/trestle/scopes/scope.rb
trestle-0.8.12 lib/trestle/scopes/scope.rb