Module: Shared::IsData

Extended by:
ActiveSupport::Concern
Included in:
AlternateValue, AssertedDistribution, BiocurationClassification, BiologicalAssociation, BiologicalAssociationsBiologicalAssociationsGraph, BiologicalAssociationsGraph, BiologicalRelationship, BiologicalRelationshipType, CharacterState, Citation, CitationTopic, CollectingEvent, CollectionObject, CollectionObjectObservation, CollectionProfile, CommonName, Confidence, Container, ContainerItem, Content, ControlledVocabularyTerm, DataAttribute, Depiction, DerivedCollectionObject, Descriptor, Document, Documentation, Extract, GeneAttribute, GeographicArea, GeographicAreaType, GeographicAreasGeographicItem, GeographicItem, Georeference, Identifier, Image, Language, Loan, LoanItem, Namespace, Note, Observation, ObservationMatrix, ObservationMatrixColumn, ObservationMatrixColumnItem, ObservationMatrixRow, ObservationMatrixRowItem, OriginRelationship, Otu, OtuPageLayout, OtuPageLayoutSection, Person, PreparationType, ProjectMember, ProjectSource, Protocol, ProtocolRelationship, RangedLotCategory, Repository, Role, Sequence, SequenceRelationship, Serial, SerialChronology, Source, Tag, TaggedSectionKeyword, TaxonDetermination, TaxonName, TaxonNameClassification, TaxonNameRelationship, TypeMaterial
Defined in:
app/models/concerns/shared/is_data.rb

Overview

Shared code for a classes that are “data” sensu TaxonWorks (things like Projects, users, and preferences are not data).

!! This module must in included last !!

Defined Under Namespace

Modules: ClassMethods, Stripper

Instance Method Summary (collapse)

Instance Method Details

- (Hash) errors_excepting(*keys)

Parameters:

  • keys (Symbol)

Returns:

  • (Hash)


103
104
105
106
107
108
109
# File 'app/models/concerns/shared/is_data.rb', line 103

def errors_excepting(*keys)
  valid?
  keys.each do |k|
    errors.delete(k)
  end
  errors
end

- (Array) full_error_messages_excepting(*keys)

Parameters:

  • keys (Symbol)

Returns:

  • (Array)


139
140
141
# File 'app/models/concerns/shared/is_data.rb', line 139

def full_error_messages_excepting(*keys)
  errors_excepting(*keys).full_messages
end

- (Scope) identical

Returns:

  • (Scope)


126
127
128
129
130
131
132
133
134
135
# File 'app/models/concerns/shared/is_data.rb', line 126

def identical
  klass = self.class
  attr  = Stripper.strip_identical_attributes(klass, attributes)
  if id
    scope = klass.where(attr).not_self(self)
  else
    scope = klass.where(attr)
  end
  scope
end

- (Boolean) is_community?

Returns:

  • (Boolean)


84
85
86
# File 'app/models/concerns/shared/is_data.rb', line 84

def is_community?
  self.class < Shared::SharedAcrossProjects ? true : false
end

- (Boolean) is_in_use?

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/concerns/shared/is_data.rb', line 89

def is_in_use?
  self.class.reflect_on_all_associations(:has_many).each do |r|
    return true if self.send(r.name).count > 0
  end

  self.class.reflect_on_all_associations(:has_one).each do |r|
    return true if self.send(r.name).count > 0
  end

  false
end

- (Object) metamorphosize

Returns the same object, but namespaced to the base class used many places, might be good target for optimization

Returns:

  • (Object)

    the same object, but namespaced to the base class used many places, might be good target for optimization



78
79
80
81
# File 'app/models/concerns/shared/is_data.rb', line 78

def metamorphosize
  return self if self.class.descends_from_active_record?
  self.becomes(self.class.base_class)
end

- (Scope) similar

Returns:

  • (Scope)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/concerns/shared/is_data.rb', line 112

def similar
  klass = self.class
  attr  = Stripper.strip_similar_attributes(klass, attributes)
  # matching only those attributes from the instance which are not empty
  attr = attr.select { |_kee, val| val.present? }
  if id
    scope = klass.where(attr).not_self(self)
  else
    scope = klass.where(attr)
  end
  scope
end