Class: Scat::Edit

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/scat/edit.rb

Overview

The Edit helper makes a CaTissue::Specimen from the edit form parameters and creates the specimen in the database.

Instance Method Summary collapse

Constructor Details

#initializeEdit

Returns a new instance of Edit.



13
14
15
16
# File 'lib/scat/edit.rb', line 13

def initialize
  super
  @conf = Configuration.new(FIELD_CONFIG)
end

Instance Method Details

#fields<Field>

Returns the specimen edit field specifications.

Returns:

  • (<Field>)

    the specimen edit field specifications



19
20
21
22
# File 'lib/scat/edit.rb', line 19

def fields
  # Delegate to the configuration.
  @conf.fields
end

#input_id(name_or_label) ⇒ String

Returns the corresponding HTML input element id.

Parameters:

  • name (Symbol, String)

    the field name or label

Returns:

  • (String)

    the corresponding HTML input element id



26
27
28
29
30
# File 'lib/scat/edit.rb', line 26

def input_id(name_or_label)
  field = @conf[name_or_label]
  if field.nil? then raise ArgumentError.new("Scat field not found: #{name_or_label.qp}") end
  field.input_id
end

#save(params, session) ⇒ CaTissue::Specimen

Saves the CaTissue::Specimen object specified in the request parameters.

Parameters:

  • params ({Symbol => String})

    the request parameters

  • session ({String => String})

    the current Sinatra session

Returns:

  • (CaTissue::Specimen)

    the saved specimen

Raises:

  • (ScatError)

    if the parameters are insufficient to build a specimen

  • (CaRuby::DatabaseError)

    if the save is unsuccessful



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scat/edit.rb', line 39

def save(params, session)
  logger.debug { "Scat is saving the specimen with parameters #{params.qp}..." }
  pcl_id = session[:protocol_id] if session[:protocol] == params[:protocol]
  site_id = session[:site_id] if pcl_id
  cpr_id = session[:cpr_id] if pcl_id and session[:mrn] == params[:mrn]
  scg_id = session[:scg_id] if cpr_id and session[:spn] == params[:spn]
  # the caTissue class => { property => value } hash
  ph = @conf.slice(params)
  # the collection protocol
  pcl = to_protocol(ph[CaTissue::CollectionProtocol].merge({:identifier => pcl_id}))
  # the current user
  user = params[:user]
  # the collection site
  site = site_id ? CaTissue::Site.new(:identifier => site_id) : to_site(pcl, user)
  # the patient
  pnt = CaTissue::Participant.new(ph[CaTissue::Participant])
  # the CPR parameters
  reg_params = ph[CaTissue::ParticipantMedicalIdentifier].merge(:participant => pnt, :site => site)
  # the CPR
  reg = to_registration(pcl, reg_params) 
  reg.identifier = cpr_id
  # the specimen parameters
  spc_params = ph[CaTissue::Specimen].merge(ph[CaTissue::SpecimenCharacteristics])
  # The current user is the biobank specimen receiver.
  spc_params.merge!(:receiver => user)
  # the specimen to save
  spc = to_specimen(pcl, spc_params)
  # the SCG parameters
  scg_params = ph[CaTissue::SpecimenCollectionGroup].merge(:participant => pnt, :site => site)
  # the SCG which contains the specimen
  pcl.add_specimens(spc, scg_params)
  # Save the specimen.
  logger.debug { "Scat is saving #{spc} with content:\n#{spc.dump}" }
  spc.save
  # Format the status message.
  session[:status] = "Created the specimen with label #{spc.label}."
  # Capture the params in the session to refresh the form.
  params.each { |a, v| session[a.to_sym] = v }
  # Capture the ids.
  scg = spc.specimen_collection_group
  session[:scg_id] = scg.identifier
  session[:site_id] = site.identifier
  cpr = scg.registration
  session[:cpr_id] = cpr.identifier
  session[:protocol_id] = cpr.protocol.identifier
  logger.debug { "Scat saved #{spc}." }
  spc
end