parametrize pagination on dossier index api

This commit is contained in:
Tanguy PATTE 2016-01-21 11:41:24 +01:00
parent df7ac75483
commit 514918b2b8
3 changed files with 10 additions and 5 deletions

View file

@ -2,8 +2,8 @@ class API::V1::DossiersController < APIController
def index def index
procedure = current_administrateur.procedures.find(params[:procedure_id]) procedure = current_administrateur.procedures.find(params[:procedure_id])
dossiers = procedure.dossiers.paginate(page: params[:page]).decorate dossiers = procedure.dossiers.paginate(page: params[:page])
render json: dossiers, meta: pagination(dossiers), meta_key: 'pagination', status: 200 render json: dossiers, each_serializer: DossiersSerializer, meta: pagination(dossiers), meta_key: 'pagination', status: 200
rescue ActiveRecord::RecordNotFound => e rescue ActiveRecord::RecordNotFound => e
render json: {}, status: 404 render json: {}, status: 404
end end

View file

@ -0,0 +1,5 @@
class DossiersSerializer < ActiveModel::Serializer
attributes :id,
:nom_projet,
:updated_at
end

View file

@ -24,7 +24,7 @@ describe API::V1::DossiersController do
context 'when procedure is found and belongs to admin' do context 'when procedure is found and belongs to admin' do
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
let(:date_creation) { Time.local(2008, 9, 1, 10, 5, 0) } let(:date_creation) { Time.local(2008, 9, 1, 10, 5, 0) }
let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, :with_user, procedure: procedure) } } let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, procedure: procedure) } }
let(:body) { JSON.parse(response.body, symbolize_names: true) } let(:body) { JSON.parse(response.body, symbolize_names: true) }
it { expect(response.code).to eq('200') } it { expect(response.code).to eq('200') }
it { expect(body).to have_key :pagination } it { expect(body).to have_key :pagination }
@ -54,8 +54,8 @@ describe API::V1::DossiersController do
context 'when there are multiple pages' do context 'when there are multiple pages' do
let(:response) { get :index, token: admin.api_token, procedure_id: procedure_id, page: 2 } let(:response) { get :index, token: admin.api_token, procedure_id: procedure_id, page: 2 }
let!(:dossier1) { create(:dossier, :with_entreprise, :with_user, procedure: procedure) } let!(:dossier1) { create(:dossier, :with_entreprise, procedure: procedure) }
let!(:dossier2) { create(:dossier, :with_entreprise, :with_user, procedure: procedure) } let!(:dossier2) { create(:dossier, :with_entreprise, procedure: procedure) }
before do before do
allow(Dossier).to receive(:per_page).and_return(1) allow(Dossier).to receive(:per_page).and_return(1)
end end