Class: StudiesController

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

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, #clean_params_from_check, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!

Class Method Details

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


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/controllers/studies_controller.rb', line 267

def self.role_helper(name, success_action, error_action, &block)
  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


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

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


232
233
234
235
236
237
# File 'app/controllers/studies_controller.rb', line 232

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


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

def close
  @study = Study.find(params[:id])
  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


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

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.


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/studies_controller.rb', line 57

def create
  ActiveRecord::Base.transaction do
    @study = Study.new(params['study'].merge(user: current_user))
    @study.save!
    current_user.has_role('manager', @study)
    User.find(params[:study_owner_id]).has_role('owner', @study) unless params[:study_owner_id].blank?
  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


239
240
241
242
243
244
245
246
247
# File 'app/controllers/studies_controller.rb', line 239

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


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

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

#followObject


151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/studies_controller.rb', line 151

def follow
  @study = Study.find(params[:id])
  if current_user.has_role? 'follower', @study
    current_user.has_no_role 'follower', @study
    flash[:notice] = "You have stopped following the '#{@study.name}' study."
  else
    current_user.has_role 'follower', @study
    flash[:notice] = "You are now following the '#{@study.name}' study."
  end
  redirect_to study_information_path(@study)
end

#indexObject


31
32
33
34
35
36
37
38
39
# File 'app/controllers/studies_controller.rb', line 31

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


48
49
50
51
52
53
# File 'app/controllers/studies_controller.rb', line 48

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

#openObject


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

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

#policy_accessionObject


249
250
251
252
253
254
255
256
257
# File 'app/controllers/studies_controller.rb', line 249

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


289
290
291
292
# File 'app/controllers/studies_controller.rb', line 289

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

#propertiesObject


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

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


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/controllers/studies_controller.rb', line 205

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


294
295
296
297
# File 'app/controllers/studies_controller.rb', line 294

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


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/studies_controller.rb', line 16

def setup_studies_from_scope(exclude_nested_resource = false)
  if logged_in? and 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


80
81
82
83
84
85
86
87
88
# File 'app/controllers/studies_controller.rb', line 80

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


181
182
183
184
185
186
187
# File 'app/controllers/studies_controller.rb', line 181

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


197
198
199
200
201
202
203
# File 'app/controllers/studies_controller.rb', line 197

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


189
190
191
192
193
194
195
# File 'app/controllers/studies_controller.rb', line 189

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


259
260
261
# File 'app/controllers/studies_controller.rb', line 259

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

#stateObject


263
264
265
# File 'app/controllers/studies_controller.rb', line 263

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

#study_listObject


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

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


304
305
306
307
# File 'app/controllers/studies_controller.rb', line 304

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

#study_statusObject


121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/studies_controller.rb', line 121

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

  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


299
300
301
302
# File 'app/controllers/studies_controller.rb', line 299

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

#updateObject


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/studies_controller.rb', line 97

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

  ActiveRecord::Base.transaction do
    @study.update!(params[:study])
    unless params[:study_owner_id].blank?
      owner = User.find(params[:study_owner_id])
      unless owner.is_owner?(@study)
        @study.owners.first.has_no_role('owner', @study) if @study.owners.size == 1
        owner.has_role('owner', @study)
      end
    end

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

    redirect_to study_path(@study)
  end
rescue ActiveRecord::RecordInvalid => e
  Rails.logger.warn "Failed to update attributes: #{@study.errors.map { |e| e.to_s }}"
  flash.now[:error] = 'Failed to update attributes for study!'
  render action: 'edit', id: @study.id
end