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 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



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

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

Instance Attribute Details

- (Object) collection_objects

Returns the value of attribute collection_objects



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

def collection_objects
  @collection_objects
end

- (Object) form_params

Returns the value of attribute form_params



70
71
72
# File 'lib/material.rb', line 70

def form_params
  @form_params
end

- (Object) identifier

Returns the value of attribute identifier



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

def identifier
  @identifier
end

- (Object) locks

Returns the value of attribute locks



68
69
70
# File 'lib/material.rb', line 68

def locks
  @locks
end

- (Object) namespace

Returns the value of attribute namespace



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

def namespace
  @namespace
end

- (Object) note

Returns the value of attribute note



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

def note
  @note
end

- (Object) quick_verbatim_object

Returns the value of attribute quick_verbatim_object



67
68
69
# File 'lib/material.rb', line 67

def quick_verbatim_object
  @quick_verbatim_object
end

- (Object) repository

Returns the value of attribute repository



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

def repository
  @repository
end

Instance Method Details

- (Object) build_models



84
85
86
87
88
89
90
91
92
93
# File 'lib/material.rb', line 84

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?)
  @identifier = Identifier::Local::CatalogNumber.new(form_params['identifier'])
  @namespace  = identifier.namespace 
end

- (Object) collection_object



186
187
188
# File 'lib/material.rb', line 186

def collection_object
  @quick_verbatim_object ||= QuickVerbatimObject.new
end

- (Object) duplicate_with_locks



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/material.rb', line 166

def duplicate_with_locks
  n = QuickVerbatimResponse.new(form_params)
  # nullify if not locked
  #
  n.repository = nil if !locked?('repository')
  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)


162
163
164
# File 'lib/material.rb', line 162

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

- (Object) next_identifier



181
182
183
184
# File 'lib/material.rb', line 181

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

- (Object) save



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/material.rb', line 137

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

  begin
    ActiveRecord::Base.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