add move down for type de champ controller and refactor

This commit is contained in:
Tanguy PATTE 2015-11-19 11:37:01 +01:00
parent 0503e2f278
commit fde504b54d
5 changed files with 86 additions and 9 deletions

View file

@ -22,16 +22,19 @@ class Admin::TypesDeChampController < AdminController
end
def move_up
index = params[:index].to_i
if @procedure.types_de_champ.count < 2 || index < 1
render json: {}, status: 400
else
types_de_champ_to_move_down = @procedure.types_de_champ_ordered[index - 1]
types_de_champ_to_move_up = @procedure.types_de_champ_ordered[index]
types_de_champ_to_move_down.update_attributes(order_place: index)
types_de_champ_to_move_up.update_attributes(order_place: index - 1)
index = params[:index].to_i - 1
if @procedure.switch_types_de_champ index
render 'show', format: :js
else
render json: {}, status: 400
end
end
def move_down
if @procedure.switch_types_de_champ params[:index].to_i
render 'show', format: :js
else
render json: {}, status: 400
end
end

View file

@ -12,4 +12,15 @@ class Procedure < ActiveRecord::Base
def types_de_champ_ordered
types_de_champ.order(:order_place)
end
def switch_types_de_champ index_of_first_element
return false if index_of_first_element < 0
types_de_champ_tmp = types_de_champ_ordered
nb_types_de_champ = types_de_champ_tmp.count
return false if index_of_first_element == nb_types_de_champ - 1
return false if types_de_champ_ordered.count < 1
types_de_champ_tmp[index_of_first_element].update_attributes(order_place: index_of_first_element + 1)
types_de_champ_tmp[index_of_first_element + 1].update_attributes(order_place: index_of_first_element)
true
end
end