Class: ObjectType
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ObjectType
- 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
-
.clean_up_sample_type_links(raw_object_types) ⇒ Object
used in app/planner/operation_type_export.
-
.compare_and_upgrade(raw_types) ⇒ Object
used in app/planner/operation_type_export.
-
.container_types(sample_type:) ⇒ Object
scopes for searching ObjectTypes.
-
.create_from(raw_type) ⇒ Object
used by compare_and_upgrade.
- .part_type ⇒ Object
Instance Method Summary collapse
- #collection_type? ⇒ Boolean
- #columns ⇒ Object
- #columns=(value) ⇒ Object
-
#data_object ⇒ Object
used in object_type views.
-
#default_dimensions ⇒ Object
TODO: dead code?.
-
#export ⇒ Object
used by item.export.
-
#handler ⇒ String
Gets handler of ObjectType.
-
#in_use ⇒ Object
TODO: dead code.
- #min_and_max ⇒ Object
-
#name ⇒ String
Gets name of ObjectType.
- #pos ⇒ Object
- #proper_release_method ⇒ Object
-
#quantity ⇒ Object
used in views/search/search.html.erb.
- #rows ⇒ Object
- #rows=(value) ⇒ Object
- #sample? ⇒ Boolean
-
#sample_type ⇒ SampleType
Gets SampleType for this ObjectType.
-
#sample_type_name ⇒ Object
TODO: dead code.
-
#save_as_test_type(name) ⇒ Object
TODO: dead code.
-
#to_s ⇒ Object
TODO: this shouldn't have view details, probably dead code.
Class Method Details
.clean_up_sample_type_links(raw_object_types) ⇒ Object
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..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_type ⇒ Object
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
42 43 44 |
# File 'app/models/object_type.rb', line 42 def collection_type? handler == 'collection' end |
#columns ⇒ Object
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_object ⇒ Object
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_dimensions ⇒ Object
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 |
#export ⇒ Object
used by item.export
133 134 135 |
# File 'app/models/object_type.rb', line 133 def export attributes end |
#handler ⇒ String
Gets handler of ObjectType.
22 |
# File 'app/models/object_type.rb', line 22 attr_accessible :handler |
#in_use ⇒ Object
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_max ⇒ Object
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 |
#name ⇒ String
Gets name of ObjectType.
15 |
# File 'app/models/object_type.rb', line 15 attr_accessible :name |
#pos ⇒ Object
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_method ⇒ Object
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 |
#quantity ⇒ Object
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 |
#rows ⇒ Object
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
46 47 48 |
# File 'app/models/object_type.rb', line 46 def sample? handler == 'sample_container' end |
#sample_type ⇒ SampleType
Gets SampleType for this ObjectType.
28 |
# File 'app/models/object_type.rb', line 28 belongs_to :sample_type |
#sample_type_name ⇒ Object
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_s ⇒ Object
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 |