add attachment count attribute to template factory

pull/440/head
Alex Turchyn 9 months ago committed by Pete Matsyburka
parent fc4b4de6ed
commit 8d953f210f

@ -9,6 +9,7 @@ FactoryBot.define do
transient do transient do
submitter_count { 1 } submitter_count { 1 }
attachment_count { 1 }
only_field_types do only_field_types do
%w[text date checkbox radio signature number multiple select initials image file stamp cells phone payment] %w[text date checkbox radio signature number multiple select initials image file stamp cells phone payment]
end end
@ -16,20 +17,6 @@ FactoryBot.define do
end end
after(:create) do |template, ev| after(:create) do |template, ev|
blob = ActiveStorage::Blob.create_and_upload!(
io: Rails.root.join('spec/fixtures/sample-document.pdf').open,
filename: 'sample-document.pdf',
content_type: 'application/pdf'
)
attachment = ActiveStorage::Attachment.create!(
blob:,
name: :documents,
record: template
)
Templates::ProcessDocument.call(attachment, attachment.download)
template.schema = [{ attachment_uuid: attachment.uuid, name: 'sample-document' }]
number_words = %w[first second third fourth fifth sixth seventh eighth ninth tenth] number_words = %w[first second third fourth fifth sixth seventh eighth ninth tenth]
template.submitters = Array.new(ev.submitter_count) do |i| template.submitters = Array.new(ev.submitter_count) do |i|
@ -39,296 +26,319 @@ FactoryBot.define do
} }
end end
template.fields = template.submitters.reduce([]) do |fields, submitter| ev.attachment_count.times do |i|
fields += [ attachment_index = i + 1 if i > 0
{ field_index = "(#{attachment_index})" if attachment_index
'uuid' => SecureRandom.uuid,
'submitter_uuid' => submitter['uuid'], blob = ActiveStorage::Blob.create_and_upload!(
'name' => 'First Name', io: Rails.root.join('spec/fixtures/sample-document.pdf').open,
'type' => 'text', filename: 'sample-document.pdf',
'required' => true, content_type: 'application/pdf'
'preferences' => {}, )
'areas' => [ attachment = ActiveStorage::Attachment.create!(
{ blob:,
'x' => 0.09273546006944444, name: :documents,
'y' => 0.1099851117387033, record: template
'w' => 0.2701497395833333, )
'h' => 0.0372705365913556,
'attachment_uuid' => attachment.uuid, Templates::ProcessDocument.call(attachment, attachment.download)
'page' => 0
} template.schema << {
] attachment_uuid: attachment.uuid,
}, name: ['sample-document', attachment_index].compact.join('-')
{ }
'uuid' => SecureRandom.uuid,
'submitter_uuid' => submitter['uuid'], template.fields += template.submitters.reduce([]) do |fields, submitter|
'name' => 'Birthday', fields += [
'type' => 'date', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => { 'format' => 'DD/MM/YYYY' }, 'submitter_uuid' => submitter['uuid'],
'areas' => [ 'name' => ['First Name', field_index].compact.join(' '),
{ 'type' => 'text',
'x' => 0.09166666666666666, 'required' => true,
'y' => 0.1762778204144282, 'preferences' => {},
'w' => 0.2763888888888889, 'areas' => [
'h' => 0.0359029261474578, {
'attachment_uuid' => attachment.uuid, 'x' => 0.09273546006944444,
'page' => 0 'y' => 0.1099851117387033,
} 'w' => 0.2701497395833333,
] 'h' => 0.0372705365913556,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'Do you agree?', },
'type' => 'checkbox', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'areas' => [ 'name' => ['Birthday', field_index].compact.join(' '),
{ 'type' => 'date',
'x' => 0.09051106770833334, 'required' => true,
'y' => 0.227587027259332, 'preferences' => { 'format' => 'DD/MM/YYYY' },
'w' => 0.2784450954861111, 'areas' => [
'h' => 0.04113074042239687, {
'attachment_uuid' => attachment.uuid, 'x' => 0.09166666666666666,
'page' => 0 'y' => 0.1762778204144282,
} 'w' => 0.2763888888888889,
] 'h' => 0.0359029261474578,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'First child', },
'type' => 'radio', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'options' => [ 'name' => ['Do you agree?', field_index].compact.join(' '),
{ 'value' => 'Girl', 'uuid' => SecureRandom.uuid }, 'type' => 'checkbox',
{ 'value' => 'Boy', 'uuid' => SecureRandom.uuid } 'required' => true,
], 'preferences' => {},
'areas' => [ 'areas' => [
{ {
'x' => 0.09027777777777778, 'x' => 0.09051106770833334,
'y' => 0.3020184190330008, 'y' => 0.227587027259332,
'w' => 0.2, 'w' => 0.2784450954861111,
'h' => 0.02857142857142857, 'h' => 0.04113074042239687,
'attachment_uuid' => attachment.uuid, 'attachment_uuid' => attachment.uuid,
'page' => 0 'page' => 0
} }
] ]
}, },
{ {
'uuid' => SecureRandom.uuid, 'uuid' => SecureRandom.uuid,
'submitter_uuid' => submitter['uuid'], 'submitter_uuid' => submitter['uuid'],
'name' => 'Signature', 'name' => ['First child', field_index].compact.join(' '),
'type' => 'signature', 'type' => 'radio',
'required' => true, 'required' => true,
'preferences' => {}, 'preferences' => {},
'areas' => [ 'options' => [
{ { 'value' => 'Girl', 'uuid' => SecureRandom.uuid },
'x' => 0.08611111111111111, { 'value' => 'Boy', 'uuid' => SecureRandom.uuid }
'y' => 0.3487183422870299, ],
'w' => 0.2, 'areas' => [
'h' => 0.0707269155206287, {
'attachment_uuid' => attachment.uuid, 'x' => 0.09027777777777778,
'page' => 0 'y' => 0.3020184190330008,
} 'w' => 0.2,
] 'h' => 0.02857142857142857,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'House number', },
'type' => 'number', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'areas' => [ 'name' => ['Signature', field_index].compact.join(' '),
{ 'type' => 'signature',
'x' => 0.08333333333333333, 'required' => true,
'y' => 0.4582041442824252, 'preferences' => {},
'w' => 0.2, 'areas' => [
'h' => 0.02857142857142857, {
'attachment_uuid' => attachment.uuid, 'x' => 0.08611111111111111,
'page' => 0 'y' => 0.3487183422870299,
} 'w' => 0.2,
] 'h' => 0.0707269155206287,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'Colors', },
'type' => 'multiple', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'options' => [ 'name' => ['House number', field_index].compact.join(' '),
{ 'value' => 'Red', 'uuid' => SecureRandom.uuid }, 'type' => 'number',
{ 'value' => 'Green', 'uuid' => SecureRandom.uuid }, 'required' => true,
{ 'value' => 'Blue', 'uuid' => SecureRandom.uuid } 'preferences' => {},
], 'areas' => [
'areas' => [ {
{ 'x' => 0.08333333333333333,
'x' => 0.45, 'y' => 0.4582041442824252,
'y' => 0.1133998465080583, 'w' => 0.2,
'w' => 0.2, 'h' => 0.02857142857142857,
'h' => 0.02857142857142857, 'attachment_uuid' => attachment.uuid,
'attachment_uuid' => attachment.uuid, 'page' => 0
'page' => 0 }
} ]
] },
}, {
{ 'uuid' => SecureRandom.uuid,
'uuid' => SecureRandom.uuid, 'submitter_uuid' => submitter['uuid'],
'submitter_uuid' => submitter['uuid'], 'name' => ['Colors', field_index].compact.join(' '),
'name' => 'Gender', 'type' => 'multiple',
'type' => 'select', 'required' => true,
'required' => true, 'preferences' => {},
'preferences' => {}, 'options' => [
'options' => [ { 'value' => 'Red', 'uuid' => SecureRandom.uuid },
{ 'value' => 'Male', 'uuid' => SecureRandom.uuid }, { 'value' => 'Green', 'uuid' => SecureRandom.uuid },
{ 'value' => 'Female', 'uuid' => SecureRandom.uuid } { 'value' => 'Blue', 'uuid' => SecureRandom.uuid }
], ],
'areas' => [ 'areas' => [
{ {
'x' => 0.4513888888888889, 'x' => 0.45,
'y' => 0.1752954719877206, 'y' => 0.1133998465080583,
'w' => 0.2, 'w' => 0.2,
'h' => 0.02857142857142857, 'h' => 0.02857142857142857,
'attachment_uuid' => attachment.uuid, 'attachment_uuid' => attachment.uuid,
'page' => 0 'page' => 0
} }
] ]
}, },
{ {
'uuid' => SecureRandom.uuid, 'uuid' => SecureRandom.uuid,
'submitter_uuid' => submitter['uuid'], 'submitter_uuid' => submitter['uuid'],
'name' => 'Initials', 'name' => ['Gender', field_index].compact.join(' '),
'type' => 'initials', 'type' => 'select',
'required' => true, 'required' => true,
'preferences' => {}, 'preferences' => {},
'areas' => [ 'options' => [
{ { 'value' => 'Male', 'uuid' => SecureRandom.uuid },
'x' => 0.4486111111111111, { 'value' => 'Female', 'uuid' => SecureRandom.uuid }
'y' => 0.2273599386032233, ],
'w' => 0.1, 'areas' => [
'h' => 0.02857142857142857, {
'attachment_uuid' => attachment.uuid, 'x' => 0.4513888888888889,
'page' => 0 'y' => 0.1752954719877206,
} 'w' => 0.2,
] 'h' => 0.02857142857142857,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'Avatar', },
'type' => 'image', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'areas' => [ 'name' => ['Initials', field_index].compact.join(' '),
{ 'type' => 'initials',
'x' => 0.7180555555555556, 'required' => true,
'y' => 0.1129547198772064, 'preferences' => {},
'w' => 0.2, 'areas' => [
'h' => 0.1414538310412574, {
'attachment_uuid' => attachment.uuid, 'x' => 0.4486111111111111,
'page' => 0 'y' => 0.2273599386032233,
} 'w' => 0.1,
] 'h' => 0.02857142857142857,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'Attachment', },
'type' => 'file', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'areas' => [ 'name' => ['Avatar', field_index].compact.join(' '),
{ 'type' => 'image',
'x' => 0.7166666666666667, 'required' => true,
'y' => 0.3020107444359171, 'preferences' => {},
'w' => 0.2, 'areas' => [
'h' => 0.02857142857142857, {
'attachment_uuid' => attachment.uuid, 'x' => 0.7180555555555556,
'page' => 0 'y' => 0.1129547198772064,
} 'w' => 0.2,
] 'h' => 0.1414538310412574,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'Stamp', },
'type' => 'stamp', {
'required' => true, 'uuid' => SecureRandom.uuid,
'readonly' => true, 'submitter_uuid' => submitter['uuid'],
'preferences' => {}, 'name' => ['Attachment', field_index].compact.join(' '),
'areas' => [ 'type' => 'file',
{ 'required' => true,
'x' => 0.7166666666666667, 'preferences' => {},
'y' => 0.3771910974673829, 'areas' => [
'w' => 0.2, {
'h' => 0.0707269155206287, 'x' => 0.7166666666666667,
'attachment_uuid' => attachment.uuid, 'y' => 0.3020107444359171,
'page' => 0 'w' => 0.2,
} 'h' => 0.02857142857142857,
] 'attachment_uuid' => attachment.uuid,
}, 'page' => 0
{ }
'uuid' => SecureRandom.uuid, ]
'submitter_uuid' => submitter['uuid'], },
'name' => 'Cell code', {
'type' => 'cells', 'uuid' => SecureRandom.uuid,
'required' => true, 'submitter_uuid' => submitter['uuid'],
'preferences' => {}, 'name' => ['Stamp', field_index].compact.join(' '),
'areas' => [ 'type' => 'stamp',
{ 'required' => true,
'x' => 0.4472222222222222, 'readonly' => true,
'y' => 0.3530851880276286, 'preferences' => {},
'w' => 0.2, 'areas' => [
'h' => 0.02857142857142857, {
'cell_w' => 0.04, 'x' => 0.7166666666666667,
'attachment_uuid' => attachment.uuid, 'y' => 0.3771910974673829,
'page' => 0 'w' => 0.2,
} 'h' => 0.0707269155206287,
] 'attachment_uuid' => attachment.uuid,
}, 'page' => 0
{ }
'uuid' => SecureRandom.uuid, ]
'submitter_uuid' => submitter['uuid'], },
'name' => 'Payment', {
'type' => 'payment', 'uuid' => SecureRandom.uuid,
'required' => true, 'submitter_uuid' => submitter['uuid'],
'preferences' => { 'currency' => 'EUR', 'price' => 1000 }, 'name' => ['Cell code', field_index].compact.join(' '),
'areas' => [ 'type' => 'cells',
{ 'required' => true,
'x' => 0.4486111111111111, 'preferences' => {},
'y' => 0.43168073676132, 'areas' => [
'w' => 0.2, {
'h' => 0.02857142857142857, 'x' => 0.4472222222222222,
'attachment_uuid' => attachment.uuid, 'y' => 0.3530851880276286,
'page' => 0 'w' => 0.2,
} 'h' => 0.02857142857142857,
] 'cell_w' => 0.04,
}, 'attachment_uuid' => attachment.uuid,
{ 'page' => 0
'uuid' => SecureRandom.uuid, }
'submitter_uuid' => submitter['uuid'], ]
'name' => 'Mobile Phone', },
'type' => 'phone', {
'required' => true, 'uuid' => SecureRandom.uuid,
'preferences' => {}, 'submitter_uuid' => submitter['uuid'],
'areas' => [ 'name' => ['Payment', field_index].compact.join(' '),
{ 'type' => 'payment',
'x' => 0.44443359375, 'required' => true,
'y' => 0.3010283960092095, 'preferences' => { 'currency' => 'EUR', 'price' => 1000 },
'w' => 0.2, 'areas' => [
'h' => 0.02857142857142857, {
'attachment_uuid' => attachment.uuid, 'x' => 0.4486111111111111,
'page' => 0 'y' => 0.43168073676132,
} 'w' => 0.2,
] 'h' => 0.02857142857142857,
} 'attachment_uuid' => attachment.uuid,
].select { |f| ev.only_field_types.include?(f['type']) && ev.except_field_types.exclude?(f['type']) } 'page' => 0
}
]
},
{
'uuid' => SecureRandom.uuid,
'submitter_uuid' => submitter['uuid'],
'name' => ['Mobile Phone', field_index].compact.join(' '),
'type' => 'phone',
'required' => true,
'preferences' => {},
'areas' => [
{
'x' => 0.44443359375,
'y' => 0.3010283960092095,
'w' => 0.2,
'h' => 0.02857142857142857,
'attachment_uuid' => attachment.uuid,
'page' => 0
}
]
}
].select { |f| ev.only_field_types.include?(f['type']) && ev.except_field_types.exclude?(f['type']) }
fields fields
end
end end
template.save! template.save!

Loading…
Cancel
Save