Class: AuditsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- AuditsController
- Includes:
- Pagy::Backend
- Defined in:
- app/controllers/audits_controller.rb
Overview
This controller manage the views refering to Audit model
-
before_action :powered_in!
-
before_action :set_user
-
before_action set_audit, except: [:index, :create, :destroy]
Instance Method Summary collapse
-
#create ⇒ Object
POST /users/user_id/audits.
-
#destroy ⇒ Object
DELETE /user/:user_id/audits/:id.
-
#edit ⇒ Object
GET /user/:user_id/audits/:id/edit.
-
#index ⇒ Object
GET /users/:user_id/audits.
-
#list ⇒ Object
GET /audits/list.
-
#update ⇒ Object
PATCH /user/:user_id/audits/:id.
Methods inherited from ApplicationController
#admin_in!, #doctor_in!, #powered_in!, #secretary_in!, #translate_errors
Instance Method Details
#create ⇒ Object
POST /users/user_id/audits
Try to make a new audit.
-
set @audit as new audit
otherwise status :500
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/controllers/audits_controller.rb', line 39 def create @audit = Audit.unscoped.find_or_initialize_by( category_id: audit_params[:category_id], user_id: @user.id ) @audit.category_id = audit_params[:category_id] @audit.user_id = @user.id @audit.expire = Time.zone.now @audit.status = 'created' @audit.histories_attributes = [status: 'created', author_id: current_user.id] if @audit.save flash.now[:success] = 'Creazione avvenuta con successo' else flash.now[:error] = write_errors @audit end category = Category.find(audit_params[:category_id]) render turbo_stream: [ turbo_stream.replace("user_#{@user.id}", partial: 'users/user', locals: {user: @user, current_user: current_user}), turbo_stream.replace("user_#{@user.id}_category_#{category.id}", partial: 'audits/category', locals: {user: @user, category: category}), turbo_stream.replace(:flashes, partial: "flashes") ] # render partial: 'audits/category', locals: { category: @audit.category, user: @user } end |
#destroy ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/controllers/audits_controller.rb', line 103 def destroy @audit = Audit.unscoped.where('id = ? and user_id = ?', params[:id], @user.id).first @audit.histories_attributes = [status: 'deleted', author_id: current_user.id] if @audit.save flash.now[:success] = 'Cancellazione avvenuta con successo' @audit.meetings.delete_all end category = Category.find(audit_params[:category_id]) render turbo_stream: [ turbo_stream.replace("user_#{@user.id}", partial: 'users/user', locals: {user: @user, current_user: current_user}), turbo_stream.replace("user_#{@user.id}_category_#{category.id}", partial: 'audits/category', locals: {user: @user, category: category}), turbo_stream.replace(:flashes, partial: "flashes") ] end |
#edit ⇒ Object
GET /user/:user_id/audits/:id/edit
render the form for update the audit and show his history
-
set @audit from #set_audit
-
set @history with new History for @audit
-
set @pagy, @history with @audit.histories pagination
70 71 72 73 |
# File 'app/controllers/audits_controller.rb', line 70 def edit @history = @audit.histories.new @pagy, @histories = pagy(@audit.histories.availables.order('id DESC').limit(5), link_extra: "data-turbo-frame='audits'") end |
#index ⇒ Object
GET /users/:user_id/audits
render autits index
19 20 21 |
# File 'app/controllers/audits_controller.rb', line 19 def index @categories = Category.all end |
#list ⇒ Object
28 29 30 31 |
# File 'app/controllers/audits_controller.rb', line 28 def list categories = Category.all @pagy, @categories = pagy(categories, link_extra: "data-turbo-frame='categories'") end |
#update ⇒ Object
PATCH /user/:user_id/audits/:id
Try to update the history status
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/controllers/audits_controller.rb', line 83 def update if @audit.update(audit_params) @pagy, @histories = pagy(@audit.histories.availables.order('id DESC').limit(5), link_extra: "data-turbo-frame='audits'") @categories = Category.all @history = @audit.histories.new flash.now[:success] = 'Aggiornamento avvenuto con successo' render :edit, status: :ok else @history = @audit.histories.new flash.now[:error] = @audit.errors.map { |k, v| "#{I18n.t k} #{v}" }.join(', ') render :edit end end |