lib/responsable.rb in responsable-1.0.0 vs lib/responsable.rb in responsable-1.4.0

- old
+ new

@@ -134,6 +134,42 @@ code: '100' } ] }, status: :no_content end + + # Method to render not implemented in a consistent way + # @param detail the description of the functionality that is not supported + def not_implemented(detail) + render json: { + result: [ + { + status: '501', + title: 'Not Implemented', + detail: detail, + code: '100' + } + ] + }, status: :not_implemented + end + + # Method to render paginated resources in a consistent way + # @param paginated_query the query that has been paginated with Kaminari + # @param serialized_data the serialized resource (should not be a hash `{data: ...}` to prevent double nesting) + def render_page(paginated_query, serialized_data) + if paginated_query.respond_to? :current_page + render json: { + data: serialized_data, + page: { + current_page: paginated_query.current_page, + next_page: paginated_query.next_page, + prev_page: paginated_query.prev_page, + total_count: paginated_query.total_count, + total_pages: paginated_query.total_pages, + has_next: !paginated_query.last_page? && paginated_query.size.positive? + } + } + else + not_implemented('a pagination gem that implements #current_page is needed') + end + end end