Class: Admin::StudiesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/studies_controller.rb

Overview

rubocop:todo Style/Documentation

Instance Method Summary collapse

Instance Method Details

#editObject


24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/admin/studies_controller.rb', line 24

def edit
  @request_types = RequestType.order(name: :asc)
  if params[:id] != '0'
    @study = Study.find(params[:id])
    flash.now[:warning] = @study.warnings if @study.warnings.present?
    render partial: 'edit', locals: { study: @study }
  else
    render nothing: true
  end
end

#filterObject

TODO: remove unneeded code rubocop:todo Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/admin/studies_controller.rb', line 37

def filter # rubocop:todo Metrics/CyclomaticComplexity
  filter_conditions = { approved: false } if params[:filter][:by] == 'not approved' unless params[:filter].nil?

  if params[:filter][:by] == 'not approved' || params[:filter][:by] == 'all'
    @studies = Study.where(filter_conditions).alphabetical.select { |p| p.name.include? params[:q] }
  end

  unless params[:filter].nil?
    if params[:filter][:by] == 'unallocated manager'
      @studies = Study.all.select { |p| p.name.include?(params[:q]) && !(p.roles.map(&:name).include?('manager')) }
    end
  end

  case params[:filter][:status]
  when 'open'
    @studies = @studies.select(&:active?)
  when 'closed'
    @studies = @studies.reject(&:active?)
  end
  @request_types = RequestType.order(:name)
  render partial: 'filtered_studies'
end

#indexObject


8
9
10
# File 'app/controllers/admin/studies_controller.rb', line 8

def index
  @studies = Study.alphabetical
end

#managed_updateObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/admin/studies_controller.rb', line 62

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

  if params[:study][:uploaded_data].present?
    Document.create!(documentable: @study, uploaded_data: params[:study][:uploaded_data])
  end
  params[:study].delete(:uploaded_data)

  ActiveRecord::Base.transaction do
    params[:study].delete(:ethically_approved) unless can? :change_ethically_approved, @study
    @study.update!(params[:study])
    flash[:notice] = 'Your study has been updated'
    redirect_to controller: 'admin/studies', action: 'update', id: @study.id
  end
rescue ActiveRecord::RecordInvalid => e
  errors = @study.errors.full_messages
  logger.warn "Failed to update attributes: #{errors}}"
  flash[:error] = 'Failed to update attributes for study!'
  render action: :show, id: @study.id and return
end

#showObject


12
13
14
15
# File 'app/controllers/admin/studies_controller.rb', line 12

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

#sortObject


83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/admin/studies_controller.rb', line 83

def sort
  @studies = Study.all.sort_by(&:name)
  case params[:sort]
  when 'date'
    @studies = @studies.sort_by(&:created_at)
  when 'owner'
    @studies = @studies.sort_by(&:user_id)
  end
  render partial: 'studies'
end

#updateObject


17
18
19
20
21
22
# File 'app/controllers/admin/studies_controller.rb', line 17

def update
  @study = Study.find(params[:id])
  flash.now[:warning] = @study.warnings if @study.warnings.present?
  flash[:notice] = 'Your study has been updated'
  render partial: 'manage_single_study'
end