Skip to content

Commit

Permalink
Refactors the pagination behavior for index actions
Browse files Browse the repository at this point in the history
  • Loading branch information
harshs08 committed Dec 27, 2015
1 parent 668703a commit bbdcb59
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
6 changes: 1 addition & 5 deletions app/controllers/api/v1/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ class Api::V1::OrdersController < ApplicationController

def index
orders = current_user.orders.page(params[:page]).per(params[:per_page])
render json: orders, meta: { pagination: {
per_page: params[:per_page],
total_pages: orders.total_pages,
total_objects: orders.total_count
} }
render json: orders, meta: pagination(orders, params[:per_page])
end

def show
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/api/v1/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ def show

def index
products = Product.search(params).page(params[:page]).per(params[:per_page])
render json: products, meta: { pagination: {
per_page: params[:per_page],
total_pages: products.total_pages,
total_objects: products.total_count
}
}
render json: products, meta: pagination(products, params[:per_page])
end

def create
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session

include Authenticable

private
def pagination(paginated_array, per_page)
{ pagination: { per_page: per_page.to_i,
total_pages: paginated_array.total_pages,
total_objects: paginated_array.total_count
} }
end
end
6 changes: 1 addition & 5 deletions spec/controllers/api/v1/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
expect(order_response.size).to eql(4)
end

it { expect(json_response).to have_key(:meta) }
it { expect(json_response[:meta]).to have_key(:pagination) }
it { expect(json_response[:meta][:pagination]).to have_key(:per_page)}
it { expect(json_response[:meta][:pagination]).to have_key(:total_pages) }
it { expect(json_response[:meta][:pagination]).to have_key(:total_objects) }
it_behaves_like "paginated list"

it { is_expected.to respond_with 200 }
end
Expand Down
8 changes: 2 additions & 6 deletions spec/controllers/api/v1/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@
end
end

it { expect(json_response).to have_key(:meta) }
it { expect(json_response[:meta]).to have_key(:pagination) }
it { expect(json_response[:meta][:pagination]).to have_key(:per_page) }
it { expect(json_response[:meta][:pagination]).to have_key(:total_pages) }
it { expect(json_response[:meta][:pagination]).to have_key(:total_objects) }

it_behaves_like "paginated list"

it { is_expected.to respond_with 200 }

context "when product_ids parameter is sent" do
Expand Down
7 changes: 7 additions & 0 deletions spec/support/shared_examples/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
shared_examples "paginated list" do
it { expect(json_response).to have_key(:meta) }
it { expect(json_response[:meta]).to have_key(:pagination) }
it { expect(json_response[:meta][:pagination]).to have_key(:per_page)}
it { expect(json_response[:meta][:pagination]).to have_key(:total_pages) }
it { expect(json_response[:meta][:pagination]).to have_key(:total_objects) }
end

0 comments on commit bbdcb59

Please sign in to comment.