From 5b9f25271fc1d9b8e109aa36bcd6a6a993f34834 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Tue, 24 Apr 2018 20:27:19 +0200 Subject: [PATCH] [Fix #1865] Expose attachment field over API --- app/serializers/champ_serializer.rb | 10 ++++++++++ config/environments/test.rb | 2 ++ spec/serializers/champ_serializer_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 spec/serializers/champ_serializer_spec.rb diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb index 981c2b9cc..454d3b944 100644 --- a/app/serializers/champ_serializer.rb +++ b/app/serializers/champ_serializer.rb @@ -1,5 +1,15 @@ class ChampSerializer < ActiveModel::Serializer + include Rails.application.routes.url_helpers + attributes :value has_one :type_de_champ + + def value + if object.piece_justificative_file.attached? + url_for(object.piece_justificative_file) + else + object.value + end + end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 9de8d2433..380b51502 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -31,6 +31,8 @@ Rails.application.configure do # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test + config.active_storage.service = :local + # Randomize the order test cases are executed. config.active_support.test_order = :random diff --git a/spec/serializers/champ_serializer_spec.rb b/spec/serializers/champ_serializer_spec.rb new file mode 100644 index 000000000..96b8ce784 --- /dev/null +++ b/spec/serializers/champ_serializer_spec.rb @@ -0,0 +1,22 @@ +describe ChampSerializer do + describe '#attributes' do + subject { ChampSerializer.new(champ).serializable_hash } + + context 'when type champ is piece justificative' do + include Rails.application.routes.url_helpers + + let(:champ) { create(:champ, type_de_champ: create(:type_de_champ_piece_justificative)) } + + before { champ.piece_justificative_file.attach({ filename: __FILE__, io: File.open(__FILE__) }) } + after { champ.piece_justificative_file.purge } + + it { is_expected.to include(value: url_for(champ.piece_justificative_file)) } + end + + context 'when type champ is not piece justificative' do + let(:champ) { create(:champ, value: "blah") } + + it { is_expected.to include(value: "blah") } + end + end +end