Class: AuditsController

Inherits:
ApplicationController show all
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

Methods inherited from ApplicationController

#admin_in!, #doctor_in!, #powered_in!, #secretary_in!, #translate_errors

Instance Method Details

#createObject

POST /users/user_id/audits

Try to make a new audit.

  • set @audit as new audit

otherwise status :500

Returns:

  • (Object)

    if audit is created render audits/category



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

#destroyObject

DELETE /user/:user_id/audits/:id

set the audit with status deleted

  • set @audit searching an Audit with id = params

  • set @audit.histories_attributes with status deleted

Returns:

  • (Object)

    render audits/category



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

#editObject

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

Returns:

  • (Object)

    render audits/edit



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

#indexObject

GET /users/:user_id/audits

render autits index

Returns:

  • (Object)

    audits/index.haml



19
20
21
# File 'app/controllers/audits_controller.rb', line 19

def index
  @categories = Category.all
end

#listObject

GET /audits/list

render the list the all Audit

  • set @pagy, @audits for the @users pagination

Returns:

  • (Object)

    render users/index



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

#updateObject

PATCH /user/:user_id/audits/:id

Try to update the history status

  • set @audit from #set_audit and try to update this with #audit_params as param

  • set @page, @histories with @audit.histories pagination if @audit is updated

  • set @category with all Category if @audit is updated

  • set @history with a new @audit History

Returns:

  • (Object)

    render audits/edit



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