Skip to content

Commit

Permalink
Adds a test to validate parent at higher rank
Browse files Browse the repository at this point in the history
  • Loading branch information
dceades committed Oct 14, 2013
1 parent 4d32861 commit 5288934
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app/models/nomenclatural_rank/iczn/genus_group.rb
@@ -1,7 +1,7 @@
class NomenclaturalRank::Iczn::GenusGroup < NomenclaturalRank::Iczn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') if not(taxon_name.name = taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') if not(taxon_name.name == taxon_name.name.capitalize)
end

end
10 changes: 9 additions & 1 deletion app/models/taxon_name.rb
Expand Up @@ -14,7 +14,8 @@ class TaxonName < ActiveRecord::Base
before_validation :set_type_if_empty,
:check_format_of_name,
:validate_rank_class_class,
:validate_source_type
:validate_source_type,
:validate_parent_rank_is_higher

after_validation :set_cached_name

Expand Down Expand Up @@ -48,6 +49,13 @@ def set_cached_name
true
end

def validate_parent_rank_is_higher
return true if self.parent.nil? || self.parent.rank_class == NomenclaturalRank
if RANKS.index(self.rank_class) < RANKS.index(self.parent.rank_class)
errors.add(:parent_id, "parent rank (#{self.parent.rank_class.rank_name}) is not higher than #{self.rank_class.rank_name}")
end
end

def validate_source_type
if !self.source.nil?
errors.add(:source_id, "source must be a Bibtex") if self.source.type != 'Source::Bibtex'
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -8,6 +8,8 @@
</head>
<body>

<h1> * TaxonWorks *</h1>

<%= yield %>

</body>
Expand Down
2 changes: 1 addition & 1 deletion app/views/taxon_names/demo.erb
Expand Up @@ -2,6 +2,6 @@
<div>
<%= @taxon_name.name.to_s -%> <br />
<% @taxon_name.children.each do |child| -%>
<%= child.name.to_s -%> <br />
<%= child.name.to_s -%> [ <%= child.rank_class.rank_name[0..2].capitalize -%> ] <br />
<% end %>
</div>
84 changes: 42 additions & 42 deletions spec/factories/taxon_name.rb
@@ -1,131 +1,131 @@
FactoryGirl.define do

factory :iczn_taxon_name, class: Protonym do
name "Aus"
rank_class Ranks.lookup(:iczn, "genus")
name 'Aus'
rank_class Ranks.lookup(:iczn, 'genus')
end

factory :root_taxon_name, class: Protonym do
name "Root"
name 'Root'
rank_class NomenclaturalRank
parent_id nil
end

# TODO: use parent rather than parent_id parent should use an association to a factory generator.
factory :iczn_order, class: Protonym do
name "Hemiptera"
name 'Hemiptera'
parent nil
cached_name nil
cached_author_year nil
cached_higher_classification "Hemiptera"
cached_higher_classification 'Hemiptera'
source_id nil
year_of_publication nil
author nil
rank_class Ranks.lookup(:iczn, "order")
rank_class Ranks.lookup(:iczn, 'order')
type 'Protonym'
end

factory :iczn_family, class: Protonym do
name "Cicadellidae"
name 'Cicadellidae'
association :parent, factory: :iczn_order
cached_name nil
cached_author_year nil
cached_higher_classification "Hemiptera:Cicadellidae"
cached_higher_classification 'Hemiptera:Cicadellidae'
source_id nil
year_of_publication nil
author nil
rank_class Ranks.lookup(:iczn, "Family")
rank_class Ranks.lookup(:iczn, 'Family')
type 'Protonym'
end

factory :iczn_subfamily, class: Protonym do
name "Typhlocybinae"
name 'Typhlocybinae'
association :parent, factory: :iczn_family
cached_name nil
cached_author_year nil
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae"
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae'
source_id nil
year_of_publication nil
author nil
rank_class Ranks.lookup(:iczn, "Subfamily")
rank_class Ranks.lookup(:iczn, 'Subfamily')
type 'Protonym'
end

factory :iczn_tribe, class: Protonym do
name "Erythroneurini"
name 'Erythroneurini'
association :parent, factory: :iczn_subfamily
cached_name nil
cached_author_year nil
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini"
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id nil
year_of_publication nil
author nil
rank_class Ranks.lookup(:iczn, "Tribe")
rank_class Ranks.lookup(:iczn, 'Tribe')
type 'Protonym'
end

factory :iczn_subtribe, class: Protonym do
name "Aainaini" # TODO: Used to be "Aaina", added suffix "ini" to make the validation pass, please review logic or just delete this comment if previous value was incorrect
name 'Aainaini' # TODO: Used to be 'Aaina', added suffix 'ini' to make the validation pass, please review logic or just delete this comment if previous value was incorrect
association :parent, factory: :iczn_tribe
cached_name nil
cached_author_year nil
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini:Aaina"
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini:Aaina'
source_id nil
year_of_publication nil
author nil
rank_class Ranks.lookup(:iczn, "Tribe")
rank_class Ranks.lookup(:iczn, 'Tribe')
type 'Protonym'
end

factory :iczn_genus, class: Protonym do
name "Erythroneura"
name 'Erythroneura'
association :parent, factory: :iczn_tribe
cached_name "Erythroneura"
cached_author_year "Say, 1850"
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini"
cached_name 'Erythroneura'
cached_author_year 'Say, 1850'
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id nil
year_of_publication 1850
author "Say"
rank_class Ranks.lookup(:iczn, "Genus")
author 'Say'
rank_class Ranks.lookup(:iczn, 'Genus')
type 'Protonym'
end

factory :iczn_subgenus, class: Protonym do
name "Erythroneura"
name 'Erythroneura'
association :parent, factory: :iczn_genus
cached_name "Erythroneura (Erythroneura)"
cached_author_year "Say, 1850"
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini"
cached_name 'Erythroneura (Erythroneura)'
cached_author_year 'Say, 1850'
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id nil
year_of_publication 1850
author "Say"
rank_class Ranks.lookup(:iczn, "Subgenus")
author 'Say'
rank_class Ranks.lookup(:iczn, 'Subgenus')
type 'Protonym'
end

factory :iczn_species, class: Protonym do
name "aaa"
name 'aaa'
association :parent, factory: :iczn_subgenus
cached_name "Erythroneura (Erythroneura) aaa"
cached_author_year "McAtee, 1900"
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini"
cached_name 'Erythroneura (Erythroneura) aaa'
cached_author_year 'McAtee, 1900'
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id 10
year_of_publication 1900
author "McAtee"
rank_class Ranks.lookup(:iczn, "SPECIES")
author 'McAtee'
rank_class Ranks.lookup(:iczn, 'SPECIES')
type 'Protonym'
end

factory :iczn_subspecies, class: Protonym do
name "bbb"
name 'bbb'
association :parent, factory: :iczn_species
cached_name "Erythroneura (Erythroneura) aaa bbb"
cached_author_year "McAtee, 1900"
cached_higher_classification "Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini"
cached_name 'Erythroneura (Erythroneura) aaa bbb'
cached_author_year 'McAtee, 1900'
cached_higher_classification 'Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id 10
year_of_publication 1900
author "McAtee"
rank_class Ranks.lookup(:iczn, "subspecies")
author 'McAtee'
rank_class Ranks.lookup(:iczn, 'subspecies')
type 'Protonym'
end

Expand Down
16 changes: 13 additions & 3 deletions spec/models/taxon_name_spec.rb
Expand Up @@ -31,6 +31,15 @@
specify "type" do
expect(taxon_name.type).to eq('Protonym')
end

specify 'parent rank is higher' do
taxon_name.update(rank_class: Ranks.lookup(:iczn, 'Genus'),
name: 'Aus')
taxon_name.parent = FactoryGirl.build(:iczn_species)
taxon_name.valid?
expect(taxon_name.errors.include?(:parent_id)).to be_true
end

end

context "source" do
Expand Down Expand Up @@ -117,11 +126,12 @@
end
end

context "hierarchy" do
context "rank related" do
context "ancestor_at_rank" do
context 'hierarchy' do
context 'rank related' do
context 'ancestor_at_rank' do
genus = FactoryGirl.create(:iczn_genus)
subspecies = FactoryGirl.create(:iczn_subspecies)


specify "returns an ancestor at given rank" do
expect(subspecies.ancestor_at_rank('family').name).to eq('Cicadellidae')
Expand Down

0 comments on commit 5288934

Please sign in to comment.