Refactor code for private type de champ

This commit is contained in:
Xavier J 2016-08-04 12:06:46 +02:00
parent 116dd0d94f
commit ddb64878c8
13 changed files with 93 additions and 71 deletions

View file

@ -4,16 +4,19 @@ class Admin::TypesDeChampController < AdminController
def destroy
@procedure.types_de_champ.destroy(params[:id])
create_facade
render 'show', format: :js
rescue ActiveRecord::RecordNotFound
render json: { message: 'Champ not found' }, status: 404
end
def show
create_facade
end
def update
@procedure.update_attributes(update_params)
create_facade
flash.now.notice = 'Modifications sauvegardées'
render 'show', format: :js
end
@ -27,6 +30,7 @@ class Admin::TypesDeChampController < AdminController
def move_up
index = params[:index].to_i - 1
if @procedure.switch_types_de_champ index
create_facade
render 'show', format: :js
else
render json: {}, status: 400
@ -35,9 +39,16 @@ class Admin::TypesDeChampController < AdminController
def move_down
if @procedure.switch_types_de_champ params[:index].to_i
create_facade
render 'show', format: :js
else
render json: {}, status: 400
end
end
private
def create_facade
@types_de_champ_facade = AdminTypesDeChampFacades.new false, @procedure
end
end

View file

@ -4,19 +4,22 @@ class Admin::TypesDeChampPrivateController < AdminController
def destroy
@procedure.types_de_champ_private.destroy(params[:id])
render 'show', format: :js
create_facade
render 'admin/types_de_champ/show', format: :js
rescue ActiveRecord::RecordNotFound
render json: { message: 'Champ not found' }, status: 404
end
def show
create_facade
render 'admin/types_de_champ/show'
end
def update
@procedure.update_attributes(update_params)
create_facade
flash.now.notice = 'Modifications sauvegardées'
render 'show', format: :js
render 'admin/types_de_champ/show', format: :js
end
def update_params
@ -28,7 +31,8 @@ class Admin::TypesDeChampPrivateController < AdminController
def move_up
index = params[:index].to_i - 1
if @procedure.switch_types_de_champ_private index
render 'show', format: :js
create_facade
render 'admin/types_de_champ/show', format: :js
else
render json: {}, status: 400
end
@ -36,9 +40,16 @@ class Admin::TypesDeChampPrivateController < AdminController
def move_down
if @procedure.switch_types_de_champ_private params[:index].to_i
render 'show', format: :js
create_facade
render 'admin/types_de_champ/show', format: :js
else
render json: {}, status: 400
end
end
private
def create_facade
@types_de_champ_facade = AdminTypesDeChampFacades.new true, @procedure
end
end

View file

@ -0,0 +1,48 @@
class AdminTypesDeChampFacades
include Rails.application.routes.url_helpers
def initialize private, procedure
@private = private
@procedure = procedure
end
def private
@private
end
def active
@private ? 'Champs privés' : '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
@private ? TypeDeChampPrivate.new.decorate : TypeDeChampPublic.new.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,4 +1,4 @@
= f.fields_for :types_de_champ, types_de_champ, remote: true do |ff|
= f.fields_for @types_de_champ_facade.fields_for_var, types_de_champ, remote: true do |ff|
.form-inline{class:"#{ff.object.object.type_champ == 'header_section' ? 'header_section' : ''}"}
.form-group.libelle
%h4 Libellé
@ -6,7 +6,7 @@
.form-group.type
%h4 Type
= ff.select :type_champ, TypeDeChampPublic.type_champs, {}, {class: 'form-control type_champ'}
= ff.select :type_champ, TypeDeChamp.type_champs, {}, {class: 'form-control type_champ'}
.form-group.description
%h4 Description
@ -21,17 +21,18 @@
= ff.hidden_field :order_place, value: ff.index
= ff.hidden_field :type
= ff.hidden_field :id
- unless ff.object.id.nil?
.form-group
%br &nbsp;
= ff.object.button_up(index: ff.index, url: move_up_admin_procedure_types_de_champ_path(@procedure, ff.index))
= ff.object.button_down(index: ff.index, url: move_down_admin_procedure_types_de_champ_path(@procedure, ff.index))
= 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)
.form-group
%br &nbsp;
- if ff.object.id.nil?
= f.submit "Ajouter le champ", class: 'btn btn-success', id: :add_type_de_champ
= f.submit "Ajouter le champ", class: 'btn btn-success', id: @types_de_champ_facade.add_button_id
- else
= link_to("", admin_procedure_type_de_champ_path(@procedure, ff.object.id), 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("", @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) )

View file

@ -1,6 +1,6 @@
= form_for [:admin, @procedure], url: admin_procedure_types_de_champ_path(@procedure) , remote: true do |f|
= render partial: 'fields', locals: { types_de_champ: @procedure.types_de_champ_ordered.decorate, f: f }
= 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 }
= f.submit "Enregistrer", class: 'btn btn-success', id: :save
%hr
#new_type_de_champ
= render partial: 'fields', locals: { types_de_champ: TypeDeChampPublic.new.decorate, f: f }
= render partial: 'admin/types_de_champ/fields', locals: { types_de_champ: @types_de_champ_facade.new_type_de_champ, f: f }

View file

@ -1,4 +1,4 @@
=render partial: 'admin/procedures/head', locals: {active: 'Champs'}
=render partial: 'admin/procedures/head', locals: {active: @types_de_champ_facade.active}
#liste_champ
= render partial: 'form'
= render partial: 'admin/types_de_champ/form'

View file

@ -1,5 +1,5 @@
<% flash.each do |type, message| %>
$("#flash_message").html("<div class=\"alert alert-success move_up\" style=\"display: block:\"> <%= message.html_safe %></div>").children().fadeOut(5000)
<% end %>
$('#liste_champ').html("<%= escape_javascript(render partial: 'form', locals: { procedure: @procedure, types_de_champ: @types_de_champ } ) %>");
$('#liste_champ').html("<%= escape_javascript(render partial: 'admin/types_de_champ/form', locals: { procedure: @procedure, types_de_champ: @types_de_champ } ) %>");
on_change_type_de_champ_select ();

View file

@ -1,38 +0,0 @@
= f.fields_for :types_de_champ_private, types_de_champ_private, remote: true do |ff|
.form-inline{class:"#{ff.object.object.type_champ == 'header_section' ? 'header_section' : ''}"}
.form-group.libelle
%h4 Libellé
= ff.text_field :libelle, class: 'form-control libelle', placeholder: 'Libellé'
.form-group.type
%h4 Type
= ff.select :type_champ, TypeDeChampPrivate.type_champs, {}, {class: 'form-control type_champ'}
.form-group.description
%h4 Description
= ff.text_area :description, class: 'form-control description', placeholder: 'Description'
.form-group.mandatory
%h4 Obligatoire ?
.center
= ff.check_box :mandatory, placeholder: 'Obligatoire ?'
.form-group
= ff.hidden_field :order_place, value: ff.index
= ff.hidden_field :type
= ff.hidden_field :id
- unless ff.object.id.nil?
.form-group
%br &nbsp;
= ff.object.button_up(index: ff.index, url: move_up_admin_procedure_types_de_champ_private_path(@procedure, ff.index), private: true)
= ff.object.button_down(index: ff.index, url: move_down_admin_procedure_types_de_champ_private_path(@procedure, ff.index), private: true)
.form-group
%br &nbsp;
- if ff.object.id.nil?
= f.submit "Ajouter le champ", class: 'btn btn-success', id: :add_type_de_champ_private
- else
= link_to("", admin_procedure_type_de_champ_private_path(@procedure, ff.object.id), method: :delete, remote: true, id: "delete_type_de_champ_private_#{ff.object.id}", class: %w(form-control btn btn-danger fa fa-trash-o) )

View file

@ -1,6 +0,0 @@
= form_for [:admin, @procedure], url: admin_procedure_types_de_champ_private_path(@procedure) , remote: true do |f|
= render partial: 'fields', locals: { types_de_champ_private: @procedure.types_de_champ_private_ordered.decorate, f: f }
= f.submit "Enregistrer", class: 'btn btn-success', id: :save
%hr
#new_type_de_champ
= render partial: 'fields', locals: { types_de_champ_private: TypeDeChampPrivate.new.decorate, f: f }

View file

@ -1,4 +0,0 @@
=render partial: 'admin/procedures/head', locals: {active: 'Champs privés'}
#liste_champ
= render partial: 'form'

View file

@ -1,5 +0,0 @@
<% flash.each do |type, message| %>
$("#flash_message").html("<div class=\"alert alert-success move_up\" style=\"display: block:\"> <%= message.html_safe %></div>").children().fadeOut(5000)
<% end %>
$('#liste_champ').html("<%= escape_javascript(render partial: 'form', locals: { procedure: @procedure, types_de_champ: @types_de_champ } ) %>");
on_change_type_de_champ_select ();

View file

@ -11,6 +11,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))
render
end
it 'sorts by order place' do
@ -22,6 +23,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))
render
rendered
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe 'admin/types_de_champ_private/show.html.haml', type: :view do
describe 'admin/types_de_champ/show.html.haml', type: :view do
let(:procedure) { create(:procedure) }
describe 'fields sorted' do
@ -11,6 +11,7 @@ describe 'admin/types_de_champ_private/show.html.haml', type: :view do
before do
procedure.reload
assign(:procedure, procedure)
assign(:types_de_champ_facade, AdminTypesDeChampFacades.new(true, procedure))
render
end
it 'sorts by order place' do
@ -22,6 +23,7 @@ describe 'admin/types_de_champ_private/show.html.haml', type: :view do
subject do
procedure.reload
assign(:procedure, procedure)
assign(:types_de_champ_facade, AdminTypesDeChampFacades.new(true, procedure))
render
rendered
end