Projet

Général

Profil

Bug #1615 » results_controller.rb

modification fichier - Marion Maguer, 17/10/2017 15:42

 
# -*- coding: utf-8 -*-
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
before_filter :check_for_cancel, only: [:create, :update, :update_massive]
before_filter :check_for_result, only: [:edit_massive]

# GET /results
# GET /results.json
def index
@results = nil
@sample = nil
@collection = nil
# if result_params[:sample_id].nil? or result_params[:collection_id].nil?
# # No collection or sample specified. Listing not possible.
# render "index" and return
# end
if result_params[:sample_id]
@results = Result.where('sample_id=?', result_params[:sample_id])
@sample = Sample.find result_params[:sample_id]
@collection = @sample.collection
end
if result_params[:collection_id]
@results = Result.where('collection_id=?', result_params[:collection_id])
@collection = Collection.find result_params[:collection_id]
end

# Set some default values
@variable = Variable.find_by(name: "Abondance des individus (tous stades, tous états)")
@meth_ana = MethAna.first
@unit = Unit.find_by(symbol: "unité")

# Choose how to render the results index from this collection
# or sample
if @collection.name.start_with? "Granulo" or
@collection.name.start_with? "1/2 quadrats" or
@collection.name.start_with? "Epiphytes" or
@collection.name.start_with? "Biométrie" or

@variable = Variable.find_by(name: "Largeur moyenne des feuilles de Zostera marina")
@meth_ana = MethAna.find_by(name: "Mesure biométrique macroscopique")
@unit = Unit.find_by(symbol: "mm")
render "index_type2" and return

elsif @collection.name.start_with? "Carotte" or
@collection.name.start_with? "Benne" or
@collection.name.start_with? "Haveneau" or
@collection.name.start_with? "Bloc" or
@collection.samplemethod.name.start_with? "Quadrats Intertidal Rocheux REBENT"

render "index_type1" and return

elsif @collection.samplemethod.name.start_with? "Quadrats Herbier" and
@collection.name.start_with? "Quadrat"
@variable = Variable.find_by(name: "Densité des pieds de Zostera marina")
@meth_ana = MethAna.find_by(name: "Comptage de pieds de Zostera marina")
@unit = Unit.find_by(name: "Unité de dénombrement (d'individus, de cellules)")
render "index_type2" and return
end
end

# GET /results/1
# GET /results/1.json
def show
end

# GET /results/new
def new
@result = Result.new
end

# GET /results/1/edit
def edit
end

# POST /results
# POST /results.json
def create
message = "Result was successfully created."
taxonname = params[:result].extract! :taxonomy_name
@result = Result.new(result_params.except!(:taxonomy_name))
logger.debug "Taxonomy name : #{taxonname}"

# This new result, does it have a valid taxonomy ?
# If not, use the provided taxonname to create a new one
if @result.taxonomy == nil
taxon = Taxonomy.new
taxon.name = taxonname[:taxonomy_name]
taxon.save
@result.taxonomy = taxon
message << " A new taxonomy has been created for this result"
end
respond_to do |format|
if @result.save
format.html { redirect_to :back, notice: message}
format.json { render :show, status: :created, location: @result }
format.js {
# Pour ce rendu, on veut renvoyer la nouvelle liste des résultats
@results = Result.where('collection_id=?', @result.collection_id)
@collection = @result.collection
render "update"
}
else
format.html { render :new }
format.json { render json: @result.errors, status: :unprocessable_entity }
format.js
end
end
end

# PATCH/PUT /results/1
# PATCH/PUT /results/1.json
def update
respond_to do |format|
if @result.update(result_params)
format.html { redirect_to :back, notice: 'Result was successfully updated.' }
format.json { render :show, status: :ok, location: @result }
else
format.html { render :edit }
format.json { render json: @result.errors, status: :unprocessable_entity }
end
end
end

# DELETE /results/1
# DELETE /results/1.json
def destroy
@collection = @result.collection
@result.destroy
respond_to do |format|
format.html { redirect_to :back, notice: 'Result was successfully destroyed.' }
format.json { head :no_content }
format.js {
@results = Result.where('collection_id=?', @result.collection_id)
render "update"
}
end
end

# POST /resulsts/edit_massive
# Prepare the view for massive edition
def edit_massive
@results = Result.find(params[:result_ids])
@collection = @results[0].collection
end

# PATCH/PUT /resulsts/update_massive
# Prepare the view for massive edition
def update_massive
# This line updates every results with its value.
# update method has no validation check.
# So lets collect all results that where rejected in the @results list.
@results = Result.update(params[:results].keys, params[:results].values).reject{|r| r.errors.empty?}
if @results.empty?
flash[:notice] = "Results updated"
# redirect to the results of the collection from the first result of this list.
result = Result.find( params[:results].keys[0] )
redirect_to action: :index, result: {collection_id: result.collection_id}
else
logger.debug "Results rejected : #{@results.inspect}"
@collection = @results[0].collection
render "edit_massive"
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_result
@result = Result.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def result_params
params.require(:result).permit(:number, :value, :broken, :restype, :comment, :variable_id, :unit_id, :sample_id, :collection_id, :taxonomy_id, :meth_ana_id, :functionalgroup_id)
end

def check_for_cancel
if params[:commit] == "Cancel"
redirect_to :back
end
end

def check_for_result
if params[:result_ids].nil?
redirect_to :back
end
end
end
    (1-1/1)