Module: V1::ApiController::Metadata
- Included in:
- V1::ApiController
- Defined in:
- app/controllers/v1/api_controller/metadata.rb
Overview
@apiDefine Pagination Pagination
@apiSuccess {Object} Metadata
@apiSuccess {Integer} .current_page Current page in pagination
@apiSuccess {Integer} .total_pages Total number of pages
@apiSuccess {Integer} .total_count Total number of elements
Instance Method Summary collapse
-
#meta_attributes(collection, extra_meta = {}) ⇒ Hash
Generate metadata information for request.
-
#meta_pagination(collection) ⇒ Hash
Generate metadata pagination for request.
Instance Method Details
#meta_attributes(collection, extra_meta = {}) ⇒ Hash
Generate metadata information for request.
20 21 22 23 24 25 |
# File 'app/controllers/v1/api_controller/metadata.rb', line 20 def (collection, = {}) = {} = .merge((collection)) if collection.class.name.include?('ActiveRecord') # Merge the metadata result with the extra metadata .merge() end |
#meta_pagination(collection) ⇒ Hash
Generate metadata pagination for request.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/controllers/v1/api_controller/metadata.rb', line 31 def (collection) next_page = previous_page = nil # Generate the link to the next page next_page = compile_page(URI(request.original_url), collection.next_page) if collection.total_pages > collection.current_page # Generate the link to the previous page previous_page = compile_page(URI(request.original_url), collection.prev_page) if collection.current_page > 1 && collection.current_page < collection.total_pages # Current page of the response # Total pages of the query # Total records of the query = { current_page: collection.current_page, total_pages: collection.total_pages, total_count: collection.total_count } # Next number page [:next_page] = collection.next_page if collection.next_page # Previous number page [:prev_page] = collection.prev_page if collection.prev_page # Link to the next page [:href_previous_page] = previous_page if previous_page # Link to the previous page [:href_next_page] = next_page if next_page end |