Class: SamplesController

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

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

#accessionObject


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/samples_controller.rb', line 134

def accession
  @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


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

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


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/samples_controller.rb', line 22

def create
  @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


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

def edit
  @sample = Sample.find(params[:id])
  redirect_if_not_owner_or_admin_otherwise do
    if @sample.released? && !current_user.is_administrator?
      flash[:error] = 'Cannot edit publically 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
end

#historyObject


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

def history
  @sample = Sample.find(params[:id])
  respond_to do |format|
    format.html
  end
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


17
18
19
20
# File 'app/controllers/samples_controller.rb', line 17

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

#releaseObject


57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/samples_controller.rb', line 57

def release
  @sample = Sample.find(params[:id])
  redirect_if_not_owner_or_admin_otherwise do
    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
end

#remove_from_studyObject


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

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


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

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


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

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


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/samples_controller.rb', line 155

def taxon_lookup
  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 = ''
  elsif not configatron.proxy.blank?
    RestClient.proxy = configatron.proxy
    rc.headers['User-Agent'] = 'Internet Explorer 5.0'
  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


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

def update
  @sample = Sample.find(params[:id])
  redirect_if_not_owner_or_admin_otherwise do
    cleaned_params = clean_params_from_check(params[:sample]).permit()
    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
end