Class: ObjectType

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/object_type.rb

Overview

Defines the type of physical object that would be represented in an Item

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

used in app/planner/operation_type_export



227
228
229
230
231
232
233
234
235
236
# File 'app/models/object_type.rb', line 227

def self.clean_up_sample_type_links(raw_object_types)
  raw_object_types.each do |rot|
    ot = ObjectType.find_by(name: rot[:name])
    st = SampleType.find_by(name: rot[:sample_type_name])
    if st && ot
      ot.sample_type_id = st.id
      ot.save
    end
  end
end

.compare_and_upgrade(raw_types) ⇒ Object

used in app/planner/operation_type_export



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/object_type.rb', line 193

def self.compare_and_upgrade(raw_types)
  parts = %i[cleanup data description handler max min name safety
             vendor unit cost release_method release_description prefix]
  inconsistencies = []
  notes = []

  raw_types.each do |raw_type|
    type = ObjectType.find_by(name: raw_type[:name])

    if type
      parts.each do |part|
        inconsistencies << "Container '#{raw_type[:name]}': field #{part} differs from imported object type's corresponding field." unless type[part] == raw_type[part]
      end
      notes << "Container '#{raw_type[:name]}' matches existing object type." unless inconsistencies.any?
      next
    end

    next if inconsistencies.any?

    type = ObjectType.create_from(raw_type)
    type.save
    if type.errors.any?
      inconsistencies << "Could not create '#{raw_type[:name]}': #{type.errors.full_messages.join(', ')}"
    else
      notes << "Created new object type '#{raw_type[:name]}' with id #{type.id}"
    end
  end

  notes << 'Could not create required object type(s) due to type definition inconsistencies.' if inconsistencies.any?

  { notes: notes, inconsistencies: inconsistencies }
end

.container_types(sample_type:) ⇒ Object

scopes for searching ObjectTypes



239
240
241
# File 'app/models/object_type.rb', line 239

def self.container_types(sample_type:)
  where(sample_type_id: sample_type.id).where.not(name: '__Part')
end

.create_from(raw_type) ⇒ Object

used by compare_and_upgrade



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/object_type.rb', line 176

def self.create_from(raw_type)
  attributes = %i[cleanup data description handler max min name safety
                  vendor unit cost release_method release_description prefix]
  ot = ObjectType.new
  attributes.each do |attribute|
    ot[attribute] = raw_type[attribute]
  end

  if ot.collection_type?
    ot[:rows] = raw_type[:rows]
    ot[:columns] = raw_type[:columns]
  end

  ot
end

.part_typeObject



243
244
245
# File 'app/models/object_type.rb', line 243

def self.part_type
  find_by(name: '__Part')
end

Instance Method Details

#collection_type?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/object_type.rb', line 42

def collection_type?
  handler == 'collection'
end

#columnsObject



56
57
58
59
60
# File 'app/models/object_type.rb', line 56

def columns
  return unless collection_type?

  self[:columns] || 12
end

#columns=(value) ⇒ Object



66
67
68
# File 'app/models/object_type.rb', line 66

def columns=(value)
  self[:columns] = value
end

#data_objectObject

used in object_type views



161
162
163
164
165
166
167
168
# File 'app/models/object_type.rb', line 161

def data_object
  begin
    result = JSON.parse(T.must(data), symbolize_names: true)
  rescue StandardError
    result = {}
  end
  result
end

#default_dimensionsObject

TODO: dead code?



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/object_type.rb', line 138

def default_dimensions # for collections
  raise 'Tried to get dimensions of a container that is not a collection' unless collection_type?

  begin
    h = JSON.parse(T.must(data), symbolize_names: true)
  rescue JSON::ParserError
    raise "Could not parse data field '#{data}' of object type #{id}. Please go to " \
          "<a href='/object_types/#{id}/edit'>Object Type #{id}</a> and edit the data " \
          'field so that it reads something like { "rows": 10, "columns": 10 }'
  end
  if h[:rows] && h[:columns]
    [h[:rows], h[:columns]]
  else
    [1, 1]
  end
end

#exportObject

used by item.export



133
134
135
# File 'app/models/object_type.rb', line 133

def export
  attributes
end

#handlerString

Gets handler of ObjectType.

Returns:

  • (String)

    the name of the category that classifies the object type, as in "liquid_media". The special handler "collection" is used to show that items with this given object type are collections



22
# File 'app/models/object_type.rb', line 22

attr_accessible :handler

#in_useObject

TODO: dead code



100
101
102
103
104
105
106
# File 'app/models/object_type.rb', line 100

def in_use
  q = 0
  items.each do |i|
    q += T.must(i.inuse)
  end
  q
end

#min_and_maxObject



70
71
72
73
# File 'app/models/object_type.rb', line 70

def min_and_max
  errors.add(:min, 'min must be greater than zero and less than or equal to max') unless
    min && max && T.must(min) >= 0 && T.must(min) <= T.must(max)
end

#nameString

Gets name of ObjectType.

Returns:

  • (String)

    the name of the ObjectType, as in "1 L Bottle"



15
# File 'app/models/object_type.rb', line 15

attr_accessible :name

#posObject



75
76
77
78
# File 'app/models/object_type.rb', line 75

def pos
  errors.add(:cost, 'must be at least $0.01') unless
    cost && T.must(cost) >= 0.01
end

#proper_release_methodObject



80
81
82
83
84
85
# File 'app/models/object_type.rb', line 80

def proper_release_method
  errors.add(:release_method, 'must be either return, dispose, or query') unless
    release_method && (release_method == 'return' ||
                             release_method == 'dispose' ||
                             release_method == 'query')
end

#quantityObject

used in views/search/search.html.erb



91
92
93
94
95
96
97
# File 'app/models/object_type.rb', line 91

def quantity
  q = 0
  items.each do |i|
    q += T.must(i.quantity) if T.must(i.quantity) >= 0
  end
  q
end

#rowsObject



50
51
52
53
54
# File 'app/models/object_type.rb', line 50

def rows
  return unless collection_type?

  self[:rows] || 1
end

#rows=(value) ⇒ Object



62
63
64
# File 'app/models/object_type.rb', line 62

def rows=(value)
  self[:rows] = value
end

#sample?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/object_type.rb', line 46

def sample?
  handler == 'sample_container'
end

#sample_typeSampleType

Gets SampleType for this ObjectType.

Returns:

  • (SampleType)

    type of Sample that is allowed to exist in an item with this ObjectType as its container



28
# File 'app/models/object_type.rb', line 28

belongs_to :sample_type

#sample_type_nameObject

TODO: dead code



171
172
173
# File 'app/models/object_type.rb', line 171

def sample_type_name
  sample_type&.name
end

#save_as_test_type(name) ⇒ Object

TODO: dead code



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/object_type.rb', line 109

def save_as_test_type(name)

  self.name = name
  self.handler = 'temporary'
  self.unit = 'object'
  self.min = 0
  self.max = 100
  self.safety = 'No safety information'
  self.cleanup = 'No cleanup information'
  self.data = 'No data'
  self.vendor = 'No vendor information'
  self.cost = 0.01
  self.release_method = 'return'
  self.description = 'An object type made on the fly.'
  save
  i = items.new
  i.quantity = 1000
  i.inuse = 0
  i.location = 'A0.000'
  i.save

end

#to_sObject

TODO: this shouldn't have view details, probably dead code



156
157
158
# File 'app/models/object_type.rb', line 156

def to_s
  "<a href='/object_types/#{id}' class='aquarium-item' id='#{id}'>#{id}</a>"
end