Class: Admin::StudiesController

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

Instance Method Summary collapse

Instance Method Details

#editObject



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

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



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

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



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

def managed_update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  @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.now[: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



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

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



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

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