Class: Geckorate::Decorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/geckorate.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decorate_collection(collection, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/geckorate.rb', line 8

def decorate_collection(collection, options = {})
  return [] if collection.empty?

  klass           = options.fetch(:class_name, collection.first.class)
  full_klass_name = klass.to_s.concat('Decorator')
  decorator_klass = Class.const_get(full_klass_name)

  collection.map do |item|
    decorator_klass.new(item).decorate(options)
  end
end

.decorate_kaminari_collection(collection, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/geckorate.rb', line 20

def decorate_kaminari_collection(collection, options = {})
  {
    page: collection.current_page,
    per_page: collection.current_per_page,
    total: collection.total_count,
    records: decorate_collection(collection, options)
  }
end

.decorate_will_paginate_collection(collectionf, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/geckorate.rb', line 29

def decorate_will_paginate_collection(collectionf, options = {})
  {
    page: collection.current_page,
    per_page: collection.per_page,
    total: collection.total_entries,
    records: decorate_collection(collection, options)
  }
end

Instance Method Details

#decorate(options = {}) ⇒ Object



5
# File 'lib/geckorate.rb', line 5

def decorate(options = {}); end