Sha256: 8fbcac7256d2d17e1a7199e3f578e1884ab3de8a847816613bb41df79dfae7a5

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

class InventoryFile < ActiveRecord::Base
  attr_accessible :inventory, :note
  has_many :inventories, :dependent => :destroy
  has_many :items, :through => :inventories
  belongs_to :user
  validates_presence_of :user

  if Setting.uploaded_file.storage == :s3
    has_attached_file :inventory, :storage => :s3, :s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
      :s3_permissions => :private
  else
    has_attached_file :inventory,
      :path => ":rails_root/private/system/:class/:attachment/:id_partition/:style/:filename"
  end
  validates_attachment_content_type :inventory, :content_type => ['text/csv', 'text/plain', 'text/tab-separated-values']
  validates_attachment_presence :inventory

  paginates_per 10

  def import
    self.reload
    file = File.open(self.inventory.path)
    reader = file.read
    reader.split.each do |row|
      item = Item.where(:item_identifier => row.to_s.strip).first
      if item
        unless self.items.where(:id => item.id).select('items.id').first
          self.items << item
        end
      end
    end
    file.close
    true
  end
end

# == Schema Information
#
# Table name: inventory_files
#
#  id                     :integer         not null, primary key
#  filename               :string(255)
#  content_type           :string(255)
#  size                   :integer
#  user_id                :integer
#  note                   :text
#  created_at             :datetime        not null
#  updated_at             :datetime        not null
#  inventory_file_name    :string(255)
#  inventory_content_type :string(255)
#  inventory_file_size    :integer
#  inventory_updated_at   :datetime
#  inventory_fingerprint  :string(255)
#

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
enju_inventory-0.1.11.pre4 app/models/inventory_file.rb
enju_inventory-0.1.11.pre2 app/models/inventory_file.rb
enju_inventory-0.1.11.pre app/models/inventory_file.rb