Class: InternalAttribute

Inherits:
DataAttribute show all
Defined in:
app/models/internal_attribute.rb

Constant Summary

Constants included from Shared::DualAnnotator

Shared::DualAnnotator::ALWAYS_COMMUNITY

Instance Attribute Summary collapse

Attributes inherited from DataAttribute

#attribute_subject_id, #attribute_subject_type, #project_id, #type, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataAttribute

#editable?, find_for_autocomplete, #predicate_name

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#controlled_vocabulary_term_idid

The the id of the ControlledVocabularyTerm::Predicate. Term is referenced as #predicate.

Returns:

  • (id)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/internal_attribute.rb', line 10

class InternalAttribute < DataAttribute
  validates_presence_of :predicate
  validates_uniqueness_of :value, scope: [:attribute_subject_id, :attribute_subject_type, :type, :controlled_vocabulary_term_id, :project_id]

  after_save :update_dwc_occurrences

  # TODO: wrap in generic (reindex_dwc_occurrences method for use in InternalAttribute and elsewhere)
  # TODO: perhaps a Job
  def update_dwc_occurrences
    if DWC_ATTRIBUTE_URIS.values.flatten.include?(predicate.uri)

      if attribute_subject.respond_to?(:set_dwc_occurrence)
        attribute_subject.set_dwc_occurrence
      end

      if attribute_subject.respond_to?(:update_dwc_occurrences)
        attribute_subject.update_dwc_occurrences
      end
    end
  end

  def self.batch_create(params)
    ids = params[:attribute_subject_id]
    params.delete(:attribute_subject_id)

    internal_attributes = []
    InternalAttribute.transaction do
      begin
        ids.each do |id|
          internal_attributes.push InternalAttribute.create!(
            params.merge(
              attribute_subject_id: id
            )
          )
        end
      rescue ActiveRecord::RecordInvalid
        return false
      end
    end
    internal_attributes
  end
end

Class Method Details

.batch_create(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/internal_attribute.rb', line 31

def self.batch_create(params)
  ids = params[:attribute_subject_id]
  params.delete(:attribute_subject_id)

  internal_attributes = []
  InternalAttribute.transaction do
    begin
      ids.each do |id|
        internal_attributes.push InternalAttribute.create!(
          params.merge(
            attribute_subject_id: id
          )
        )
      end
    rescue ActiveRecord::RecordInvalid
      return false
    end
  end
  internal_attributes
end

Instance Method Details

#update_dwc_occurrencesObject

TODO: wrap in generic (reindex_dwc_occurrences method for use in InternalAttribute and elsewhere) TODO: perhaps a Job



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/internal_attribute.rb', line 18

def update_dwc_occurrences
  if DWC_ATTRIBUTE_URIS.values.flatten.include?(predicate.uri)

    if attribute_subject.respond_to?(:set_dwc_occurrence)
      attribute_subject.set_dwc_occurrence
    end

    if attribute_subject.respond_to?(:update_dwc_occurrences)
      attribute_subject.update_dwc_occurrences
    end
  end
end