Class: InstantApi::Model::Collection
- Inherits:
-
Object
- Object
- InstantApi::Model::Collection
- Defined in:
- lib/instant_api/model/collection.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#query_builder ⇒ Object
readonly
Returns the value of attribute query_builder.
Instance Method Summary collapse
- #collection ⇒ Object
- #count ⇒ Object
-
#initialize(klass, params) ⇒ Collection
constructor
A new instance of Collection.
- #page ⇒ Object
- #paginated_collection ⇒ Object
- #per_page ⇒ Object
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
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/instant_api/model/collection.rb', line 5 def params @params end |
#query_builder ⇒ Object (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
#collection ⇒ Object
30 31 32 |
# File 'lib/instant_api/model/collection.rb', line 30 def collection @collection ||= query_builder.query(params) end |
#count ⇒ Object
26 27 28 |
# File 'lib/instant_api/model/collection.rb', line 26 def count collection.count end |
#page ⇒ Object
12 13 14 |
# File 'lib/instant_api/model/collection.rb', line 12 def page @page ||= [(params[:page] || 1).to_i, 1].max end |
#paginated_collection ⇒ Object
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_page ⇒ Object
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 |