Module: RocketPants::Respondable

Extended by:
ActiveSupport::Concern
Defined in:
lib/rocket_pants/controller/respondable.rb

Constant Summary collapse

RENDERING_OPTIONS =
[:status, :content_type]

Class Method Summary collapse

Class Method Details

.extract_pagination(collection) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rocket_pants/controller/respondable.rb', line 19

def self.extract_pagination(collection)
  case pagination_type(collection)
  when :will_paginate
    {
      :previous => collection.previous_page.try(:to_i),
      :next     => collection.next_page.try(:to_i),
      :current  => collection.current_page.try(:to_i),
      :per_page => collection.per_page.try(:to_i),
      :count    => collection.total_entries.try(:to_i),
      :pages    => collection.total_pages.try(:to_i)
    }
  when :kaminari
    current, total, per_page = collection.current_page, collection.num_pages, collection.limit_value
    {
      :current  => current,
      :previous => (current > 1 ? (current - 1) : nil),
      :next     => (current == total ? nil : (current + 1)),
      :per_page => per_page,
      :pages    => total,
      :count    => collection.total_count
    }
  end
end

.normalise_object(object, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rocket_pants/controller/respondable.rb', line 43

def self.normalise_object(object, options = {})
  # Convert the object using a standard grape-like lookup chain.
  if object.is_a?(Array) || object.is_a?(Set)
    object.map { |o| normalise_object o, options }
  elsif object.respond_to?(:serializable_hash)
    object.serializable_hash options
  elsif object.respond_to?(:as_json)
    object.as_json options
  else
    object
  end
end

.paginated?(object) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/rocket_pants/controller/respondable.rb', line 15

def self.paginated?(object)
  !pagination_type(object).nil?
end

.pagination_type(object) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/rocket_pants/controller/respondable.rb', line 5

def self.pagination_type(object)
  if object.respond_to?(:total_entries)
    :will_paginate
  elsif object.respond_to?(:num_pages) && object.respond_to?(:current_page)
    :kaminari
  else
    nil
  end
end