# encoding: utf-8 module Flyboy class TasksController < ::Flyboy::ApplicationController handles_sortable_columns before_action :set_objects, only: [:show, :edit, :update, :destroy, :complete, :snooze] def index authorize! :read, Task @filters = SmallData::FilterForTasks.new(cookies) @tasks = @filters.apply get_index_tasks respond_to do |format| format.html format.csv { send_data @tasks.to_csv, filename: "feuille_de_route_#{Date.today}.csv", disposition: "attachment"} format.xls format.pdf do pdf = Roadmap.new(@tasks) pdf.build send_data pdf.render, filename: "feuille_de_route_#{Date.today}.pdf", disposition: "inline" end end end def show @task = Task.find(params[:id]) authorize! :read, @task end def new @goal = Goal.find(params[:goal_id]) @task = @goal.tasks.new authorize! :create, @task end def edit authorize! :update, @task end def create @task = Task.new(task_params) authorize! :create, @task if @task.save redirect_to @task, notice: t("messages.tasks.create_ok") else render :new end end def update authorize! :update, @task if @task.update_attributes(task_params) redirect_to @task, notice: t("messages.tasks.update_ok") else render :edit end end def destroy authorize! :delete, @task @task.destroy redirect_to tasks_url end def complete authorize! :complete, @task comment = @task.comments.create( :progress => 100, :description =>"Marquée comme terminée.", :date => DateTime.now ) redirect_to request.referer end def snooze @task.snooze @task.save redirect_to tasks_url end private def get_index_tasks Task.all.order(get_index_order) end def get_index_order sortable_column_order do |column, direction| case column when "title", "status" %(LOWER(flyboy_tasks.#{column}) #{direction}) when "goal" %(LOWER(flyboy_goals.title) #{direction}) else params["sort"] = "term" "term ASC" end end end def set_objects @task = Task.find params[:id] @goal = @task.goal end def task_params params.require(:task).permit! end end end