Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
rack layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjpage committed May 13, 2011
1 parent c28dc4a commit 1a71a3b
Show file tree
Hide file tree
Showing 36 changed files with 443 additions and 160 deletions.
20 changes: 20 additions & 0 deletions app/controllers/rack_receptions_controller.rb
@@ -0,0 +1,20 @@
class RackReceptionsController < ApplicationController
def index
@reception = Reception::Rack::Base.new
end

def create
@reception = Reception::Rack::Base.new(params[:reception])

respond_to do |format|
if @reception.valid?
@reception.save
format.html { redirect_to( rack_receptions_path, :notice => 'Uploaded rack') }
else
format.html { redirect_to( rack_receptions_path, :notice => @reception.errors.full_messages.first) }
end
end
end


end
4 changes: 2 additions & 2 deletions app/controllers/receptions_controller.rb
@@ -1,11 +1,11 @@
class ReceptionsController < ApplicationController

def index
@reception = Reception::Base.new
@reception = Reception::Standard::Base.new
end

def create
@reception = Reception::Base.new(params[:reception])
@reception = Reception::Standard::Base.new(params[:reception])

respond_to do |format|
if @reception.valid?
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/rescan_racks_controller.rb
@@ -0,0 +1,5 @@
class RescanRacksController < ApplicationController
def index
@assets = Asset.where( :dirty_layout => true ).paginate(:page => params[:page])
end
end
33 changes: 21 additions & 12 deletions app/models/asset.rb
@@ -1,47 +1,56 @@
class Asset < ActiveRecord::Base
cattr_reader :per_page
@@per_page = 384

belongs_to :storage_area
belongs_to :map
belongs_to :container, :class_name => "Asset"
has_many :contained_assets, :class_name => "Asset", :foreign_key => :container_id
has_many :asset_audits

validates_presence_of :barcode
validates_uniqueness_of :barcode

attr_accessor :user_login

acts_as_audited :except => [:created_at, :updated_at]

before_save :decode_to_sanger_barcode, :if => :barcode_changed?
before_save :container_layout_dirty, :if => :container_id_changed?
after_save :audit_change

scope :for_search_query, lambda { |search_terms| { :conditions => [ 'barcode IN (?) OR decoded_barcode_number in (?)', search_terms, search_terms ] } }


def decode_to_sanger_barcode
self.decoded_barcode_number, self.decoded_prefix = nil, nil
return unless Barcode.valid_ean13_barcode?(barcode)
prefix_number, self.decoded_barcode_number, _ = Barcode.split_barcode(barcode)
self.decoded_prefix = Barcode.prefix_to_human(prefix_number)
end


def container_layout_dirty
previous_container = Asset.find_by_id(container_id_was)
return if previous_container.nil?
previous_container.update_attributes!(:dirty_layout => true)
end


def audit_change
action = 'Check in'
action = 'Check out' if storage_area.nil? && container.nil?

self.asset_audits.create!(
:container_barcode => container.try(:barcode),
:container_barcode => container.try(:barcode),
:map_description => map.try(:description),
:storage_area_name => storage_area.try(:name),
:freezer_name => storage_area.try(:freezer).try(:name),
:building_area_name => storage_area.try(:freezer).try(:building_area).try(:name),
:user_login => user_login,
:action => action
)
end



end
3 changes: 1 addition & 2 deletions app/models/building_area.rb
Expand Up @@ -12,8 +12,7 @@ class BuildingArea < ActiveRecord::Base
scope :for_search_query, lambda { |search_terms| { :conditions => [ 'name IN (?)', search_terms ] } }

def assets
# TODO replace with single query
freezers.map(&:assets).flatten
Asset.joins( :storage_area => { :freezer => :building_area } ).where( :building_areas => { :id => self.id } )
end

end
10 changes: 0 additions & 10 deletions app/models/reception/assets.rb

This file was deleted.

45 changes: 1 addition & 44 deletions app/models/reception/base.rb
@@ -1,49 +1,6 @@
# only checks into storage area not into container
class Reception::Base
include ActiveModel::Validations
include Reception::Validations
include Reception::Assets

attr_reader :user_barcode, :storage_area_barcode, :asset_barcodes

def initialize(attributes = {})
@user_barcode = attributes[:user_barcode]
@storage_area_barcode = attributes[:storage_area_barcode]
@asset_barcodes = attributes[:asset_barcodes]
end

def source_barcodes
@source_barcodes ||= split_barcodes(self.asset_barcodes)
end

def split_barcodes(barcodes_string)
return [] if barcodes_string.blank?
barcodes_string.split(/\W/)
end

def user_login
@user_name ||= UserBarcode::UserBarcode.find_username_from_barcode(self.user_barcode)
end

def storage_area
@storage_area ||= StorageArea.find_by_barcode(storage_area_barcode)
end

def container
@container ||= Asset.find_by_barcode(storage_area_barcode)
end

def check_in?
return false if storage_area_barcode.blank?
true
end


def save
assets.each do |asset|
asset.update_attributes!(:storage_area => storage_area, :container => container, :user_login => user_login)
end
end

include Reception::User
end

30 changes: 30 additions & 0 deletions app/models/reception/rack/base.rb
@@ -0,0 +1,30 @@
class Reception::Rack::Base < Reception::Base
include Reception::Rack::Validations

attr_reader :rack_layout_file, :rack_barcode

def initialize(attributes = {})
super(attributes)
@rack_layout_file = attributes[:rack_layout_file]
@rack_barcode = attributes[:rack_barcode]
end

def container
@container ||= Asset.find_or_create_by_barcode(:barcode => self.rack_barcode)
end

def rack_layout
@rack_layout ||= Reception::Rack::Layout::Base.new(:file => rack_layout_file)
end

def save
rack_layout.location_to_barcodes.each do |map, barcodes|
Asset.find_or_create_by_barcode(:barcode => barcodes.first).update_attributes!(:storage_area => nil, :map => map, :container => container, :user_login => user_login)
end

container.update_attributes!(:dirty_layout => false) if container.dirty_layout

true
end

end
20 changes: 20 additions & 0 deletions app/models/reception/rack/layout/base.rb
@@ -0,0 +1,20 @@
class Reception::Rack::Layout::Base
include ActiveModel::Validations
include Reception::Rack::Layout::Parser
include Reception::Rack::Layout::Validations

attr_reader :uploaded_file

def initialize(attributes = {})
@uploaded_file = attributes[:file]
end

def file
@file ||= self.uploaded_file.read
end

def location_to_barcodes
@location_to_barcodes ||= parse_spreadsheet(file)
end

end
23 changes: 23 additions & 0 deletions app/models/reception/rack/layout/parser.rb
@@ -0,0 +1,23 @@
module Reception::Rack::Layout::Parser

def parse_spreadsheet(spreadsheet_data)
map_to_barcodes = {}
CSV.parse(spreadsheet_data) do |row|
map, barcode = parse_row(row)
if map_to_barcodes[map]
map_to_barcodes[map] << barcode
else
map_to_barcodes[map] = [barcode]
end
end
map_to_barcodes
end

def parse_row(row)
return nil if row.blank?
map_description, barcode = row
map = Map.find_or_create_by_description( :description => map_description )

[map, barcode.strip]
end
end
45 changes: 45 additions & 0 deletions app/models/reception/rack/layout/validations.rb
@@ -0,0 +1,45 @@
module Reception::Rack::Layout::Validations
def self.included(base)
base.class_eval do
validates_presence_of :uploaded_file
validate :file_format
validate :one_barcode_per_location
validate :no_duplicated_barcodes
end
end

def file_format
begin
location_to_barcodes
rescue
invalid_format_error
end
end

def one_barcode_per_location
begin
location_to_barcodes.each do |location, barcodes|
if barcodes.size > 1
errors.add(:uploaded_file, "must have only one barcode per location")
return
end
end
rescue NoMethodError
invalid_format_error
end
end

def no_duplicated_barcodes
begin
barcodes = location_to_barcodes.values.flatten
errors.add(:uploaded_file, "must not have duplicated barcodes") if barcodes.size != barcodes.uniq.size
rescue NoMethodError
invalid_format_error
end
end

def invalid_format_error
errors.add(:uploaded_file, "must be a valid format")
end

end
22 changes: 22 additions & 0 deletions app/models/reception/rack/validations.rb
@@ -0,0 +1,22 @@
module Reception::Rack::Validations
def self.included(base)
base.class_eval do
validates_presence_of :rack_barcode
validates_presence_of :rack_layout_file
validate :format_of_rack_layout_file
end
end

def format_of_rack_layout_file
unless rack_layout.valid?
save_errors_to_base(rack_layout.errors)
end
end

def save_errors_to_base(object_errors)
object_errors.each do |key, message|
self.errors.add(key, message)
end
end

end
17 changes: 17 additions & 0 deletions app/models/reception/standard/asset.rb
@@ -0,0 +1,17 @@
module Reception::Standard::Asset
def find_assets
source_barcodes.map{ |barcode| Asset.find_or_create_by_barcode(:barcode => barcode) }
end

def assets
@assets ||= find_assets
end

def save
assets.each do |asset|
asset.update_attributes!(:storage_area => storage_area, :container => container, :user_login => user_login)
end

true
end
end
36 changes: 36 additions & 0 deletions app/models/reception/standard/base.rb
@@ -0,0 +1,36 @@
class Reception::Standard::Base < Reception::Base
include Reception::Standard::Validations
include Reception::Standard::Asset

attr_reader :storage_area_barcode, :asset_barcodes

def initialize(attributes = {})
super(attributes)
@storage_area_barcode = attributes[:storage_area_barcode]
@asset_barcodes = attributes[:asset_barcodes]
end

def container
@container ||= Asset.find_by_barcode(storage_area_barcode)
end

def check_in?
return false if storage_area_barcode.blank?
true
end

def storage_area
@storage_area ||= StorageArea.find_by_barcode(storage_area_barcode)
end

def source_barcodes
@source_barcodes ||= split_barcodes(self.asset_barcodes)
end

def split_barcodes(barcodes_string)
return [] if barcodes_string.blank?
barcodes_string.split(/\W/)
end

end

0 comments on commit 1a71a3b

Please sign in to comment.