Merge pull request #2939 from tchak/remove-facades

Remove facades
This commit is contained in:
gregoirenovel 2018-11-07 11:50:28 +01:00 committed by GitHub
commit 1075785091
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 138 additions and 194 deletions

View file

@ -5,30 +5,30 @@ class Admin::TypesDeChampController < AdminController
def destroy
@procedure.types_de_champ.destroy(params[:id])
create_facade
setup_type_de_champ_service
render 'show', format: :js
rescue ActiveRecord::RecordNotFound
render json: { message: 'Champ not found' }, status: 404
end
def show
create_facade
setup_type_de_champ_service
end
def update
if @procedure.update(TypesDeChampService.create_update_procedure_params params)
setup_type_de_champ_service
if @procedure.update(@type_de_champ_service.create_update_procedure_params(params))
flash.now.notice = 'Modifications sauvegardées'
else
flash.now.alert = @procedure.errors.full_messages.join(', ')
end
create_facade
render 'show', format: :js
end
def move_up
index = params[:index].to_i - 1
if @procedure.switch_types_de_champ index
create_facade
if @procedure.switch_types_de_champ(index)
setup_type_de_champ_service
render 'show', format: :js
else
render json: {}, status: 400
@ -36,8 +36,8 @@ class Admin::TypesDeChampController < AdminController
end
def move_down
if @procedure.switch_types_de_champ params[:index].to_i
create_facade
if @procedure.switch_types_de_champ(params[:index].to_i)
setup_type_de_champ_service
render 'show', format: :js
else
render json: {}, status: 400
@ -46,7 +46,7 @@ class Admin::TypesDeChampController < AdminController
private
def create_facade
@types_de_champ_facade = AdminTypesDeChampFacades.new false, @procedure
def setup_type_de_champ_service
@type_de_champ_service = TypesDeChampService.new(@procedure)
end
end

View file

@ -5,28 +5,31 @@ class Admin::TypesDeChampPrivateController < AdminController
def destroy
@procedure.types_de_champ_private.destroy(params[:id])
create_facade
setup_type_de_champ_service
render 'admin/types_de_champ/show', format: :js
rescue ActiveRecord::RecordNotFound
render json: { message: 'Champ not found' }, status: 404
end
def show
create_facade
setup_type_de_champ_service
render 'admin/types_de_champ/show'
end
def update
@procedure.update(TypesDeChampService.create_update_procedure_params params, true)
create_facade
flash.now.notice = 'Modifications sauvegardées'
setup_type_de_champ_service
if @procedure.update(@type_de_champ_service.create_update_procedure_params(params))
flash.now.notice = 'Modifications sauvegardées'
else
flash.now.alert = @procedure.errors.full_messages.join(', ')
end
render 'admin/types_de_champ/show', format: :js
end
def move_up
index = params[:index].to_i - 1
if @procedure.switch_types_de_champ_private index
create_facade
if @procedure.switch_types_de_champ_private(index)
setup_type_de_champ_service
render 'admin/types_de_champ/show', format: :js
else
render json: {}, status: 400
@ -34,8 +37,8 @@ class Admin::TypesDeChampPrivateController < AdminController
end
def move_down
if @procedure.switch_types_de_champ_private params[:index].to_i
create_facade
if @procedure.switch_types_de_champ_private(params[:index].to_i)
setup_type_de_champ_service
render 'admin/types_de_champ/show', format: :js
else
render json: {}, status: 400
@ -44,7 +47,7 @@ class Admin::TypesDeChampPrivateController < AdminController
private
def create_facade
@types_de_champ_facade = AdminTypesDeChampFacades.new true, @procedure
def setup_type_de_champ_service
@type_de_champ_service = TypesDeChampService.new(@procedure, true)
end
end

View file

@ -1,48 +0,0 @@
class AdminTypesDeChampFacades
include Rails.application.routes.url_helpers
def initialize(private, procedure)
@private = private
@procedure = procedure
end
def private
@private
end
def active
@private ? 'Annotations privées' : 'Champs'
end
def url
@private ? admin_procedure_types_de_champ_private_path(@procedure) : admin_procedure_types_de_champ_path(@procedure)
end
def types_de_champ
@private ? @procedure.types_de_champ_private_ordered.decorate : @procedure.types_de_champ_ordered.decorate
end
def new_type_de_champ
TypeDeChamp.new(private: @private).decorate
end
def fields_for_var
@private ? :types_de_champ_private : :types_de_champ
end
def move_up_url(ff)
@private ? move_up_admin_procedure_types_de_champ_private_path(@procedure, ff.index) : move_up_admin_procedure_types_de_champ_path(@procedure, ff.index)
end
def move_down_url(ff)
@private ? move_down_admin_procedure_types_de_champ_private_path(@procedure, ff.index) : move_down_admin_procedure_types_de_champ_path(@procedure, ff.index)
end
def delete_url(ff)
@private ? admin_procedure_type_de_champ_private_path(@procedure, ff.object.id) : admin_procedure_type_de_champ_path(@procedure, ff.object.id)
end
def add_button_id
@private ? :add_type_de_champ_private : :add_type_de_champ
end
end

View file

@ -1,55 +0,0 @@
class DossierFacades
# TODO rechercher en fonction de la personne/email
def initialize(dossier_id, email, champ_id = nil)
@dossier = Dossier.find(dossier_id)
@champ_id = champ_id
end
def dossier
@dossier.decorate
end
def champs
@dossier.champs
end
def etablissement
@dossier.etablissement
end
def types_de_pieces_justificatives
@dossier.types_de_piece_justificative.order('order_place ASC')
end
def champ_id
@champ_id
end
def commentaires
@dossier.commentaires.where(champ_id: @champ_id).decorate
end
def procedure
@dossier.procedure
end
def invites
@dossier.invites
end
def champs_private
@dossier.champs_private
end
def individual
@dossier.individual
end
def commentaires_files
PieceJustificative.where(dossier_id: @dossier.id, type_de_piece_justificative_id: nil)
end
def followers
@dossier.followers_gestionnaires
end
end

View file

@ -1,19 +0,0 @@
module TypeDeChampHelper
TOGGLES = {
TypeDeChamp.type_champs.fetch(:piece_justificative) => :champ_pj?,
TypeDeChamp.type_champs.fetch(:siret) => :champ_siret?,
TypeDeChamp.type_champs.fetch(:linked_drop_down_list) => :champ_linked_dropdown?,
TypeDeChamp.type_champs.fetch(:integer_number) => :champ_integer_number?
}
def tdc_options
tdcs = TypeDeChamp.type_de_champs_list_fr
tdcs.select! do |tdc|
toggle = TOGGLES[tdc.last]
toggle.blank? || Flipflop.send(toggle)
end
tdcs
end
end

View file

@ -1,7 +1,71 @@
class TypesDeChampService
def self.create_update_procedure_params(params, private = false)
attributes = (private ? 'types_de_champ_private_attributes' : 'types_de_champ_attributes')
include Rails.application.routes.url_helpers
TOGGLES = {
TypeDeChamp.type_champs.fetch(:piece_justificative) => :champ_pj?,
TypeDeChamp.type_champs.fetch(:siret) => :champ_siret?,
TypeDeChamp.type_champs.fetch(:linked_drop_down_list) => :champ_linked_dropdown?,
TypeDeChamp.type_champs.fetch(:integer_number) => :champ_integer_number?
}
def options
types_de_champ = TypeDeChamp.type_de_champs_list_fr
types_de_champ.select! do |tdc|
toggle = TOGGLES[tdc.last]
toggle.blank? || Flipflop.send(toggle)
end
types_de_champ
end
def initialize(procedure, private_type_de_champ = false)
@procedure = procedure
@private_type_de_champ = private_type_de_champ
end
def private?
@private_type_de_champ
end
def active
private? ? 'Annotations privées' : 'Champs'
end
def url
private? ? admin_procedure_types_de_champ_private_path(@procedure) : admin_procedure_types_de_champ_path(@procedure)
end
def types_de_champ
private? ? @procedure.types_de_champ_private_ordered.decorate : @procedure.types_de_champ_ordered.decorate
end
def new_type_de_champ
TypeDeChamp.new(private: private?).decorate
end
def fields_for_var
private? ? :types_de_champ_private : :types_de_champ
end
def move_up_url(ff)
private? ? move_up_admin_procedure_types_de_champ_private_path(@procedure, ff.index) : move_up_admin_procedure_types_de_champ_path(@procedure, ff.index)
end
def move_down_url(ff)
private? ? move_down_admin_procedure_types_de_champ_private_path(@procedure, ff.index) : move_down_admin_procedure_types_de_champ_path(@procedure, ff.index)
end
def delete_url(ff)
private? ? admin_procedure_type_de_champ_private_path(@procedure, ff.object.id) : admin_procedure_type_de_champ_path(@procedure, ff.object.id)
end
def add_button_id
private? ? :add_type_de_champ_private : :add_type_de_champ
end
def create_update_procedure_params(params)
attributes = "#{fields_for_var}_attributes"
params_with_ordered_champs = order_champs(params, attributes)
parameters = params_with_ordered_champs
@ -21,13 +85,13 @@ class TypesDeChampService
])
parameters[attributes].each do |index, param|
param[:private] = private
param[:private] = private?
if param[:libelle].empty?
parameters[attributes].delete(index.to_s)
end
if param['drop_down_list_attributes'] && param['drop_down_list_attributes']['value']
param['drop_down_list_attributes']['value'] = self.clean_value (param['drop_down_list_attributes']['value'])
param['drop_down_list_attributes']['value'] = clean_value(param['drop_down_list_attributes']['value'])
end
end
@ -36,7 +100,7 @@ class TypesDeChampService
private
def self.order_champs(params, attributes)
def order_champs(params, attributes)
# It's OK to use an unsafe hash here because the params will then go through
# require / permit methods in #create_update_procedure_params
tdcas = params[:procedure][attributes].to_unsafe_hash.to_a
@ -64,15 +128,15 @@ class TypesDeChampService
params
end
def self.is_number?(value)
def is_number?(value)
(value =~ /^[0-9]+$/) == 0
end
def self.tdca_order_changed?(tdca)
def tdca_order_changed?(tdca)
(tdca[:order_place].to_i + 1) != tdca[:custom_order_place].to_i
end
def self.clean_value(value)
def clean_value(value)
value.split("\r\n").map(&:strip).join("\r\n")
end
end

View file

@ -1,4 +1,4 @@
= f.fields_for @types_de_champ_facade.fields_for_var, types_de_champ, remote: true do |ff|
= f.fields_for type_de_champ_service.fields_for_var, types_de_champ, remote: true do |ff|
- type_champ = ff.object.object.type_champ
.form-inline{ class: (type_champ == TypeDeChamp.type_champs.fetch(:header_section) ? 'header-section' : nil) }
@ -8,7 +8,7 @@
.form-group.type
%h4 Type
= ff.select :type_champ, tdc_options, {}, { class: 'form-control type-champ' }
= ff.select :type_champ, type_de_champ_service.options, {}, { class: 'form-control type-champ' }
.form-group.description
%h4 Description
@ -65,8 +65,8 @@
- if ff.object.id.present?
.form-group
%br &nbsp;
= ff.object.button_up(index: ff.index, url: @types_de_champ_facade.move_up_url(ff), private: @types_de_champ_facade.private)
= ff.object.button_down(index: ff.index, url: @types_de_champ_facade.move_down_url(ff), private: @types_de_champ_facade.private)
= ff.object.button_up(index: ff.index, url: type_de_champ_service.move_up_url(ff), private: type_de_champ_service.private?)
= ff.object.button_down(index: ff.index, url: type_de_champ_service.move_down_url(ff), private: type_de_champ_service.private?)
.form-group
%h4 position
@ -76,10 +76,10 @@
%br &nbsp;
- if ff.object.id.nil?
= f.button 'Ajouter le champ',
id: @types_de_champ_facade.add_button_id,
id: type_de_champ_service.add_button_id,
class: 'btn btn-success',
data: { disable: true }
- else
= link_to("", @types_de_champ_facade.delete_url(ff), method: :delete, remote: true, id: "delete_type_de_champ_#{ff.object.id}", class: %w(form-control btn btn-danger fa fa-trash-o) )
= link_to("", type_de_champ_service.delete_url(ff), method: :delete, remote: true, id: "delete_type_de_champ_#{ff.object.id}", class: %w(form-control btn btn-danger fa fa-trash-o) )
%div{ style: 'background-color: rgb(204, 204, 204); height: 1px; margin: 30px auto;' }

View file

@ -1,5 +1,5 @@
= form_for [:admin, @procedure], url: @types_de_champ_facade.url, remote: true do |f|
= render partial: 'admin/types_de_champ/fields', locals: { types_de_champ: @types_de_champ_facade.types_de_champ, f: f }
= form_for [:admin, procedure], url: type_de_champ_service.url, remote: true do |f|
= render partial: 'admin/types_de_champ/fields', locals: { type_de_champ_service: type_de_champ_service, types_de_champ: type_de_champ_service.types_de_champ, f: f }
= f.button 'Enregistrer',
id: :save,
@ -8,4 +8,4 @@
%hr
#new_type_de_champ
= render partial: 'admin/types_de_champ/fields', locals: { types_de_champ: @types_de_champ_facade.new_type_de_champ, f: f }
= render partial: 'admin/types_de_champ/fields', locals: { type_de_champ_service: type_de_champ_service, types_de_champ: type_de_champ_service.new_type_de_champ, f: f }

View file

@ -1,3 +1,3 @@
.row.white-back
#liste-champ
= render partial: 'admin/types_de_champ/form'
= render partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, type_de_champ_service: @type_de_champ_service }

View file

@ -1,2 +1,2 @@
<%= render_flash(timeout: 3000, sticky: true) %>
<%= render_to_element('#liste-champ', partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, types_de_champ: @types_de_champ }) %>
<%= render_to_element('#liste-champ', partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, type_de_champ_service: @type_de_champ_service }) %>

View file

@ -1 +1 @@
= render partial: 'layouts/left_panels/left_panel_admin_procedurescontroller_navbar', locals: { active: @types_de_champ_facade.active }
= render partial: 'layouts/left_panels/left_panel_admin_procedurescontroller_navbar', locals: { active: @type_de_champ_service.active }

View file

@ -1,25 +0,0 @@
require 'rails_helper'
RSpec.describe TypeDeChampHelper, type: :helper do
describe ".tdc_options" do
let(:pj_option) { ["Pièce justificative", TypeDeChamp.type_champs.fetch(:piece_justificative)] }
subject { tdc_options }
context "when the champ_pj is enabled" do
before do
Flipflop::FeatureSet.current.test!.switch!(:champ_pj, true)
end
it { is_expected.to include(pj_option) }
end
context "when the champ_pj is disabled" do
before do
Flipflop::FeatureSet.current.test!.switch!(:champ_pj, false)
end
it { is_expected.not_to include(pj_option) }
end
end
end

View file

@ -2,10 +2,12 @@ require 'spec_helper'
describe TypesDeChampService do
let(:params) { ActionController::Parameters.new({ procedure: { types_de_champ_attributes: types_de_champ_attributes } }) }
let(:procedure) { create(:procedure) }
let(:service) { TypesDeChampService.new(procedure) }
let(:result) { TypesDeChampService.create_update_procedure_params(params) }
describe 'create_update_procedure_params' do
let(:result) { service.create_update_procedure_params(params) }
describe 'self.create_update_procedure_params' do
describe 'the drop down list attributes' do
let(:types_de_champ_attributes) do
{
@ -99,4 +101,26 @@ describe TypesDeChampService do
end
end
end
describe ".options" do
let(:pj_option) { ["Pièce justificative", TypeDeChamp.type_champs.fetch(:piece_justificative)] }
subject { service.options }
context "when the champ_pj is enabled" do
before do
Flipflop::FeatureSet.current.test!.switch!(:champ_pj, true)
end
it { is_expected.to include(pj_option) }
end
context "when the champ_pj is disabled" do
before do
Flipflop::FeatureSet.current.test!.switch!(:champ_pj, false)
end
it { is_expected.not_to include(pj_option) }
end
end
end

View file

@ -14,7 +14,7 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do
before do
procedure.reload
assign(:procedure, procedure)
assign(:types_de_champ_facade, AdminTypesDeChampFacades.new(false, procedure))
assign(:type_de_champ_service, TypesDeChampService.new(procedure))
render
end
it 'sorts by order place' do
@ -26,7 +26,7 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do
subject do
procedure.reload
assign(:procedure, procedure)
assign(:types_de_champ_facade, AdminTypesDeChampFacades.new(false, procedure))
assign(:type_de_champ_service, TypesDeChampService.new(procedure))
render
rendered
end

View file

@ -14,7 +14,7 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do
before do
procedure.reload
assign(:procedure, procedure)
assign(:types_de_champ_facade, AdminTypesDeChampFacades.new(true, procedure))
assign(:type_de_champ_service, TypesDeChampService.new(procedure, true))
render
end
it 'sorts by order place' do
@ -26,7 +26,7 @@ describe 'admin/types_de_champ/show.html.haml', type: :view do
subject do
procedure.reload
assign(:procedure, procedure)
assign(:types_de_champ_facade, AdminTypesDeChampFacades.new(true, procedure))
assign(:type_de_champ_service, TypesDeChampService.new(procedure, true))
render
rendered
end