From bd5a39ff17cddb6e7374bebf0ed85b564b4612d0 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Fri, 19 Feb 2016 16:59:18 +0100 Subject: [PATCH] Add CSV output for Dossier API --- Gemfile | 2 ++ Gemfile.lock | 5 +++ app/controllers/api/v1/dossiers_controller.rb | 6 ++-- app/controllers/api_controller.rb | 5 +++ app/models/dossier.rb | 8 +++++ spec/models/dossier_spec.rb | 35 +++++++++++++++++++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index e7442aea7..0612d7aca 100644 --- a/Gemfile +++ b/Gemfile @@ -80,6 +80,8 @@ gem 'bootstrap-wysihtml5-rails', '~> 0.3.3.8' gem 'swagger-docs' gem 'grape-swagger-rails' +gem 'as_csv' + group :test do gem 'capybara' gem 'factory_girl' diff --git a/Gemfile.lock b/Gemfile.lock index 5355056ee..d2119251c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,6 +48,10 @@ GEM tzinfo (~> 1.1) addressable (2.3.8) arel (6.0.2) + as_csv (2.0.2) + actionpack (>= 3.0) + activemodel (>= 3.0) + responders ast (2.0.0) astrolabe (1.3.0) parser (>= 2.2.0.pre.3, < 3.0) @@ -491,6 +495,7 @@ PLATFORMS DEPENDENCIES active_model_serializers + as_csv bootstrap-datepicker-rails bootstrap-sass (~> 3.3.5) bootstrap-wysihtml5-rails (~> 0.3.3.8) diff --git a/app/controllers/api/v1/dossiers_controller.rb b/app/controllers/api/v1/dossiers_controller.rb index 274212105..3c6c432fb 100644 --- a/app/controllers/api/v1/dossiers_controller.rb +++ b/app/controllers/api/v1/dossiers_controller.rb @@ -1,5 +1,4 @@ class API::V1::DossiersController < APIController - swagger_controller :dossiers, "Dossiers" swagger_api :index do @@ -32,7 +31,10 @@ class API::V1::DossiersController < APIController def show procedure = current_administrateur.procedures.find(params[:procedure_id]) dossier = procedure.dossiers.find(params[:id]) - render json: dossier, status: 200 + respond_to do |format| + format.json { render json: dossier, status: 200 } + format.csv { render csv: dossier, status: 200 } + end rescue ActiveRecord::RecordNotFound => e render json: {}, status: 404 end diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 574216a48..bf471b3ba 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -1,5 +1,6 @@ class APIController < ApplicationController before_action :authenticate_user + before_filter :default_format_json def authenticate_user render json: {}, status: 401 unless valid_token? @@ -14,4 +15,8 @@ class APIController < ApplicationController def current_administrateur @administrateur ||= Administrateur.find_by_api_token(params[:token]) end + + def default_format_json + request.format = "json" unless request.accept + end end \ No newline at end of file diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 9f211771f..eed631711 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1,4 +1,5 @@ class Dossier < ActiveRecord::Base + enum state: {draft: 'draft', initiated: 'initiated', replied: 'replied', @@ -192,6 +193,13 @@ class Dossier < ActiveRecord::Base procedure.cerfa_flag? && !cerfa.empty? end + def as_csv(options={}) + dossier_attr = DossierSerializer.new(self).attributes + etablissement_attr = EtablissementSerializer.new(self.etablissement).attributes.map {|k, v| ["etablissement.#{k}", v] }.to_h + entreprise_attr = EntrepriseSerializer.new(self.entreprise).attributes.map {|k, v| ["entreprise.#{k}", v] }.to_h + dossier_attr.merge(etablissement_attr).merge(entreprise_attr) + end + private def build_default_cerfa diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 60342f009..072c57286 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -543,4 +543,39 @@ describe Dossier do it { expect(dossier.cerfa_available?).to be_falsey } end end + + describe '#as_csv?' do + let(:procedure) { create(:procedure) } + let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } + subject { dossier.as_csv } + + it { expect(subject[:nom_projet]).to eq("Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger") } + it { expect(subject[:description]).to eq("Ma super description") } + it { expect(subject[:archived]).to be_falsey } + it { expect(subject['etablissement.siret']).to eq('44011762001530') } + it { expect(subject['etablissement.siege_social']).to be_truthy } + it { expect(subject['etablissement.naf']).to eq('4950Z') } + it { expect(subject['etablissement.libelle_naf']).to eq('Transports par conduites') } + it { expect(subject['etablissement.adresse']).to eq("GRTGAZ\r IMMEUBLE BORA\r 6 RUE RAOUL NORDLING\r 92270 BOIS COLOMBES\r") } + it { expect(subject['etablissement.numero_voie']).to eq('6') } + it { expect(subject['etablissement.type_voie']).to eq('RUE') } + it { expect(subject['etablissement.nom_voie']).to eq('RAOUL NORDLING') } + it { expect(subject['etablissement.complement_adresse']).to eq('IMMEUBLE BORA') } + it { expect(subject['etablissement.code_postal']).to eq('92270') } + it { expect(subject['etablissement.localite']).to eq('BOIS COLOMBES') } + it { expect(subject['etablissement.code_insee_localite']).to eq('92009') } + it { expect(subject['entreprise.siren']).to eq('440117620') } + it { expect(subject['entreprise.capital_social']).to eq(537100000) } + it { expect(subject['entreprise.numero_tva_intracommunautaire']).to eq('FR27440117620') } + it { expect(subject['entreprise.forme_juridique']).to eq("SA à conseil d'administration (s.a.i.)") } + it { expect(subject['entreprise.forme_juridique_code']).to eq('5599') } + it { expect(subject['entreprise.nom_commercial']).to eq('GRTGAZ') } + it { expect(subject['entreprise.raison_sociale']).to eq('GRTGAZ') } + it { expect(subject['entreprise.siret_siege_social']).to eq('44011762001530') } + it { expect(subject['entreprise.code_effectif_entreprise']).to eq('51') } + it { expect(subject['entreprise.date_creation']).to eq('Thu, 28 Jan 2016 10:16:29 UTC +00:0') } + it { expect(subject['entreprise.nom']).to be_nil } + it { expect(subject['entreprise.prenom']).to be_nil } + end + end