Class: SamplesController

Inherits:
ApplicationController show all
Includes:
AccessionHelper
Defined in:
app/controllers/samples_controller.rb

Overview

rubocop:todo Metrics/ClassLength

Constant Summary

Constants included from FlashTruncation

FlashTruncation::STRING_OVERHEAD

Instance Method Summary collapse

Methods included from AccessionHelper

#accessioning_enabled?, #permitted_to_accession?

Methods inherited from ApplicationController

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

Methods included from AuthenticatedSystem

included

Methods included from FlashTruncation

#max_flash_size, #truncate_flash, #truncate_flash_array

Instance Method Details

#accessionObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/samples_controller.rb', line 152

def accession # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  # @sample needs to be set before initially for use in the ensure block
  @sample = Sample.find(params[:id])

  unless accessioning_enabled?
    flash[:error] = 'Accessioning is not enabled in this environment'
    redirect_to sample_path(@sample)
    return
  end
  # TODO: Y26-026 - Enforce accessioning permissions
  # unless permitted_to_accession?(@sample)
  #   flash[:error] = 'Permission required to accession this sample'
  #   redirect_to sample_path(@sample)
  #   return
  # end

  # Must check if an accession number is assigned _before_ performing the accession
  accession_action = @sample.accession_number? ? :update : :create

  # Synchronously perform accessioning job
  Accession.accession_sample(@sample, current_user, perform_now: true)

  if accession_action == :create
    flash[:notice] = "Accession number generated: #{@sample..sample_ebi_accession_number}"
  elsif accession_action == :update
    flash[:notice] = 'Accessioned metadata updated'
  end

  # Handle errors for both synchronous and asynchronous accessioning
rescue Accession::InternalValidationError
  flash[:error] = "Please fill in the required fields: #{@sample.errors.full_messages.join(', ')}"
  redirect_to(edit_sample_path(@sample)) # send the user to edit the sample
rescue Accession::ExternalValidationError => e
  flash[:warning] = "No accession number was generated: #{e.message}"
rescue Accession::Error => e
  flash[:error] = "Accessioning Service Failed: #{e.message}"
rescue Faraday::Error => e
  flash[:error] = "Accessioning failed with a network error: #{e.message}"
ensure
  # Redirect back to where we came from if not already redirected
  redirect_back_with_anchor_or_to(sample_path(@sample)) unless performed?
end

#add_to_studyObject

rubocop:todo Metrics/AbcSize



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

def add_to_study # rubocop:todo Metrics/AbcSize
  sample = Sample.find(params[:id])
  study = Study.find(params[:study][:id])
  study.samples << sample
  redirect_to sample_path(sample)
rescue ActiveRecord::RecordInvalid => e
  flash[:error] = e.record.errors.full_messages
  redirect_to sample_path(sample)
end

#createObject

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



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

def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  @sample = Sample.new(params[:sample])

  study_id = params[:study_id]
  if study_id
    study = Study.find(study_id)
    study.samples << @sample
  end

  respond_to do |format|
    @sample.current_user = current_user
    if @sample.save
      flash[:notice] = 'Sample successfully created'
      format.html { redirect_to sample_path(@sample) }
      format.xml { render xml: @sample, status: :created, location: @sample }
      format.json { render json: @sample, status: :created, location: @sample }
    else
      flash[:error] = 'Problems creating your new sample'
      format.html { render action: :new }
      format.xml { render xml: @sample.errors, status: :unprocessable_entity }
      format.json { render json: @sample.errors, status: :unprocessable_entity }
    end
  end
end

#editObject

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/samples_controller.rb', line 38

def edit # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  @sample = Sample.find(params[:id])
  authorize! :update, @sample

  if @sample.released? && cannot?(:update_released, @sample)
    flash[:error] = 'Cannot edit publicly released sample'
    redirect_to sample_path(@sample)
    return
  end

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

#historyObject

rubocop:enable Metrics/MethodLength



120
121
122
123
# File 'app/controllers/samples_controller.rb', line 120

def history
  @sample = Sample.find(params[:id])
  respond_to { |format| format.html }
end

#indexObject



11
12
13
14
15
16
17
18
# File 'app/controllers/samples_controller.rb', line 11

def index
  @samples = Sample.order(created_at: :desc).page(params[:page])
  respond_to do |format|
    format.html
    format.xml
    format.json { render json: Sample.all.to_json }
  end
end

#newObject



33
34
35
36
# File 'app/controllers/samples_controller.rb', line 33

def new
  @sample = Sample.new
  @studies = Study.alphabetical
end

#releaseObject



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

def release
  @sample = Sample.find(params[:id])
  authorize! :release, @sample

  if @sample.released?
    flash[:notice] = "Sample '#{@sample.name}' already publically released"
  else
    @sample.release
    flash[:notice] = "Sample '#{@sample.name}' publically released"
  end
  redirect_to sample_path(@sample)
end

#remove_from_studyObject

rubocop:todo Metrics/AbcSize



135
136
137
138
139
140
141
# File 'app/controllers/samples_controller.rb', line 135

def remove_from_study # rubocop:todo Metrics/AbcSize
  study = Study.find(params[:study_id])
  sample = Sample.find(params[:id])
  StudySample.find_by(study_id: params[:study_id], sample_id: params[:id]).destroy
  flash[:notice] = "Sample was removed from study #{study.name.humanize}"
  redirect_to sample_path(sample)
end

#showObject

rubocop:disable Metrics/AbcSize



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/samples_controller.rb', line 20

def show # rubocop:disable Metrics/AbcSize
  @sample = Sample.includes(:assets, :studies).find(params[:id])
  @studies = Study.where(state: %w[pending active]).alphabetical
  @page_name = @sample.name
  @component_samples = @sample.component_samples.paginate({ page: params[:page], per_page: 25 })

  respond_to do |format|
    format.html
    format.xml { render layout: false }
    format.json { render json: @sample.to_json }
  end
end

#show_accessionObject



143
144
145
146
147
148
149
150
# File 'app/controllers/samples_controller.rb', line 143

def show_accession
  @sample = Sample.find(params[:id])
  respond_to do |format|
    accession_service = AccessionService.select_for_sample(@sample)
    xml_text = accession_service.accession_sample_xml(@sample)
    format.xml { render(xml: xml_text) }
  end
end

#updateObject

rubocop:todo Metrics/MethodLength



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/samples_controller.rb', line 94

def update # rubocop:todo Metrics/AbcSize
  @sample = Sample.find(params[:id])
  @sample.current_user = current_user
  authorize! :update, @sample

  cleaned_params = params[:sample].permit()

  # if consent is being withdrawn and wasn't previously, set a couple of fields
  if (cleaned_params[:sample_metadata_attributes][:consent_withdrawn] == 'true') && !@sample.consent_withdrawn
    cleaned_params[:date_of_consent_withdrawn] = DateTime.now
    cleaned_params[:user_id_of_consent_withdrawn] = current_user.id
  end

  if @sample.update(cleaned_params)
    flash[:notice] = 'Sample details have been updated'
    flash[:warning] = @sample.errors.full_messages if @sample.errors.present? # also shows warnings from accessioning
    redirect_to sample_path(@sample)
  else
    flash[:error] = 'Failed to update attributes for sample'
    flash[:warning] = @sample.errors.full_messages if @sample.errors.present?
    redirect_to edit_sample_path(@sample)
  end
end