Use "to_assignable_attributes"
This commit is contained in:
parent
4a6841c009
commit
fc94aaaa21
6 changed files with 50 additions and 34 deletions
|
@ -51,11 +51,7 @@ class PrefillDescription < SimpleDelegator
|
|||
|
||||
def prefilled_champs_for_query
|
||||
prefilled_champs.map do |type_de_champ|
|
||||
if type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:repetition)
|
||||
"\"champ_#{type_de_champ.to_typed_id}\": #{type_de_champ.example_value}"
|
||||
else
|
||||
"\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.example_value}\""
|
||||
end
|
||||
"\"champ_#{type_de_champ.to_typed_id}\": #{type_de_champ.formatted_example_value}"
|
||||
end.join(', ')
|
||||
end
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ class PrefillParams
|
|||
end
|
||||
|
||||
def to_a
|
||||
build_prefill_values.filter(&:prefillable?).map(&:to_h).flatten
|
||||
build_prefill_values.filter(&:prefillable?).map(&:champ_attributes).flatten
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -54,15 +54,10 @@ class PrefillParams
|
|||
champ.prefillable? && valid?
|
||||
end
|
||||
|
||||
def to_h
|
||||
if champ.type_champ == TypeDeChamp.type_champs.fetch(:repetition)
|
||||
repeatable_hashes
|
||||
else
|
||||
{
|
||||
id: champ.id,
|
||||
value: value
|
||||
}
|
||||
end
|
||||
def champ_attributes
|
||||
TypesDeChamp::PrefillTypeDeChamp
|
||||
.build(champ.type_de_champ)
|
||||
.to_assignable_attributes(champ, value)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -70,22 +65,8 @@ class PrefillParams
|
|||
def valid?
|
||||
return true unless NEED_VALIDATION_TYPES_DE_CHAMPS.include?(champ.type_champ)
|
||||
|
||||
champ.value = value
|
||||
champ.assign_attributes(champ_attributes)
|
||||
champ.valid?(:prefill)
|
||||
end
|
||||
|
||||
def repeatable_hashes
|
||||
return [] unless value.is_a?(Array)
|
||||
|
||||
value.map.with_index do |repetition, index|
|
||||
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
|
||||
JSON.parse(repetition).map do |key, value|
|
||||
id = row.find { |champ| champ.libelle == key }&.id
|
||||
next unless id
|
||||
{ id: id, value: value }
|
||||
end.compact
|
||||
rescue JSON::ParserError
|
||||
end.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,6 +13,22 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
|
|||
[row_values_format, row_values_format].map { |row| row.to_s.gsub("=>", ":") }
|
||||
end
|
||||
|
||||
alias_method :formatted_example_value, :example_value
|
||||
|
||||
def to_assignable_attributes(champ, value)
|
||||
return [] unless value.is_a?(Array)
|
||||
|
||||
value.map.with_index do |repetition, index|
|
||||
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
|
||||
JSON.parse(repetition).map do |key, value|
|
||||
id = row.find { |champ| champ.libelle == key }&.id
|
||||
next unless id
|
||||
{ id: id, value: value }
|
||||
end.compact
|
||||
rescue JSON::ParserError
|
||||
end.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def too_many_possible_values?
|
||||
|
|
|
@ -33,6 +33,14 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
|
|||
I18n.t("views.prefill_descriptions.edit.examples.#{type_champ}")
|
||||
end
|
||||
|
||||
def formatted_example_value
|
||||
"\"#{example_value}\"" if example_value.present?
|
||||
end
|
||||
|
||||
def to_assignable_attributes(champ, value)
|
||||
{ id: champ.id, value: value }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def possible_values_list
|
||||
|
|
|
@ -72,12 +72,12 @@ RSpec.describe PrefillParams do
|
|||
context "when the type de champ is authorized (#{type_de_champ_type})" do
|
||||
let(:types_de_champ_public) { [{ type: type_de_champ_type }] }
|
||||
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
||||
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
|
||||
let(:champ) { find_champ_by_stable_id(dossier, type_de_champ.stable_id) }
|
||||
|
||||
let(:params) { { "champ_#{type_de_champ.to_typed_id}" => value } }
|
||||
|
||||
it "builds an array of hash(id, value) matching the given params" do
|
||||
expect(prefill_params_array).to match([{ id: champ_id, value: value }])
|
||||
expect(prefill_params_array).to match([{ id: champ.id }.merge(attributes(champ, value))])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -86,12 +86,12 @@ RSpec.describe PrefillParams do
|
|||
context "when the type de champ is authorized (#{type_de_champ_type})" do
|
||||
let(:types_de_champ_private) { [{ type: type_de_champ_type }] }
|
||||
let(:type_de_champ) { procedure.published_revision.types_de_champ_private.first }
|
||||
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
|
||||
let(:champ) { find_champ_by_stable_id(dossier, type_de_champ.stable_id) }
|
||||
|
||||
let(:params) { { "champ_#{type_de_champ.to_typed_id}" => value } }
|
||||
|
||||
it "builds an array of hash(id, value) matching the given params" do
|
||||
expect(prefill_params_array).to match([{ id: champ_id, value: value }])
|
||||
expect(prefill_params_array).to match([{ id: champ.id }.merge(attributes(champ, value))])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -231,4 +231,10 @@ RSpec.describe PrefillParams do
|
|||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
dossier.champs.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||
end
|
||||
|
||||
def attributes(champ, value)
|
||||
TypesDeChamp::PrefillTypeDeChamp
|
||||
.build(champ.type_de_champ)
|
||||
.to_assignable_attributes(champ, value)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -82,4 +82,13 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
|
|||
it { expect(example_value).to eq(I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_assignable_attributes' do
|
||||
let(:type_de_champ) { build(:type_de_champ_email) }
|
||||
let(:champ) { build(:champ, type_de_champ: type_de_champ) }
|
||||
let(:value) { "any@email.org" }
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ).to_assignable_attributes(champ, value) }
|
||||
|
||||
it { is_expected.to match({ id: champ.id, value: value }) }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue