Class: RequestsController

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

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!

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters


13
14
15
# File 'app/controllers/requests_controller.rb', line 13

def parameters
  @parameters
end

Instance Method Details

#additionalObject


101
102
103
104
105
# File 'app/controllers/requests_controller.rb', line 101

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


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/requests_controller.rb', line 118

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


184
185
186
187
188
189
190
191
192
# File 'app/controllers/requests_controller.rb', line 184

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


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

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

#editObject


46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/requests_controller.rb', line 46

def edit
  @request = Request.find(params[:id])
  @request_types = RequestType.where(asset_type: @request.request_type.asset_type)
  if current_user.is_administrator?
    respond_to do |format|
      format.html
    end
  else
    flash[:error] = "You cannot update a request unless you're an administrator"
    redirect_to request_path(@request)
  end
end

#filter_change_decisionObject


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

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

#find_requestObject


173
174
175
# File 'app/controllers/requests_controller.rb', line 173

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

#historyObject

Displays history of events


133
134
135
136
137
138
139
140
# File 'app/controllers/requests_controller.rb', line 133

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 ]


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
43
44
# File 'app/controllers/requests_controller.rb', line 16

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] and 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
    format.xml { render xml: Request.all.to_xml }
  end
end

#list_inboxesObject


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

def list_inboxes
  @tasks = Task.all
end

#redirect_if_not_owner_or_adminObject


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

def redirect_if_not_owner_or_admin
  unless current_user == @request.user or current_user.is_administrator? or current_user.is_manager?
    flash[:error] = 'Request details can only be altered by the owner or a manager'
    redirect_to request_path(@request)
    return true
  end
  false
end

#resetObject


107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/requests_controller.rb', line 107

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


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

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


194
195
196
197
198
# File 'app/controllers/requests_controller.rb', line 194

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


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

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

#showObject


89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/requests_controller.rb', line 89

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

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

#updateObject


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/requests_controller.rb', line 59

def update
  @request = Request.find(params[:id])
  if redirect_if_not_owner_or_admin
    return
  end

  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