Class: Studies::SampleRegistrationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/studies/sample_registration_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/studies/sample_registration_controller.rb', line 10

def create
  # We have to remap the contents of the 'sample_registrars' parameter from a hash to an array, because
  # that's what it actually is: a map from index to attributes for that SampleRegistrar instance.
  attributes = clean_params_from_check(params['sample_registrars']).to_h.each_with_object([]) do |(index_as_string, parameters), store|
    store[index_as_string.to_i] = parameters.merge(study: @study, user: current_user)
  end.compact

  @sample_registrars = SampleRegistrar.register!(attributes)
  flash[:notice] = 'Your samples have been registered'
  respond_to do |format|
    format.html { redirect_to study_path(@study) }
    format.json { render(json: flash.to_json) }
    format.xml  { render(xml: flash.to_xml) }
  end
rescue SampleRegistrar::NoSamplesError => e
  flash.now[:error] = 'You do not appear to have specified any samples'
  @sample_registrars = [SampleRegistrar.new]
  render(action: 'new')
rescue SampleRegistrar::RegistrationError => e
  flash.now[:error] = 'Your samples have not been registered'
  @sample_registrars = e.sample_registrars
  render(action: 'new')
end

#indexObject


7
8
# File 'app/controllers/studies/sample_registration_controller.rb', line 7

def index
end

#newObject


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

def new
  @sample_registrars = [SampleRegistrar.new]
end

#spreadsheetObject


38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/studies/sample_registration_controller.rb', line 38

def spreadsheet
  flash.now[:notice] = 'Processing your file: please wait a few minutes...'
  @sample_registrars = SampleRegistrar.from_spreadsheet(params['file'], @study, current_user)
  flash.now[:notice] = 'Your file has been processed'
  render :new
rescue SampleRegistrar::SpreadsheetError => e
  flash[:notice] = 'Your file has been processed'
  flash[:error] = e.message
  redirect_to upload_study_sample_registration_index_path
end

#uploadObject


49
50
# File 'app/controllers/studies/sample_registration_controller.rb', line 49

def upload
end