Sha256: 97d7160c8d2ad1e598053e8094a7d06a56c5be94959b03428870a2163b3410c2

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

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

      def initialize(admin)
        @admin = admin
      end

      def prepare(params)
        collection = initialize_collection(params)
        collection = apply_scopes(collection, params)
        collection = apply_sorting(collection, params)
        collection = paginate(collection, params)
        collection = finalize_collection(collection)
        collection = decorate_collection(collection)
        collection
      end

    private
      def apply_scopes(collection, params)
        unscoped = initialize_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

4 entries across 4 versions & 1 rubygems

Version Path
trestle-0.8.10 lib/trestle/resource/collection.rb
trestle-0.8.9 lib/trestle/resource/collection.rb
trestle-0.8.8 lib/trestle/resource/collection.rb
trestle-0.8.7 lib/trestle/resource/collection.rb