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}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (QuickVerbatimResponse) initialize(options = {})

Returns a new instance of QuickVerbatimResponse



85
86
87
88
89
# File 'lib/material.rb', line 85

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

Instance Attribute Details

- (Object) collection_objects

Returns the value of attribute collection_objects



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

def collection_objects
  @collection_objects
end

- (Object) form_params

Returns the value of attribute form_params



76
77
78
# File 'lib/material.rb', line 76

def form_params
  @form_params
end

- (Object) identifier

Returns the value of attribute identifier



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

def identifier
  @identifier
end

- (Object) locks

Returns the value of attribute locks



74
75
76
# File 'lib/material.rb', line 74

def locks
  @locks
end

- (Object) namespace

Returns the value of attribute namespace



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

def namespace
  @namespace
end

- (Object) note

Returns the value of attribute note



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

def note
  @note
end

- (Object) preparation_type

Returns the value of attribute preparation_type



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

def preparation_type
  @preparation_type
end

- (Object) quick_verbatim_object

Returns the value of attribute quick_verbatim_object



73
74
75
# File 'lib/material.rb', line 73

def quick_verbatim_object
  @quick_verbatim_object
end

- (Object) repository

Returns the value of attribute repository



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

def repository
  @repository
end

Instance Method Details

- (Object) build_models



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/material.rb', line 91

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

- (Object) collection_object



203
204
205
# File 'lib/material.rb', line 203

def collection_object
  @quick_verbatim_object ||= QuickVerbatimObject.new
end

- (Object) duplicate_with_locks



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/material.rb', line 182

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)

Returns:

  • (Boolean)


178
179
180
# File 'lib/material.rb', line 178

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

- (Object) next_identifier



198
199
200
201
# File 'lib/material.rb', line 198

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

- (Object) save



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/material.rb', line 153

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