Skip to content

Commit

Permalink
container
Browse files Browse the repository at this point in the history
  • Loading branch information
proceps committed Feb 27, 2014
1 parent 9fdda92 commit 54d1642
Show file tree
Hide file tree
Showing 21 changed files with 160 additions and 17 deletions.
22 changes: 22 additions & 0 deletions app/models/collection_profile.rb
@@ -1,4 +1,26 @@
class CollectionProfile < ActiveRecord::Base

include Housekeeping
include SoftValidation

belongs_to :container
belongs_to :otu

validates_presence_of :conservation_status, message: 'Conservation status is not selected'
validates_presence_of :processing_state, message: 'Processing state is not selected'
validates_presence_of :container_condition, message: 'Container condition is not selected'
validates_presence_of :condition_of_labels, message: 'Condition of labels is not selected'
validates_presence_of :identification_level, message: 'Identification level is not selected'
validates_presence_of :arrangement_level, message: 'Arrangement level is not selected'
validates_presence_of :data_quality, message: 'Data quality is not selected'
validates_presence_of :computerization_level, message: 'Computerization_level is not selected'

before_validation :validate_number

def validate_number
if self.number_of_collection_objects.nil? && self.number_of_containers.nil?
errors.add(:number_of_collection_objects, 'At least one number of specimens or number of containers should be specified') if self.parent_id.blank?
errors.add(:number_of_containers, 'At least one number of specimens or number of containers should be specified') if self.parent_id.blank?
end
end
end
47 changes: 45 additions & 2 deletions app/models/container.rb
@@ -1,10 +1,53 @@
class Container < ActiveRecord::Base
acts_as_nested_set
acts_as_nested_set scope: [:project_id]

include Housekeeping
include Shared::Identifiable
include Shared::Containable

has_many :physical_collection_objects
belongs_to :otu


#has_many :physical_collection_objects
has_many :collection_profiles
has_many :collection_items

before_validation :check_type

#CONTAINER_TYPES = %w(dry wet slide)

# Return a String with the "common" name for this class.
def self.class_name
self.type_name.demodulize.underscore.humanize.downcase
end

def type_name
r = self.type.to_s
CONTAINER_TYPE_NAMES.include?(r) ? r : nil
end

def type_class=(value)
write_attribute(:type, value.to_s)
end

def type_class
r = read_attribute(:type).to_s
r = CONTAINER_TYPE_NAMES.include?(r) ? r.safe_constantize : nil
r
end


protected

#region Validation

def check_type
unless CONTAINER_TYPES.include?(self.type)
errors.add(:type, 'Not a legal type of container')
end
end

#endregion

end

2 changes: 2 additions & 0 deletions app/models/container/box.rb
@@ -0,0 +1,2 @@
class Container::Box < Container
end
2 changes: 2 additions & 0 deletions app/models/container/cabinet.rb
@@ -0,0 +1,2 @@
class Container::Cabinet < Container
end
2 changes: 2 additions & 0 deletions app/models/container/drawer.rb
@@ -0,0 +1,2 @@
class Container::Drawer < Container
end
2 changes: 2 additions & 0 deletions app/models/container/folder.rb
@@ -0,0 +1,2 @@
class Container::Folder < Container
end
2 changes: 1 addition & 1 deletion app/models/container/pin.rb
@@ -1,2 +1,2 @@
class Container::Virtual < Container
class Container::Pin < Container
end
2 changes: 2 additions & 0 deletions app/models/container/slide.rb
@@ -0,0 +1,2 @@
class Container::Slide < Container
end
2 changes: 2 additions & 0 deletions app/models/container/slide_box.rb
@@ -0,0 +1,2 @@
class Container::SlideBox < Container
end
2 changes: 1 addition & 1 deletion app/models/container/vial.rb
@@ -1,2 +1,2 @@
class Container::Virtual < Container
class Container::Vial < Container
end
2 changes: 2 additions & 0 deletions app/models/container/vial_rack.rb
@@ -0,0 +1,2 @@
class Container::VialRack < Container
end
2 changes: 1 addition & 1 deletion app/models/container_item.rb
@@ -1,8 +1,8 @@
class ContainerItem < ActiveRecord::Base

include Housekeeping

belongs_to :container
#belongs_to :collection_object
belongs_to :contained_object, polymorphic: true

validates_presence_of :container_id, :contained_object_id, :contained_object_type
Expand Down
1 change: 1 addition & 0 deletions app/models/otu.rb
Expand Up @@ -9,5 +9,6 @@ class Otu < ActiveRecord::Base

has_many :contents, inverse_of: :otu
has_many :taxon_determinations, inverse_of: :otu
has_many :collection_profiles
has_many :topics, through: :contents, source: :topic
end
2 changes: 1 addition & 1 deletion app/models/type_material.rb
Expand Up @@ -82,7 +82,7 @@ def check_type_type
errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
end
unless self.protonym.rank_class.parent.to_s =~ /Species/
errors.add(:protonym_id, 'Type cannot be designated for not species group taxon')
errors.add(:protonym_id, 'Type cannot be designated for a not species group taxon')
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/container.rb
@@ -0,0 +1,7 @@
# Be sure to restart your server when you modify this file.

# All assignable container types
CONTAINER_TYPES = Container.descendants

# All assignable container type names
CONTAINER_TYPE_NAMES = CONTAINER_TYPES.collect{|i| i.to_s}
6 changes: 6 additions & 0 deletions db/migrate/20140226211135_container_name.rb
@@ -0,0 +1,6 @@
class ContainerName < ActiveRecord::Migration
def change
add_column :containers, :name, :string
add_column :containers, :disposition, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20140226212010_add_containter_item.rb
@@ -0,0 +1,5 @@
class AddContainterItem < ActiveRecord::Migration
def change
add_column :loan_items, :container_id, :integer
end
end
2 changes: 1 addition & 1 deletion spec/factories/collection_profile_factory.rb
Expand Up @@ -13,7 +13,7 @@
data_quality 1
computerization_level 1
number_of_collection_objects 1
number_of_containers 1
number_of_containers nil
created_by_id 1
modified_by_id 1
project_id 1
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/content_factory.rb
Expand Up @@ -3,9 +3,9 @@
FactoryGirl.define do
factory :content, traits: [:housekeeping] do
factory :valid_content do
association :otu, factory: :valid_otu
association :topic, factory: :valid_topic
text "MyText"
association :otu, factory: :valid_otu
association :topic, factory: :valid_topic
text "MyText"
end
end
end
40 changes: 39 additions & 1 deletion spec/models/collection_profile_spec.rb
@@ -1,5 +1,43 @@
require 'spec_helper'

describe CollectionProfile do
pending "add some examples to (or delete) #{__FILE__}"
before(:all) do
@collection_profile = FactoryGirl.build_stubbed(:collection_profile)
end

context 'associations' do
context 'belongs_to' do
specify 'conservation_status' do
expect(@collection_profile).to respond_to(:conservation_status)
end
specify 'processing_state' do
expect(@collection_profile).to respond_to(:processing_state)
end
specify 'container_condition' do
expect(@collection_profile).to respond_to(:container_condition)
end
specify 'condition_of_labels' do
expect(@collection_profile).to respond_to(:condition_of_labels)
end
specify 'identification_level' do
expect(@collection_profile).to respond_to(:identification_level)
end
specify 'arrangement_level' do
expect(@collection_profile).to respond_to(:arrangement_level)
end
specify 'data_quality' do
expect(@collection_profile).to respond_to(:data_quality)
end
specify 'computerization_level' do
expect(@collection_profile).to respond_to(:computerization_level)
end

specify 'otu' do
expect(@collection_profile).to respond_to(:otu)
end
specify 'container' do
expect(@collection_profile).to respond_to(:container)
end
end
end
end
19 changes: 13 additions & 6 deletions spec/models/container_spec.rb
Expand Up @@ -6,21 +6,28 @@

context 'associations' do
context 'has_many' do
specify "physical_collection_objects" do
specify 'physical_collection_objects' do
expect(container).to respond_to(:physical_collection_objects)
end
specify 'type' do
expect(container).to respond_to(:type)
end
end
end

context "from awesome_nested_set" do
specify "root" do
context 'from awesome_nested_set' do
specify 'root' do
expect(container).to respond_to(:root)
end
end

context "concerns" do
it_behaves_like "identifiable"
it_behaves_like "containable"
context 'validation' do
specify 'type' do
container.type = 'aaa'
expect(container.valid?).to be_false
container.type = 'Container::Drawer'
expect(container.valid?).to be_true
end
end

end

0 comments on commit 54d1642

Please sign in to comment.