[Fix #1865] Expose attachment field over API

This commit is contained in:
Frederic Merizen 2018-04-24 20:27:19 +02:00
parent fe0aafdd44
commit 5b9f25271f
3 changed files with 34 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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