2N/Aclass LocIdBaseModel < ActiveRecord::Base
2N/A @abstract_class = true
2N/A after_create :create_locid
2N/A before_destroy :destroy_locid
2N/A
2N/A # There is no polymorphic has_one
2N/A has_many :loc_ids, as: :specific
2N/A
2N/A def self.find_with_locid(locid, iri = nil)
2N/A result = LocId.where(locid: locid).first.try(:specific)
2N/A if table_name == 'ontologies' && result.nil? && iri
2N/A result = AlternativeIri.where('iri LIKE ?', '%' << iri).
2N/A first.try(:ontology)
2N/A end
2N/A result
2N/A end
2N/A
2N/A def create_locid
2N/A LocId.where(locid: generate_locid_string,
2N/A specific_id: id,
58N/A specific_type: normalized_class.to_s).first_or_create!
2N/A end
2N/A
2N/A # To be overwritten in the subclasses.
2N/A def generate_locid_string
23N/A nil
23N/A end
23N/A
23N/A def destroy_locid
23N/A # When reanalysing an ontology in the migrations (because of duplicates),
32N/A # the locid can already be nil.
32N/A loc_ids.first.try(:destroy)
32N/A end
34N/A
32N/A def locid
34N/A loc_ids.first.try(:locid)
32N/A end
32N/A
58N/A def locid=(string)
32N/A if locid = loc_ids.first
2N/A locid.update_attributes(locid: string)
38N/A else
38N/A LocId.create(specific_id: id,
38N/A specific_type: normalized_class.to_s,
38N/A locid: string)
38N/A end
38N/A end
38N/A
38N/A def normalized_class
38N/A # Sometimes the objects are "Ontology" and sometimes a subclass.
83N/A if [DistributedOntology, SingleOntology].include?(self.class)
83N/A Ontology
99N/A elsif Sentence.descendants.include?(self.class)
2N/A Sentence
92N/A else
92N/A self.class
92N/A end
2N/A end
2N/Aend
83N/A