Class: Admin::StudiesController

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

Instance Method Summary collapse

Instance Method Details

#editObject


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

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


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

def filter
  unless params[:filter].nil?
    if params[:filter][:by] == 'not approved'
      filter_conditions = { approved: false }
    end
  end

  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 { |r| r.name }.include?('manager')) }
    end
  end

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

#indexObject


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

def index
  @studies = Study.alphabetical
end

#managed_updateObject


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

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

  Document.create!(documentable: @study, uploaded_data: params[:study][:uploaded_data]) unless params[:study][:uploaded_data].blank?
  params[:study].delete(:uploaded_data)

  ActiveRecord::Base.transaction do
    params[:study].delete(:ethically_approved) unless current_user.data_access_coordinator?
    @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
  logger.warn "Failed to update attributes: #{@study.errors.map(&:to_s)}}"
  flash[:error] = 'Failed to update attributes for study!'
  render action: :show, id: @study.id and return
end

#showObject


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

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

#sortObject


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

def sort
  @studies = Study.all.sort_by { |study| study.name }
  if params[:sort] == 'date'
    @studies = @studies.sort_by { |study| study.created_at }
  elsif params[:sort] == 'owner'
    @studies = @studies.sort_by { |study| study.user_id }
  end
  render partial: 'studies'
end

#updateObject


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

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