Sha256: 0602417f1255cd2353ce45bee4d0c003b7d7b70097effba5beb8327d32f6c948

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

module Trestle
  class Resource
    class Collection
      delegate :collection, :paginate, :finalize_collection, :decorate_collection,
               :scopes, :merge_scopes, :column_sorts, :sort, to: :@admin

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

      def prepare(params)
        collection = collection(params)
        collection = apply_scopes(collection, params)  if scope?
        collection = apply_sorting(collection, params) if sort?
        collection = paginate(collection, params)      if paginate?
        collection = finalize_collection(collection)   if finalize?
        collection = decorate_collection(collection)   if decorate?
        collection
      end

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

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

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

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

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

    private
      def apply_scopes(collection, params)
        unscoped = collection(params)

        active_scopes(params).reduce(collection) do |collection, scope|
          merge_scopes(collection, scope.apply(unscoped))
        end
      end

      def active_scopes(params)
        scopes.values.select { |s| s.active?(params) }
      end

      def apply_sorting(collection, params)
        return collection unless params[:sort]

        field = params[:sort].to_sym
        order = params[:order].to_s.downcase == "desc" ? :desc : :asc

        if column_sorts.has_key?(field)
          @admin.instance_exec(collection, order, &column_sorts[field])
        else
          sort(collection, field, order)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
trestle-0.9.4 lib/trestle/resource/collection.rb
trestle-0.9.3 lib/trestle/resource/collection.rb
trestle-0.9.2 lib/trestle/resource/collection.rb
trestle-0.9.1 lib/trestle/resource/collection.rb
trestle-0.9.0 lib/trestle/resource/collection.rb
trestle-0.8.13 lib/trestle/resource/collection.rb
trestle-0.8.12 lib/trestle/resource/collection.rb
trestle-0.8.11 lib/trestle/resource/collection.rb