Class: SamplesController

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

Instance Method Details

#accessionObject

rubocop:todo Metrics/MethodLength



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/samples_controller.rb', line 141

def accession # rubocop:todo Metrics/AbcSize
  @sample = Sample.find(params[:id])
  @sample.validate_ena_required_fields!
  @sample.accession_service.submit_sample_for_user(@sample, current_user)

  flash[:notice] = "Accession number generated: #{@sample..sample_ebi_accession_number}"
  redirect_to(sample_path(@sample))
rescue ActiveRecord::RecordInvalid => e
  flash[:error] = "Please fill in the required fields: #{@sample.errors.full_messages.join(', ')}"
  redirect_to(edit_sample_path(@sample))
rescue AccessionService::NumberNotRequired => e
  flash[:warning] = e.message || 'An accession number is not required for this study'
  redirect_to(sample_path(@sample))
rescue AccessionService::NumberNotGenerated => e
  flash[:warning] = "No accession number was generated: #{e.message}"
  redirect_to(sample_path(@sample))
rescue AccessionService::AccessionServiceError => e
  flash[:error] = e.message
  redirect_to(sample_path(@sample))
end

#add_to_studyObject

rubocop:todo Metrics/AbcSize



114
115
116
117
118
119
120
121
122
# File 'app/controllers/samples_controller.rb', line 114

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/samples_controller.rb', line 48

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|
    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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/samples_controller.rb', line 32

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



109
110
111
112
# File 'app/controllers/samples_controller.rb', line 109

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

#indexObject



8
9
10
11
12
13
14
15
# File 'app/controllers/samples_controller.rb', line 8

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



27
28
29
30
# File 'app/controllers/samples_controller.rb', line 27

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

#releaseObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/samples_controller.rb', line 72

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



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

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



17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/samples_controller.rb', line 17

def show
  @sample = Sample.includes(:assets, :studies).find(params[:id])
  @studies = Study.where(state: %w[pending active]).alphabetical

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

#show_accessionObject



132
133
134
135
136
137
138
# File 'app/controllers/samples_controller.rb', line 132

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

#taxon_lookupObject

rubocop:todo Metrics/MethodLength



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
# File 'app/controllers/samples_controller.rb', line 165

def taxon_lookup # rubocop:todo Metrics/AbcSize
  if params[:term]
    url = configatron.taxon_lookup_url + "/esearch.fcgi?db=taxonomy&term=#{params[:term].gsub(/\s/, '_')}"
  elsif params[:id]
    url = configatron.taxon_lookup_url + "/efetch.fcgi?db=taxonomy&mode=xml&id=#{params[:id]}"
  else
    return
  end

  rc = RestClient::Resource.new(URI.parse(url).to_s)
  if configatron.disable_web_proxy == true
    RestClient.proxy = nil
  elsif configatron.fetch(:proxy).present?
    RestClient.proxy = configatron.proxy
    rc.headers['User-Agent'] = 'Internet Explorer 5.0'
  elsif ENV['http_proxy'].present?
    RestClient.proxy = ENV['http_proxy']
  end

  # rc.verbose = true
  body = rc.get.body

  respond_to do |format|
    format.js { render plain: body }
    format.xml { render plain: body }
    #      format.html {render :nothing}
  end
end

#updateObject

rubocop:todo Metrics/MethodLength



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/samples_controller.rb', line 86

def update # rubocop:todo Metrics/AbcSize
  @sample = Sample.find(params[:id])
  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'
    redirect_to sample_path(@sample)
  else
    flash[:error] = 'Failed to update attributes for sample'
    render action: 'edit', id: @sample.id
  end
end