Class: InstantApi::Model::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/instant_api/model/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, params) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
# File 'lib/instant_api/model/collection.rb', line 7

def initialize(klass, params)
  @params = params
  @query_builder = InstantApi::Model::ActiveRecordQueryBuilder.new(klass)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/instant_api/model/collection.rb', line 5

def params
  @params
end

#query_builderObject (readonly)

Returns the value of attribute query_builder.



5
6
7
# File 'lib/instant_api/model/collection.rb', line 5

def query_builder
  @query_builder
end

Instance Method Details

#collectionObject



30
31
32
# File 'lib/instant_api/model/collection.rb', line 30

def collection
  @collection ||= query_builder.query(params)
end

#countObject



26
27
28
# File 'lib/instant_api/model/collection.rb', line 26

def count
  collection.count
end

#pageObject



12
13
14
# File 'lib/instant_api/model/collection.rb', line 12

def page
  @page ||= [(params[:page] || 1).to_i, 1].max
end

#paginated_collectionObject



34
35
36
37
38
39
40
41
# File 'lib/instant_api/model/collection.rb', line 34

def paginated_collection
  collection_to_paginate = if collection.is_a?(Array)
                             Kaminari::paginate_array(collection)
                           else
                             collection
                           end
  collection_to_paginate.page(page).per(per_page)
end

#per_pageObject



16
17
18
19
20
21
22
23
24
# File 'lib/instant_api/model/collection.rb', line 16

def per_page
  @per_page ||= begin
    aux = (params[:per_page] || 10).to_i
    if aux < 1
      aux = 10
    end
    [aux, 10].min
  end
end