Class: InventoryFile

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/inventory_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#library_idObject

Returns the value of attribute library_id.



21
22
23
# File 'app/models/inventory_file.rb', line 21

def library_id
  @library_id
end

Instance Method Details

#export(col_sep: "\t") ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/inventory_file.rb', line 47

def export(col_sep: "\t")
  file = Tempfile.create('inventory_file') do |f|
    inventories.each do |inventory|
      f.write inventory.to_hash.values.to_csv(col_sep)
    end

    f.rewind
    f.read
  end

  file
end

#found_itemsObject



64
65
66
# File 'app/models/inventory_file.rb', line 64

def found_items
  items
end

#importObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/inventory_file.rb', line 25

def import
  self.reload
  file = File.open(self.inventory.path)
  reader = file.read
  reader.split.each do |row|
    identifier = row.to_s.strip
    item = Item.find_by(item_identifier: identifier)
    if item
      unless self.items.where(id: item.id).select('items.id').first
        Inventory.create(
          inventory_file: self,
          item: item,
          current_shelf_name: shelf.name,
          item_identifier: identifier
        )
      end
    end
  end
  file.close
  true
end

#missing_itemsObject



60
61
62
# File 'app/models/inventory_file.rb', line 60

def missing_items
  Item.where(Inventory.where('items.id = inventories.item_id AND inventories.inventory_file_id = ?', id).exists.not)
end