Remove ChampDecorator

This commit is contained in:
gregoirenovel 2018-12-27 11:41:16 +01:00 committed by Pierre de La Morinerie
parent b8f57415cd
commit b41764ec03
8 changed files with 34 additions and 58 deletions

View file

@ -1,21 +0,0 @@
class ChampDecorator < Draper::Decorator
delegate_all
def value
if type_champ == TypeDeChamp.type_champs.fetch(:date) && object.value.present?
Date.parse(object.value).strftime("%d/%m/%Y")
elsif type_champ.in? [TypeDeChamp.type_champs.fetch(:checkbox), TypeDeChamp.type_champs.fetch(:engagement)]
object.value == 'on' ? 'Oui' : 'Non'
elsif type_champ == TypeDeChamp.type_champs.fetch(:yes_no)
if object.value == 'true'
'Oui'
elsif object.value == 'false'
'Non'
end
elsif type_champ == TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) && object.value.present?
JSON.parse(object.value).join(', ')
else
object.value
end
end
end

View file

@ -9,4 +9,25 @@ module ChampHelper
raw(champ.to_render_data.to_json) raw(champ.to_render_data.to_json)
# rubocop:enable Rails/OutputSafety # rubocop:enable Rails/OutputSafety
end end
def formatted_value(champ)
value = champ.value
type = champ.type_champ
if type == TypeDeChamp.type_champs.fetch(:date) && value.present?
Date.parse(value).strftime("%d/%m/%Y")
elsif type.in? [TypeDeChamp.type_champs.fetch(:checkbox), TypeDeChamp.type_champs.fetch(:engagement)]
value == 'on' ? 'Oui' : 'Non'
elsif type == TypeDeChamp.type_champs.fetch(:yes_no)
if value == 'true'
'Oui'
elsif value == 'false'
'Non'
end
elsif type == TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) && value.present?
JSON.parse(value).join(', ')
else
value
end
end
end end

View file

@ -10,10 +10,6 @@ class DropDownList < ApplicationRecord
options.select { |v| (v =~ /^--.*--$/).present? } options.select { |v| (v =~ /^--.*--$/).present? }
end end
def selected_options(champ)
champ.object.value.blank? ? [] : multiple ? JSON.parse(champ.object.value) : [champ.object.value]
end
def selected_options_without_decorator(champ) def selected_options_without_decorator(champ)
champ.value.blank? ? [] : multiple ? JSON.parse(champ.value) : [champ.value] champ.value.blank? ? [] : multiple ? JSON.parse(champ.value) : [champ.value]
end end

View file

@ -13,7 +13,7 @@
%h2 Formulaire %h2 Formulaire
- champs = @dossier.champs.decorate - champs = @dossier.champs
- if champs.any? - if champs.any?
= render partial: "shared/dossiers/champs", locals: { champs: champs, dossier: @dossier, demande_seen_at: nil, profile: 'instructeur' } = render partial: "shared/dossiers/champs", locals: { champs: champs, dossier: @dossier, demande_seen_at: nil, profile: 'instructeur' }

View file

@ -2,6 +2,7 @@
%tbody %tbody
- champs.reject(&:exclude_from_view?).each do |c| - champs.reject(&:exclude_from_view?).each do |c|
%tr %tr
- value = formatted_value(c)
- case c.type_champ - case c.type_champ
- when TypeDeChamp.type_champs.fetch(:header_section) - when TypeDeChamp.type_champs.fetch(:header_section)
%th.header-section{ colspan: 3 } %th.header-section{ colspan: 3 }
@ -10,9 +11,9 @@
%th.libelle %th.libelle
= "#{c.libelle} :" = "#{c.libelle} :"
%td.rich-text %td.rich-text
- if c.value.present? - if value.present?
%ul %ul
- c.value.split(", ").each do |item| - value.split(", ").each do |item|
%li %li
= item = item
- when TypeDeChamp.type_champs.fetch(:linked_drop_down_list) - when TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
@ -23,7 +24,7 @@
%th.libelle %th.libelle
= "#{c.libelle} :" = "#{c.libelle} :"
%td.rich-text %td.rich-text
- dossier = Dossier.includes(:procedure).find_by(id: c.value) - dossier = Dossier.includes(:procedure).find_by(id: value)
- if dossier - if dossier
- path = dossier_linked_path(current_gestionnaire, dossier) - path = dossier_linked_path(current_gestionnaire, dossier)
- if path.present? - if path.present?
@ -48,7 +49,7 @@
= "#{c.libelle} :" = "#{c.libelle} :"
%td.rich-text %td.rich-text
%span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) } %span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) }
= simple_format(c.value) = simple_format(value)
- when TypeDeChamp.type_champs.fetch(:siret) - when TypeDeChamp.type_champs.fetch(:siret)
%th.libelle %th.libelle
= "#{c.libelle} :" = "#{c.libelle} :"
@ -61,14 +62,14 @@
= "#{c.libelle} :" = "#{c.libelle} :"
%td.rich-text %td.rich-text
%span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) } %span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) }
- if c.value.present? - if value.present?
= render partial: "shared/champs/carte/show", locals: { champ: c } = render partial: "shared/champs/carte/show", locals: { champ: c }
- else - else
%th.libelle %th.libelle
= "#{c.libelle} :" = "#{c.libelle} :"
%td.rich-text %td.rich-text
%span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) } %span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) }
= sanitize(c.value) = sanitize(value)
- if c.type_champ != TypeDeChamp.type_champs.fetch(:header_section) - if c.type_champ != TypeDeChamp.type_champs.fetch(:header_section)
%td.updated-at %td.updated-at
%span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) } %span{ class: highlight_if_unseen_class(demande_seen_at, c.updated_at) }

View file

@ -10,7 +10,7 @@
= render partial: "shared/dossiers/identite_individual", locals: { individual: dossier.individual } = render partial: "shared/dossiers/identite_individual", locals: { individual: dossier.individual }
.tab-title Formulaire .tab-title Formulaire
- champs = dossier.champs.includes(:type_de_champ).decorate - champs = dossier.champs.includes(:type_de_champ)
- if champs.any? - if champs.any?
.card .card
= render partial: "shared/dossiers/champs", locals: { champs: champs, demande_seen_at: demande_seen_at, profile: profile } = render partial: "shared/dossiers/champs", locals: { champs: champs, demande_seen_at: demande_seen_at, profile: profile }

View file

@ -1,12 +1,11 @@
require 'spec_helper' require 'rails_helper'
describe ChampDecorator do RSpec.describe ChampHelper, type: :helper do
let(:type_de_champ) { create(:type_de_champ) } let(:type_de_champ) { create(:type_de_champ) }
let(:champ) { type_de_champ.champ.create } let(:champ) { type_de_champ.champ.create }
let(:decorator) { champ.decorate }
describe 'value' do describe '.formatted_value' do
subject { decorator.value } subject { formatted_value(champ) }
describe 'for a checkbox' do describe 'for a checkbox' do
let(:type_de_champ) { create(:type_de_champ_checkbox) } let(:type_de_champ) { create(:type_de_champ_checkbox) }

View file

@ -40,24 +40,4 @@ describe DropDownList do
it { expect(dropdownlist.disabled_options).to match(['--top--', '--troupt--']) } it { expect(dropdownlist.disabled_options).to match(['--top--', '--troupt--']) }
end end
describe 'selected_options' do
let(:dropdownlist) do
create(:drop_down_list, type_de_champ: type_de_champ)
end
context 'when multiple' do
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list) }
let(:champ) { type_de_champ.champ.build(value: '["1","2"]').decorate }
it { expect(dropdownlist.selected_options(champ)).to match(['1', '2']) }
end
context 'when simple' do
let(:type_de_champ) { build(:type_de_champ_drop_down_list) }
let(:champ) { type_de_champ.champ.build(value: '1').decorate }
it { expect(dropdownlist.selected_options(champ)).to match(['1']) }
end
end
end end