Skip to content

Commit

Permalink
Double synonym relationships with citations. Closes #1033
Browse files Browse the repository at this point in the history
  • Loading branch information
proceps committed Aug 29, 2019
1 parent d042b4c commit a1e1852
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
36 changes: 36 additions & 0 deletions app/models/citation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Citation < ApplicationRecord
include Shared::Tags
include Shared::IsData
include Shared::PolymorphicAnnotator

attr_accessor :no_cached

polymorphic_annotates('citation_object')

belongs_to :source, inverse_of: :citations
Expand All @@ -58,6 +61,10 @@ class Citation < ApplicationRecord
after_create :add_source_to_project
after_save :update_related_cached_values, if: :is_original?

after_save :set_cached_names_for_taxon_names, unless: -> {self.no_cached}
after_destroy :set_cached_names_for_taxon_names, unless: -> {self.no_cached}


# TODO: deprecate
# @return [Scope of matching sources]
def self.find_for_autocomplete(params)
Expand Down Expand Up @@ -123,4 +130,33 @@ def prevent_if_required
end
end

def set_cached_names_for_taxon_names
if citation_object_type == 'TaxonNameRelationship' && TAXON_NAME_RELATIONSHIP_NAMES_INVALID.include?(citation_object.type_name)
begin
TaxonNameRelationship.transaction do
t = citation_object.subject_taxon_name
vn = t.get_valid_taxon_name

t.update_columns(
cached: t.get_full_name,
cached_html: t.get_full_name_html,
cached_valid_taxon_name_id: vn.id)
t.combination_list_self.each do |c|
c.update_column(:cached_valid_taxon_name_id, vn.id)
end

vn.list_of_invalid_taxon_names.each do |s|
s.update_column(:cached_valid_taxon_name_id, vn.id)
s.combination_list_self.each do |c|
c.update_column(:cached_valid_taxon_name_id, vn.id)
end
end
end
rescue ActiveRecord::RecordInvalid
raise
end
false
end
end

end
30 changes: 15 additions & 15 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_08_13_201512) do
ActiveRecord::Schema.define(version: 2019_08_14_194707) do

# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
Expand Down Expand Up @@ -366,8 +366,8 @@
end

create_table "confidences", id: :serial, force: :cascade do |t|
t.integer "confidence_object_id", null: false
t.string "confidence_object_type", null: false
t.integer "confidence_object_id", null: false
t.integer "position", null: false
t.integer "created_by_id", null: false
t.integer "updated_by_id", null: false
Expand Down Expand Up @@ -557,8 +557,8 @@
end

create_table "documentation", id: :serial, force: :cascade do |t|
t.integer "documentation_object_id", null: false
t.string "documentation_object_type", null: false
t.integer "documentation_object_id", null: false
t.integer "document_id", null: false
t.integer "project_id", null: false
t.integer "created_by_id", null: false
Expand All @@ -576,7 +576,7 @@
create_table "documents", id: :serial, force: :cascade do |t|
t.string "document_file_file_name", null: false
t.string "document_file_content_type", null: false
t.integer "document_file_file_size", null: false
t.bigint "document_file_file_size", null: false
t.datetime "document_file_updated_at", null: false
t.integer "project_id", null: false
t.integer "created_by_id", null: false
Expand Down Expand Up @@ -762,8 +762,8 @@
t.string "vernacularName"
t.string "waterBody"
t.string "year"
t.integer "dwc_occurrence_object_id"
t.string "dwc_occurrence_object_type"
t.integer "dwc_occurrence_object_id"
t.integer "created_by_id", null: false
t.integer "updated_by_id", null: false
t.integer "project_id"
Expand Down Expand Up @@ -947,7 +947,7 @@
t.datetime "updated_at", null: false
t.string "image_file_file_name"
t.string "image_file_content_type"
t.integer "image_file_file_size"
t.bigint "image_file_file_size"
t.datetime "image_file_updated_at"
t.integer "updated_by_id", null: false
t.text "image_file_meta"
Expand Down Expand Up @@ -1007,8 +1007,8 @@
t.integer "project_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "loan_item_object_id"
t.string "loan_item_object_type"
t.integer "loan_item_object_id"
t.integer "total"
t.string "disposition"
t.index ["created_by_id"], name: "index_loan_items_on_created_by_id"
Expand Down Expand Up @@ -1037,7 +1037,7 @@
t.datetime "updated_at", null: false
t.string "recipient_honorific"
t.string "recipient_country"
t.text "lender_address", default: "Lender's address not provided.", null: false
t.text "lender_address", null: false
t.index ["created_by_id"], name: "index_loans_on_created_by_id"
t.index ["project_id"], name: "index_loans_on_project_id"
t.index ["updated_by_id"], name: "index_loans_on_updated_by_id"
Expand Down Expand Up @@ -1224,10 +1224,10 @@
end

create_table "origin_relationships", id: :serial, force: :cascade do |t|
t.integer "old_object_id", null: false
t.string "old_object_type", null: false
t.integer "new_object_id", null: false
t.integer "old_object_id", null: false
t.string "new_object_type", null: false
t.integer "new_object_id", null: false
t.integer "position"
t.integer "created_by_id", null: false
t.integer "updated_by_id", null: false
Expand Down Expand Up @@ -1308,8 +1308,8 @@
end

create_table "pinboard_items", id: :serial, force: :cascade do |t|
t.integer "pinned_object_id", null: false
t.string "pinned_object_type", null: false
t.integer "pinned_object_id", null: false
t.integer "user_id", null: false
t.integer "project_id", null: false
t.integer "position", null: false
Expand Down Expand Up @@ -1373,16 +1373,16 @@
t.datetime "updated_at", null: false
t.integer "created_by_id", null: false
t.integer "updated_by_id", null: false
t.jsonb "preferences", default: {}, null: false
t.jsonb "preferences", default: "{}", null: false
t.string "api_access_token"
t.index ["created_by_id"], name: "index_projects_on_created_by_id"
t.index ["updated_by_id"], name: "index_projects_on_updated_by_id"
end

create_table "protocol_relationships", id: :serial, force: :cascade do |t|
t.integer "protocol_id", null: false
t.integer "protocol_relationship_object_id", null: false
t.string "protocol_relationship_object_type", null: false
t.integer "protocol_relationship_object_id", null: false
t.integer "position", null: false
t.integer "created_by_id", null: false
t.integer "updated_by_id", null: false
Expand Down Expand Up @@ -1613,8 +1613,8 @@
t.string "boundary_finder", null: false
t.boolean "has_border", null: false
t.string "layout", null: false
t.jsonb "metadata_map", default: {}, null: false
t.jsonb "specimen_coordinates", default: {}, null: false
t.jsonb "metadata_map", default: "{}", null: false
t.jsonb "specimen_coordinates", default: "{}", null: false
t.integer "project_id", null: false
t.integer "created_by_id", null: false
t.integer "updated_by_id", null: false
Expand Down
24 changes: 24 additions & 0 deletions spec/models/taxon_name_specs/cached_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,30 @@
expect(combination.cached_valid_taxon_name_id).to eq(species_syn.id)
end
end

context 'two synonym relationships' do
let!(:species1) { FactoryBot.create(:relationship_species, name: 'aus', parent: genus1)}
let!(:species2) { FactoryBot.create(:relationship_species, name: 'bus', parent: genus1)}
let!(:species_syn) { FactoryBot.create(:relationship_species, name: 'cus', parent: genus1)}
let!( :r1) { FactoryBot.create(:taxon_name_relationship, subject_taxon_name: species_syn, object_taxon_name: species1, type: 'TaxonNameRelationship::Iczn::Invalidating::Synonym' )}
let!( :r2) { FactoryBot.create(:taxon_name_relationship, subject_taxon_name: species_syn, object_taxon_name: species2, type: 'TaxonNameRelationship::Iczn::Invalidating::Synonym' )}
let!(:source1) { FactoryBot.create(:soft_valid_bibtex_source_article, year: 1999) }
let!(:source2) { FactoryBot.create(:soft_valid_bibtex_source_article, year: 2000) }

specify 'valid species with one citation' do
r1.citations.create(source: source1)
species_syn.reload
expect(species_syn.cached_valid_taxon_name_id).to eq (species1.id)
end

specify 'valid species with two citation' do
r1.citations.create(source: source1)
r2.citations.create(source: source2)
species_syn.reload
expect(species_syn.cached_valid_taxon_name_id).to eq (species2.id)
end
end

end
end
end
Expand Down

0 comments on commit a1e1852

Please sign in to comment.