Class: SubmissionsController

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

Overview

The submission controller handles the AJAXy submission form, not bulk submissions The typical submission creation process is actually handled by a series of requests, such as the fields that get displayed when a submission template is selected, and the creation of each independent order. Most the actual heavy lifting occurs in Submission::SubmissionCreator

Constant Summary

Constants included from FlashTruncation

FlashTruncation::STRING_OVERHEAD

Instance Method Summary collapse

Methods inherited from ApplicationController

#block_api_access, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!

Methods included from FlashTruncation

#max_flash_size, #truncate_flash, #truncate_flash_array

Instance Method Details

#cancelObject

Cancels the selected submission, and returns the user to the submission show page. Cancelled submissions in turn cancel all their requests. Only cancellable submissions can be cancelled. (There shouldn’t be a link if they can’t be cancelled) but it might be nice to add a bit more user friendly error handling here.



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

def cancel
  submission = Submission.find(params[:id])
  submission.cancel!
  redirect_to action: :show, id: params[:id]
end

#change_priorityObject



81
82
83
84
# File 'app/controllers/submissions_controller.rb', line 81

def change_priority
  Submission.find(params[:id]).update!(priority: params[:submission][:priority])
  redirect_to action: :show, id: params[:id]
end

#createObject

Triggered when someone clicks ‘Save Order’ in the submission creator New Order is just client side Creates an order, followed by a submission, and then assigns the order to the submission. On subsequent clicks of ‘Save Order’ we pass in the submission id from the original



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/submissions_controller.rb', line 52

def create
  @presenter = Submission::SubmissionCreator.new(current_user, params[:submission].to_unsafe_h)

  if @presenter.save
    render partial: 'saved_order',
           locals: {
             presenter: @presenter,
             order: @presenter.order,
             form: :dummy_form_symbol
           },
           layout: false
  else
    render partial: 'order_errors', layout: false, status: :unprocessable_entity
  end
end

#destroyObject

Submissions can only be destroyed when they are still building.



97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/submissions_controller.rb', line 97

def destroy
  ActiveRecord::Base.transaction do
    submission = Submission::SubmissionPresenter.new(current_user, id: params[:id])
    if submission.destroy
      flash[:notice] = 'Submission successfully deleted!'
    else
      flash[:error] = "This submission can't be deleted"
    end
    redirect_to action: :index
  end
end

#editObject



44
45
46
# File 'app/controllers/submissions_controller.rb', line 44

def edit
  @presenter = Submission::SubmissionCreator.new(current_user, id: params[:id])
end

#indexObject

Displays a list of submissions for the current user. building => Submissions which haven’t yet been queued for building and may still be edited. Submissions begin in this state, and leave when the user clicks ‘Build Submission’ pending => Submissions which the user has finished setting up, and has queued for processing by the delayed job ready => Submissions which the delayed job has finished processing. The final state of a submission.



23
24
25
26
27
28
29
30
# File 'app/controllers/submissions_controller.rb', line 23

def index # rubocop:todo Metrics/AbcSize
  # Disable cache of this page
  expires_now

  @building = Submission.building.order(created_at: :desc).where(user_id: current_user.id)
  @pending = Submission.pending.order(created_at: :desc).where(user_id: current_user.id)
  @ready = Submission.ready.order(created_at: :desc).limit(10).where(user_id: current_user.id)
end

#newObject

The main landing page for creating a new submission. Lots of ajax action!



39
40
41
42
# File 'app/controllers/submissions_controller.rb', line 39

def new
  expires_now
  @presenter = Submission::SubmissionCreator.new(current_user, study_id: params[:study_id])
end

#order_fieldsObject

AJAXY route for rendering the submission level options which appear upon selecting a submission template.



123
124
125
126
127
# File 'app/controllers/submissions_controller.rb', line 123

def order_fields
  @presenter = Submission::SubmissionCreator.new(current_user, params[:submission])

  render partial: 'order_fields', layout: false
end

#showObject

Show a submission. Read-only page, but provides a link to the edit page for submissions which haven’t yet left state building



34
35
36
# File 'app/controllers/submissions_controller.rb', line 34

def show
  @presenter = Submission::SubmissionPresenter.new(current_user, id: params[:id])
end

#studyObject

An index page for study submissions. Bit unconventional URL eg: localhost:3000/submissions/study?id=23 Rather than localhost:3000/studies/23/submissions



113
114
115
116
# File 'app/controllers/submissions_controller.rb', line 113

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

#study_assetsObject

AJAXY route to populate study asset group dropdown



130
131
132
133
134
# File 'app/controllers/submissions_controller.rb', line 130

def study_assets
  @presenter = Submission::SubmissionCreator.new(current_user, params[:submission])

  render partial: 'study_assets', layout: false
end

#updateObject

This method will build a submission then redirect to the submission on completion



69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/submissions_controller.rb', line 69

def update
  @presenter = Submission::SubmissionCreator.new(current_user, id: params[:id])

  @presenter.build_submission!

  if @presenter.submission.errors.present?
    flash[:error] = "The submission could not be built: #{@presenter.submission.errors.full_messages}"
  end

  redirect_to @presenter.submission
end