Skip to content

Commit

Permalink
BicurationGroups. Tag positioning. ControlledVocabulary related facto…
Browse files Browse the repository at this point in the history
…ries and specs.
  • Loading branch information
mjy committed Apr 8, 2014
1 parent 94c76fc commit c480b96
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -8,7 +8,7 @@ gem 'pg', '~> 0.17.0'

# Postgis
gem 'activerecord-postgis-adapter'
gem 'squeel'
gem 'squeel', git: 'https://github.com/nybex/squeel.git'

# rgeo support
gem 'ffi-geos'
Expand Down
7 changes: 4 additions & 3 deletions app/models/biocuration_class.rb
@@ -1,6 +1,7 @@
class BiocurationClass < ControlledVocabularyTerm

has_many :biocuration_classifications
has_many :biological_objects

include Shared::Taggable

has_many :biocuration_classifications
has_many :biological_collection_objects, through: :biocuration_classifications, class_name: 'CollectionObject::BiologicalCollectionObject'
end
2 changes: 0 additions & 2 deletions app/models/biocuration_classification.rb
Expand Up @@ -6,6 +6,4 @@ class BiocurationClassification < ActiveRecord::Base

validates_presence_of :biocuration_class, :biological_collection_object

# TODO: scope to 'organismal/biological axis'

end
7 changes: 7 additions & 0 deletions app/models/biocuration_group.rb
@@ -0,0 +1,7 @@
class BiocurationGroup < Keyword

# Watch out this is borked with squeel
has_many :biocuration_classes, through: :tags, source: 'tag_object', source_type: 'ControlledVocabularyTerm'


end
1 change: 1 addition & 0 deletions app/models/concerns/shared/taggable.rb
Expand Up @@ -3,6 +3,7 @@ module Shared::Taggable

included do
has_many :tags, as: :tag_object, validate: false
has_many :keywords, through: :tags
end

def tagged?
Expand Down
2 changes: 2 additions & 0 deletions app/models/container.rb
Expand Up @@ -4,8 +4,10 @@ class Container < ActiveRecord::Base
include Housekeeping
include Shared::Identifiable
include Shared::Containable
include Shared::Taggable
include SoftValidation


# TODO: rethinking this
# belongs_to :otu

Expand Down
1 change: 1 addition & 0 deletions app/models/controlled_vocabulary_term.rb
@@ -1,6 +1,7 @@
class ControlledVocabularyTerm < ActiveRecord::Base
include Housekeeping
include Shared::AlternateValues
# include Shared::Taggable <- !! NO

validates_presence_of :name, :definition, :type
validates_length_of :definition, minimum: 4
Expand Down
7 changes: 7 additions & 0 deletions app/models/keyword.rb
@@ -1,2 +1,9 @@
class Keyword < ControlledVocabularyTerm

has_many :tags, foreign_key: :keyword_id

def tagged_objects
self.tags.collect{|t| t.tag_object}
end

end
2 changes: 1 addition & 1 deletion app/models/protonym.rb
Expand Up @@ -55,7 +55,7 @@ class Protonym < TaxonName
scope :named, -> (name) {where(name: name)}
scope :with_name_in_array, -> (array) { where('name in (?)', array) }

# find classifications for taxon
# find classifications for taxon
scope :with_taxon_name_classifications_on_taxon_name, -> (t) { includes(:taxon_name_classifications).where('taxon_name_classifications.taxon_name_id = ?', t).references(:taxon_name_classifications) }

# find taxa with classifications
Expand Down
5 changes: 4 additions & 1 deletion app/models/tag.rb
@@ -1,10 +1,13 @@
class Tag < ActiveRecord::Base
include Housekeeping

acts_as_list scope: [:keyword_id]

belongs_to :keyword
belongs_to :tag_object, polymorphic: true

validates :tag_object, presence: true
validates :keyword, presence: true
validates_uniqueness_of :keyword_id, scope: [:tag_object]
validates_uniqueness_of :keyword_id, scope: [ :tag_object_id, :tag_object_type]

end
5 changes: 5 additions & 0 deletions db/migrate/20140407134323_add_position_to_tags.rb
@@ -0,0 +1,5 @@
class AddPositionToTags < ActiveRecord::Migration
def change
add_column :tags, :position, :integer
end
end
7 changes: 4 additions & 3 deletions spec/factories/biocuration_classification_factory.rb
@@ -1,7 +1,8 @@
FactoryGirl.define do
factory :biocuration_classification, traits: [:housekeeping] do
biocuration_class nil
biological_collection_object nil
position 1
factory :valid_biocuration_classification do
association :biocuration_class, factory: :valid_biocuration_class
association :biological_collection_object, factory: :valid_specimen
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/biocuration_group_factory.rb
@@ -0,0 +1,8 @@
FactoryGirl.define do
factory :biocuration_group, traits: [:housekeeping] do
factory :valid_biocuration_group do
name 'life stage'
definition 'A group of life stages.'
end
end
end
5 changes: 5 additions & 0 deletions spec/factories/container/box_factory.rb
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :container_box, class: Container::Box, traits: [:housekeeping] do
factory :valid_container_box
end
end
15 changes: 5 additions & 10 deletions spec/models/biocuration_class_spec.rb
Expand Up @@ -7,21 +7,16 @@
context 'associations' do
context 'has_many' do
specify 'biocuration_classifications' do
expect(biocuration_class).to respond_to(:biocuration_classifications)
expect(biocuration_class.biocuration_classifications << FactoryGirl.build(:valid_biocuration_classification)).to be_true
end

specify 'biological_objects' do
expect(biocuration_class).to respond_to(:biological_objects)
expect(biocuration_class.biological_collection_objects << FactoryGirl.build(:valid_specimen)).to be_true
end
end
end

context 'validations' do
before(:each) do
biocuration_class.valid?
end
specify 'name is required' do
expect(biocuration_class.errors.include?(:name)).to be_true
specify 'tags' do
expect(biocuration_class.tags << FactoryGirl.build(:valid_tag)).to be_true
end
end
end

Expand Down
31 changes: 31 additions & 0 deletions spec/models/biocuration_group_spec.rb
@@ -0,0 +1,31 @@
require 'spec_helper'

describe BiocurationGroup do

let(:biocuration_group) {FactoryGirl.build(:biocuration_group)}

context 'has many' do
before(:each) {
@biocuration_group = FactoryGirl.create(:valid_biocuration_group, name: 'Stages.', definition: 'Life stages.')
@bc1 = BiocurationClass.create(name: 'Adult', definition: 'A mature indivdiual.')
@bc2 = BiocurationClass.create(name: 'Larva', definition: 'Not a mature indivdiual.')
}

specify 'tags (through Keywords)' do
expect(biocuration_group.tags << Tag.new(keyword: FactoryGirl.create(:valid_keyword))).to be_true
end

specify 'biocuration_classes' do
expect(biocuration_group).to respond_to(:biocuration_classes)
t1 =Tag.create(keyword: @biocuration_group, tag_object: @bc1)
t2 = Tag.create(keyword: @biocuration_group, tag_object: @bc2)

# Something to not match
t3 = Tag.create(keyword: FactoryGirl.create(:valid_keyword), tag_object: @bc1)

expect(@biocuration_group.biocuration_classes).to have(2).things
expect(@biocuration_group.biocuration_classes.to_a.map(&:class)).to eq([BiocurationClass, BiocurationClass])
end
end
end

34 changes: 29 additions & 5 deletions spec/models/keyword_spec.rb
@@ -1,15 +1,39 @@
require 'spec_helper'

describe Keyword do
let(:topic) { Keyword.new }
let(:keyword) { FactoryGirl.build(:keyword) }

context "associations" do
context "validation" do
before(:each) { @k = FactoryGirl.create(:valid_keyword) }

specify "can be used for tags" do
t = Tag.new(keyword: @k, tag_object: FactoryGirl.build(:valid_otu))
expect(t.save).to be_true
end

specify "can not be used for other things" do
expect {c = CitationTopic.new(topic: @k)}.to raise_error
end
end

context "validation" do
specify "can be used for tags"
specify "can not be used for citations"
context 'associations' do
context 'has_many' do
specify 'tags' do
expect(keyword.tags << FactoryGirl.build(:valid_tag)).to be_true
end
end
end

specify 'tagged_objects' do
expect(keyword).to respond_to(:tagged_objects)
k = FactoryGirl.create(:valid_keyword)
t1 = Tag.create(keyword: k, tag_object: FactoryGirl.create(:valid_otu))
t2 = Tag.create(keyword: k, tag_object: FactoryGirl.create(:valid_specimen))
expect(k.tagged_objects).to have(2).things
expect(k.tags).to have(2).things
end



end

39 changes: 37 additions & 2 deletions spec/models/tag_spec.rb
Expand Up @@ -6,7 +6,11 @@

context 'associations' do
specify 'tag_object' do
expect(tag).to respond_to(:tag_object)
expect(tag.tag_object = FactoryGirl.create(:valid_biocuration_class)).to be_true
end

specify 'keyword' do
expect(tag.keyword = FactoryGirl.create(:valid_keyword)).to be_true
end
end

Expand All @@ -24,7 +28,8 @@
end

specify 'a topic can not be used' do
pending
t = Topic.new(name: 'foo', definition: 'Something about foo')
expect{tag.keyword = t}.to raise_error
end

specify 'a tagged object is only tagged once per keyword' do
Expand All @@ -41,6 +46,36 @@
expect(dupe_tag.errors.include?(:keyword_id)).to be_true
end

context 'STI based tag behaviour' do
before(:each) {
tag.keyword = FactoryGirl.create(:valid_keyword)
tag.tag_object = FactoryGirl.create(:valid_specimen)
}

specify 'tagging an subclass of an STI model instance *stores* the tag_type as the superclass' do
expect(tag.save).to be_true
expect(tag.tag_object_type).to eq('CollectionObject')
end

specify 'tagging an subclass of an STI model instance, with subclass namespace, *stores* the tag_type as the superclass' do
tag.tag_object = FactoryGirl.create(:valid_container_box)
expect(tag.save).to be_true
expect(tag.tag_object_type).to eq('Container')
end

specify 'tagging a subclass of an STI model *returns* the subclassed object' do
expect(tag.save).to be_true
expect(tag.tag_object.class).to eq(Specimen)
end
end

context 'acts_as_list' do
specify 'position is set' do
t1 = FactoryGirl.create(:valid_tag)
expect(t1.position).to eq(1)
end
end

# TODO: Determine if we want to tag individual fields.
# specify 'a attribute is actually a attribute of the tag object'
end
Expand Down

0 comments on commit c480b96

Please sign in to comment.