Class: StudiesController

Inherits:
ApplicationController show all
Includes:
Informatics::Globals, REXML
Defined in:
app/controllers/studies_controller.rb

Overview

rubocop:todo Metrics/ClassLength

Constant Summary

Constants included from FlashTruncation

FlashTruncation::STRING_OVERHEAD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Informatics::Globals

#application, #application=, #defaults, #defaults=, #global_searchable_classes, #search_options

Methods inherited from ApplicationController

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

Methods included from FlashTruncation

#max_flash_size, #truncate_flash, #truncate_flash_array

Class Method Details

.role_helper(name, success_action, error_action, &block) ⇒ Object

rubocop:todo Metrics/MethodLength


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/controllers/studies_controller.rb', line 283

def self.role_helper(name, success_action, error_action, &block) # rubocop:todo Metrics/AbcSize
  define_method("#{name}_role") do
    ActiveRecord::Base.transaction do
      @study = Study.find(params[:id])
      @user = User.find(params.require(:role).fetch(:user))

      if request.xhr?
        block.call(@user, @study, params[:role][:authorizable_type].to_s)
        status, flash.now[:notice] = 200, "Role #{success_action}"
      else
        status, flash.now[:error] = 401, "A problem occurred while #{error_action} the role"
      end

      @roles = @study.roles.reload
      render partial: 'roles', status: status
    end
  end
end

Instance Method Details

#accessionObject

rubocop:enable Metrics/MethodLength


236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/studies_controller.rb', line 236

def accession
  rescue_accession_errors do
    @study = Study.find(params[:id])
    @study.validate_ena_required_fields!
    @study.accession_service.submit_study_for_user(@study, current_user)

    flash[:notice] = "Accession number generated: #{@study.ebi_accession_number}"
    redirect_to(study_path(@study))
  end
end

#accession_all_samplesObject


247
248
249
250
251
252
# File 'app/controllers/studies_controller.rb', line 247

def accession_all_samples
  @study = Study.find(params[:id])
  @study.accession_all_samples
  flash[:notice] = 'All of the samples in this study have been sent for accessioning.'
  redirect_to(study_path(@study))
end

#closeObject


173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/studies_controller.rb', line 173

def close
  @study = Study.find(params[:id])
  authorize! :activate, @study
  comment = params[:comment]
  @study.comments.create(description: comment, user_id: current_user.id)
  @study.deactivate!
  @study.save
  flash[:notice] = "This study has been deactivated: #{comment}"
  redirect_to study_path(@study)
end

#collaboratorsObject


154
155
156
157
158
159
# File 'app/controllers/studies_controller.rb', line 154

def collaborators
  @study = Study.find(params[:id])
  @all_roles = Role.distinct.pluck(:name)
  @roles = Role.where(authorizable_id: @study.id, authorizable_type: 'Study')
  @users = User.order(:first_name)
end

#createObject

Create the Study from new with the details from its form. Redirect to the index page with a notice.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/studies_controller.rb', line 65

def create # rubocop:todo Metrics/AbcSize
  ActiveRecord::Base.transaction do
    @study = Study.new(params['study'].merge(user: current_user))
    @study.save!
    current_user.grant_manager(@study)
    User.find(params[:study_owner_id]).grant_owner(@study) if params[:study_owner_id].present?
  end

  flash[:notice] = 'Your study has been created'
  respond_to do |format|
    format.html { redirect_to study_path(@study) }
    format.xml { render xml: @study, status: :created, location: @study }
    format.json { render json: @study, status: :created, location: @study }
  end
rescue ActiveRecord::RecordInvalid => e
  flash.now[:error] = 'Problems creating your new study'
  respond_to do |format|
    format.html { render action: 'new' }
    format.xml { render xml: @study.errors, status: :unprocessable_entity }
    format.json { render json: @study.errors, status: :unprocessable_entity }
  end
end

#dac_accessionObject


254
255
256
257
258
259
260
261
262
# File 'app/controllers/studies_controller.rb', line 254

def dac_accession
  rescue_accession_errors do
    @study = Study.find(params[:id])
    @study.accession_service.submit_dac_for_user(@study, current_user)

    flash[:notice] = "Accession number generated: #{@study.dac_accession_number}"
    redirect_to(study_path(@study))
  end
end

#editObject


98
99
100
101
102
# File 'app/controllers/studies_controller.rb', line 98

def edit
  @study = Study.find(params[:id])
  flash.now[:warning] = @study.warnings if @study.warnings.present?
  @users = User.all
end

#followObject

rubocop:todo Metrics/AbcSize


161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/studies_controller.rb', line 161

def follow # rubocop:todo Metrics/AbcSize
  @study = Study.find(params[:id])
  if current_user.follower_of?(@study)
    current_user.remove_role 'follower', @study
    flash[:notice] = "You have stopped following the '#{@study.name}' study."
  else
    current_user.grant_follower(@study)
    flash[:notice] = "You are now following the '#{@study.name}' study."
  end
  redirect_to study_information_path(@study)
end

#indexObject


41
42
43
44
45
46
47
48
49
# File 'app/controllers/studies_controller.rb', line 41

def index
  # Please do not user current_user outside this block, you kill the API calls
  setup_studies_from_scope(@exclude_nested_resource)
  respond_to do |format|
    format.html
    format.xml { render(action: (@exclude_nested_resource ? 'index' : 'index_deprecated_xml')) }
    format.json { render json: Study.all.to_json }
  end
end

#newObject


58
59
60
61
# File 'app/controllers/studies_controller.rb', line 58

def new
  @study = Study.new
  respond_to { |format| format.html }
end

#openObject


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

def open
  @study = Study.find(params[:id])
  authorize! :activate, @study
  @study.activate!
  @study.save
  flash[:notice] = 'This study has been activated'
  redirect_to study_path(@study)
end

#policy_accessionObject


264
265
266
267
268
269
270
271
272
# File 'app/controllers/studies_controller.rb', line 264

def policy_accession
  rescue_accession_errors do
    @study = Study.find(params[:id])
    @study.accession_service.submit_policy_for_user(@study, current_user)

    flash[:notice] = "Accession number generated: #{@study.policy_accession_number}"
    redirect_to(study_path(@study))
  end
end

#projectsObject


307
308
309
310
# File 'app/controllers/studies_controller.rb', line 307

def projects
  @study = Study.find(params[:id])
  @projects = @study.projects.page(params[:page])
end

#propertiesObject


144
145
146
147
148
149
150
151
152
# File 'app/controllers/studies_controller.rb', line 144

def properties
  @study = Study.find(params[:id])

  respond_to do |format|
    format.html
    format.xml
    format.json { render json: @study.to_json }
  end
end

#rescue_accession_errorsObject

rubocop:todo Metrics/MethodLength


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/controllers/studies_controller.rb', line 218

def rescue_accession_errors # rubocop:todo Metrics/AbcSize
  yield
rescue ActiveRecord::RecordInvalid => e
  flash[:error] = 'Please fill in the required fields'
  render(action: :edit)
rescue AccessionService::NumberNotRequired => e
  flash[:warning] = e.message || 'An accession number is not required for this study'
  redirect_to(study_path(@study))
rescue AccessionService::NumberNotGenerated => e
  flash[:warning] = 'No accession number was generated'
  redirect_to(study_path(@study))
rescue AccessionService::AccessionServiceError => e
  flash[:error] = e.message
  redirect_to(edit_study_path(@study))
end

#sample_manifestsObject


312
313
314
315
# File 'app/controllers/studies_controller.rb', line 312

def sample_manifests
  @study = Study.find(params[:id])
  @sample_manifests = @study.sample_manifests.page(params[:page]).order(id: :desc)
end

#setup_studies_from_scope(exclude_nested_resource = false) ⇒ Object

rubocop:todo Metrics/AbcSize


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

def setup_studies_from_scope(exclude_nested_resource = false) # rubocop:todo Metrics/AbcSize
  if logged_in? && (not exclude_nested_resource)
    @alternatives = [
      'interesting',
      'followed',
      'managed & active',
      'managed & inactive',
      'pending',
      'pending ethical approval',
      'contaminated with human dna',
      'remove x and autosomes',
      'active',
      'inactive',
      'collaborations',
      'all'
    ]
    @studies = studies_from_scope(@alternatives[params[:scope].to_i])
  elsif params[:project_id] && !(project = Project.find(params[:project_id])).nil?
    @studies = project.studies.newest_first.includes(:user, :roles)
  else
    @studies = Study.newest_first.with_user_included.with_related_users_included
  end
end

#showObject


88
89
90
91
92
93
94
95
96
# File 'app/controllers/studies_controller.rb', line 88

def show
  @study = Study.find(params[:id])
  flash.keep
  respond_to do |format|
    format.html { redirect_to study_information_path(@study) }
    format.xml { render layout: false }
    format.json { render json: @study.to_json }
  end
end

#show_accessionObject


193
194
195
196
197
198
199
# File 'app/controllers/studies_controller.rb', line 193

def show_accession
  @study = Study.find(params[:id])
  respond_to do |format|
    xml_text = @study.accession_service.accession_study_xml(@study)
    format.xml { render(xml: xml_text) }
  end
end

#show_dac_accessionObject


209
210
211
212
213
214
215
# File 'app/controllers/studies_controller.rb', line 209

def show_dac_accession
  @study = Study.find(params[:id])
  respond_to do |format|
    xml_text = @study.accession_service.accession_dac_xml(@study)
    format.xml { render(xml: xml_text) }
  end
end

#show_policy_accessionObject


201
202
203
204
205
206
207
# File 'app/controllers/studies_controller.rb', line 201

def show_policy_accession
  @study = Study.find(params[:id])
  respond_to do |format|
    xml_text = @study.accession_service.accession_policy_xml(@study)
    format.xml { render(xml: xml_text) }
  end
end

#sraObject


274
275
276
# File 'app/controllers/studies_controller.rb', line 274

def sra
  @study = Study.find(params[:id])
end

#stateObject


278
279
280
# File 'app/controllers/studies_controller.rb', line 278

def state
  @study = Study.find(params[:id])
end

#study_listObject


51
52
53
54
55
56
# File 'app/controllers/studies_controller.rb', line 51

def study_list
  return redirect_to(studies_path) unless request.xhr?

  setup_studies_from_scope
  render partial: 'study_list', locals: { studies: @studies.with_related_owners_included }
end

#study_reportsObject


322
323
324
325
# File 'app/controllers/studies_controller.rb', line 322

def study_reports
  @study = Study.find(params[:id])
  @study_reports = StudyReport.for_study(@study).page(params[:page]).order(id: :desc)
end

#study_statusObject

rubocop:enable Metrics/MethodLength


131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/studies_controller.rb', line 131

def study_status
  @study = Study.find(params[:id])
  authorize! :activate, @study

  if @study.inactive? || @study.pending?
    @study.activate!
  elsif @study.active?
    @study.deactivate!
  end
  flash[:notice] = 'Study status was updated successfully'
  redirect_to study_path(@study)
end

#suppliersObject


317
318
319
320
# File 'app/controllers/studies_controller.rb', line 317

def suppliers
  @study = Study.find(params[:id])
  @suppliers = @study.suppliers.page(params[:page])
end

#updateObject

rubocop:todo Metrics/MethodLength


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/studies_controller.rb', line 105

def update # rubocop:todo Metrics/AbcSize
  @study = Study.find(params[:id])

  ActiveRecord::Base.transaction do
    @study.update!(params[:study])
    if params[:study_owner_id].present?
      owner = User.find(params[:study_owner_id])
      unless owner.owner_of?(@study)
        @study.owners.first.remove_role('owner', @study) if @study.owners.size == 1
        owner.grant_owner(@study)
      end
    end

    flash[:notice] = 'Your study has been updated'

    redirect_to study_path(@study)
  end
rescue ActiveRecord::RecordInvalid => e
  # don't use @study.errors.map(&:to_s) because it throws an exception when within a rescue block
  Rails.logger.warn "Failed to update attributes: #{@study.errors.map { |error| error.to_s }}" # rubocop:disable Style/SymbolProc
  flash.now[:error] = 'Failed to update attributes for study!'
  render action: 'edit', id: @study.id
end