Sha256: 0cb4750953b43e5798f39ea5f641b183ca0b1fcb8fa92e1c167995f919891807

Contents?: true

Size: 973 Bytes

Versions: 5

Compression:

Stored size: 973 Bytes

Contents

module Trestle
  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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trestle-0.8.11 lib/trestle/scope.rb
trestle-0.8.10 lib/trestle/scope.rb
trestle-0.8.9 lib/trestle/scope.rb
trestle-0.8.8 lib/trestle/scope.rb
trestle-0.8.7 lib/trestle/scope.rb