Merge pull request #1867 from betagouv/frederic/fix_1865_api_expose_pj_champ

[Fix #1865] Expose attachment field over API
This commit is contained in:
Paul Chavard 2018-04-25 15:42:12 +02:00 committed by GitHub
commit 5fd1c28007
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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