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

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Ignored) initialize(options = {})

Parameters:

  • args (Hash)


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

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

Instance Attribute Details

- (Object) collection_objects

Returns the value of attribute collection_objects



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

def collection_objects
  @collection_objects
end

- (Object) form_params

Returns the value of attribute form_params



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

def form_params
  @form_params
end

- (Identifier::Local::CatalogNumber) identifier



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

def identifier
  @identifier
end

- (Object) locks

Returns the value of attribute locks



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

def locks
  @locks
end

- (Namespace) namespace

Returns:



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

def namespace
  @namespace
end

- (Note) note

Returns:



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

def note
  @note
end

- (PreparationType) preparation_type

Returns:



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

def preparation_type
  @preparation_type
end

- (Object) quick_verbatim_object

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

- (Repository) repository

Returns:



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

def repository
  @repository
end

Instance Method Details

- (String) build_models

Returns:

  • (String)


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

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

- (QuickVerbatimObject) collection_object

Returns:



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

def collection_object
  @quick_verbatim_object ||= QuickVerbatimObject.new
end

- (QuickVerbatimResponse) duplicate_with_locks



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

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

- (Boolean) locked?(name)

Parameters:

  • name (Object)

Returns:

  • (Boolean)


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

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

- (String) next_identifier

Returns:

  • (String)


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

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

- (Boolean) save

Returns:

  • (Boolean)


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

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