Class: Material::QuickVerbatimResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/material.rb

Overview

A Container to store results of create_quick_verbatim

Constant Summary collapse

LOCKS =
%w{namespace repository preparation_type increment collecting_event determinations other_labels note}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ QuickVerbatimResponse

Returns a new instance of QuickVerbatimResponse.

Parameters:

  • args (Hash)


91
92
93
94
95
# File 'lib/material.rb', line 91

def initialize(options = {})
  @form_params = options
  build_models
  @collection_objects = []
end

Instance Attribute Details

#collection_objectsObject

Returns the value of attribute collection_objects.



83
84
85
# File 'lib/material.rb', line 83

def collection_objects
  @collection_objects
end

#form_paramsObject

Returns the value of attribute form_params.



81
82
83
# File 'lib/material.rb', line 81

def form_params
  @form_params
end

#identifierIdentifier::Local::CatalogNumber



131
132
133
# File 'lib/material.rb', line 131

def identifier
  @identifier
end

#locksObject

Returns the value of attribute locks.



79
80
81
# File 'lib/material.rb', line 79

def locks
  @locks
end

#namespaceNamespace

Returns:



164
165
166
# File 'lib/material.rb', line 164

def namespace
  @namespace
end

#noteNote

Returns:



175
176
177
# File 'lib/material.rb', line 175

def note
  @note
end

#preparation_typePreparationType

Returns:



153
154
155
# File 'lib/material.rb', line 153

def preparation_type
  @preparation_type
end

#quick_verbatim_objectObject

Returns the value of attribute quick_verbatim_object.



78
79
80
# File 'lib/material.rb', line 78

def quick_verbatim_object
  @quick_verbatim_object
end

#repositoryRepository

Returns:



142
143
144
# File 'lib/material.rb', line 142

def repository
  @repository
end

Instance Method Details

#build_modelsString

Returns:

  • (String)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/material.rb', line 98

def build_models
  @quick_verbatim_object = QuickVerbatimObject.new(form_params['collection_object'])

  @locks = Forms::FieldLocks.new(form_params['locks'])

  @note       = Note.new(form_params['note'])
  @repository = Repository.find(form_params['repository']['id']) if (form_params['repository'] && !form_params['repository']['id'].blank?)
  @preparation_type = PreparationType.find(form_params['preparation_type']['id']) if (form_params['preparation_type'] && !form_params['preparation_type']['id'].blank?)
  @identifier = Identifier::Local::CatalogNumber.new(form_params['identifier'])
  @namespace  = identifier.namespace
end

#collection_objectQuickVerbatimObject

Returns:



235
236
237
# File 'lib/material.rb', line 235

def collection_object
  @quick_verbatim_object ||= QuickVerbatimObject.new
end

#duplicate_with_locksQuickVerbatimResponse



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/material.rb', line 212

def duplicate_with_locks
  n = QuickVerbatimResponse.new(form_params)
  # nullify if not locked
  #
  n.repository = nil if !locked?('repository')
  n.preparation_type = nil if !locked?('preparation_type')
  n.namespace  = nil if !locked?('namespace')
  n.note       = nil if !locked?('note')

  n.collection_object.buffered_collecting_event = nil if !locked?('collecting_event')
  n.collection_object.buffered_determinations   = nil if !locked?('determinations')
  n.collection_object.buffered_other_labels     = nil if !locked?('other_labels')
  n.identifier.identifier = next_identifier
  n
end

#locked?(name) ⇒ Boolean

Parameters:

  • name (Object)

Returns:

  • (Boolean)


207
208
209
# File 'lib/material.rb', line 207

def locked?(name)
  locks.locked?('locks', name.to_s)
end

#next_identifierString

Returns:

  • (String)


229
230
231
232
# File 'lib/material.rb', line 229

def next_identifier
  return nil if !locked?('increment')
  Utilities::Strings.increment_contained_integer(identifier.identifier)
end

#saveBoolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/material.rb', line 180

def save
  if collection_objects.size == 0
    errors = ActiveModel::Errors.new('base')
    errors.add(:total, 'No totals provided!')
    return false, errors
  end

  begin
    ApplicationRecord.transaction do

      collection_objects.each do |o|
        if o.contained_in
          o.contained_in.save! if o.contained_in.new_record?
        end

        o.save!
      end
    end

    return true
  rescue ActiveRecord::RecordInvalid => invalid
    return false, invalid.record.errors
  end
end