lib/trestle/resource/collection.rb in trestle-0.8.10 vs lib/trestle/resource/collection.rb in trestle-0.8.11
- old
+ new
@@ -1,27 +1,47 @@
module Trestle
class Resource
class Collection
- delegate :initialize_collection, :paginate, :finalize_collection, :decorate_collection,
+ delegate :collection, :paginate, :finalize_collection, :decorate_collection,
:scopes, :merge_scopes, :column_sorts, :sort, to: :@admin
- def initialize(admin)
- @admin = admin
+ def initialize(admin, options={})
+ @admin, @options = admin, options
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 = 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 = initialize_collection(params)
+ unscoped = collection(params)
active_scopes(params).reduce(collection) do |collection, scope|
merge_scopes(collection, scope.apply(unscoped))
end
end