Class: RequestsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/requests_controller.rb

Overview

rubocop:todo Style/Documentation

Constant Summary

Constants included from FlashTruncation

FlashTruncation::STRING_OVERHEAD

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationController

#block_api_access, #clean_params_from_check, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!

Methods included from FlashTruncation

#max_flash_size, #truncate_flash, #truncate_flash_array

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters


11
12
13
# File 'app/controllers/requests_controller.rb', line 11

def parameters
  @parameters
end

Instance Method Details

#additionalObject


94
95
96
97
98
# File 'app/controllers/requests_controller.rb', line 94

def additional
  @request    = Request.find(params[:id])
  @additional = @request.request_type.create!(initial_study: @request.study, items: @request.items)
  redirect_to request_path(@additional)
end

#cancelObject


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/requests_controller.rb', line 111

def cancel
  @request = Request.find(params[:id])
  if @request.try(:may_cancel_before_started?)
    if @request.cancel_before_started && @request.save
      flash[:notice] = "Request #{@request.id} has been cancelled"
    else
      flash[:error] = "Failed to cancel request #{@request.id}"
    end
  else
    flash[:error] = "Request #{@request.id} can't be cancelled"
  end
  redirect_to request_path(@request)
end

#change_decisionObject


168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/requests_controller.rb', line 168

def change_decision
  @change_decision = Request::ChangeDecision.new({ request: @request,
                                                   user: @current_user }.merge(params[:change_decision] || {})).execute!
  flash[:notice] = 'Update. Below you find the new situation.'
  redirect_to filter_change_decision_request_path(params[:id])
rescue Request::ChangeDecision::InvalidDecision => e
  flash[:error] = 'Failed! Please, read the list of problem below.'
  @change_decision = e.object
  render(action: :filter_change_decision)
end

#copyObject


139
140
141
142
143
144
# File 'app/controllers/requests_controller.rb', line 139

def copy
  old_request = Request.find(params[:id])
  new_request = old_request.copy
  flash[:notice] = "Created request #{new_request.id}"
  redirect_to receptacle_path(new_request.asset)
end

#editObject


44
45
46
47
48
49
50
51
52
# File 'app/controllers/requests_controller.rb', line 44

def edit
  @request = Request.find(params[:id])
  authorize! :update, @request

  @request_types = RequestType.where(asset_type: @request.request_type.asset_type)
  respond_to do |format|
    format.html
  end
end

#filter_change_decisionObject


161
162
163
164
165
166
# File 'app/controllers/requests_controller.rb', line 161

def filter_change_decision
  @change_decision = Request::ChangeDecision.new(request: @request, user: @current_user)
  respond_to do |format|
    format.html
  end
end

#find_requestObject


157
158
159
# File 'app/controllers/requests_controller.rb', line 157

def find_request
  @request = Request.find(params[:id])
end

#historyObject

Displays history of events


126
127
128
129
130
131
132
133
# File 'app/controllers/requests_controller.rb', line 126

def history
  @request = Request.find(params[:id])
  respond_to do |format|
    format.html
    format.xml  { @request.events.to_xml }
    format.json { @request.events.to_json }
  end
end

#indexObject

before_action :find_request_from_id, :only => [ :filter_change_decision, :change_decision ]


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/requests_controller.rb', line 15

def index
  @study, @item = nil, nil

  # Ok, here we pick the initial source for the Requests.  They either come from Request (as in all Requests), or they
  # are limited by the Asset / Item.
  request_source = Request.includes(:request_type, :initial_study, :user, :events, asset: :barcodes)
                          .order(id: :desc)
                          .where(search_params)
                          .paginate(per_page: 200, page: params[:page])

  @asset        = Receptacle.find(params[:asset_id]) if params[:asset_id]
  @request_type = RequestType.find(params[:request_type_id]) if params[:request_type_id]
  @study        = Study.find(params[:study_id]) if params[:study_id]

  @subtitle = (@study&.name || @asset&.display_name)

  # Deprecated?: It would be great if we could remove this
  if params[:request_type] && params[:workflow]
    request_source = request_source.for_request_types(params[:request_type]).includes(:user)
  end

  # Now, here we go: find all of the requests!
  @requests = request_source

  respond_to do |format|
    format.html
  end
end

#list_inboxesObject


135
136
137
# File 'app/controllers/requests_controller.rb', line 135

def list_inboxes
  @tasks = Task.all
end

#resetObject


100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/requests_controller.rb', line 100

def reset
  @request = Request.find(params[:id])
  @request.reset!
  flash[:notice] = "Request #{@request.id} was reset successfully"
  if params[:study_id]
    redirect_to study_requests_path(params[:study_id])
  else
    redirect_to requests_path
  end
end

#reset_qc_informationObject


146
147
148
149
150
151
152
153
# File 'app/controllers/requests_controller.rb', line 146

def reset_qc_information
  @request = Request.find(params[:id])
  @request.reset!
  @event = Event.find(params[:event_id])
  flash[:notice] = "QC event #{@event.id} has been deleted"
  @event.destroy
  redirect_to request_path(@request)
end

#search_paramsObject


179
180
181
182
183
# File 'app/controllers/requests_controller.rb', line 179

def search_params
  permitted = params.permit(:asset_id, :state, :request_type_id, :submission_id)
  permitted[:initial_study_id] = params[:study_id] if params[:study_id]
  permitted
end

#set_permitted_paramsObject


8
9
10
# File 'app/controllers/requests_controller.rb', line 8

def set_permitted_params
  @parameters = params[:request].reject { |k, _v| ['request_metadata_attributes'].exclude?(k.to_s) }
end

#showObject


82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/requests_controller.rb', line 82

def show
  @request = Request.find(params[:id])
  if @request.user_id.present?
    @user = User.find(@request.user_id)
  end

  respond_to do |format|
    format.html
    format.xml
  end
end

#updateObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/requests_controller.rb', line 54

def update
  @request = Request.find(params[:id])
  authorize! :update, @request

  unless params[:request][:request_type_id].nil?
    unless @request.request_type_updatable?(params[:request][:request_type_id])
      flash[:error] = 'You can not change the request type.'
      redirect_to request_path(@request)
      return
    end
  end

  begin
    if @request.update(parameters)
      flash[:notice] = 'Request details have been updated'
      redirect_to request_path(@request)
    else
      flash[:error] = 'Request was not updated. No change specified ?'
      render action: 'edit', id: @request.id
    end
  rescue => e
    error_message = "An error has occurred, category:'#{e.class}'\ndescription:'#{e.message}'"
    EventFactory.request_update_note_to_manager(@request, current_user, error_message)
    flash[:error] = 'Failed to update request. ' << error_message
    render action: 'edit', id: @request.id
  end
end