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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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

  # @params attribute_scope a Query::X::Filter instance
  #   that is NOT Query::DataAttribute::Filter
  def self.update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to)
    return false if controlled_vocabulary_term_id.nil? || value_from.nil? || value_to.nil?
    b = ::Queries::DataAttribute::Filter.new(
      attribute_scope.query_name.to_sym => attribute_scope.params,
      controlled_vocabulary_term_id:,
      value: value_from,
    )

    b.all.update_all(value: value_to)
  end

  # Add attributes to the objects in the filter that do not have them
  #
  # @return attribute_scope a Filter instance
  def self.add_value(attribute_scope, controlled_vocabulary_term_id, value_to)
    return false if controlled_vocabulary_term_id.nil? || value_to.nil?

    # with (not these)
    a = attribute_scope.all.joins(:internal_attributes).where(
      data_attributes: {
        value: value_to,
        controlled_vocabulary_term_id:
      })

    # If the join is empty, we need to add to all
    if a.size == 0
      b = attribute_scope.all
    else
      s = 'WITH query_with_ia AS (' + a.to_sql + ') ' +
        attribute_scope.referenced_klass
        .joins("LEFT JOIN query_with_ia as query_with_ia1 ON query_with_ia1.id = #{attribute_scope.table.name}.id")
        .where("query_with_ia1.id is null")
        .to_sql

      b = attribute_scope.referenced_klass.from('(' + s + ") as #{attribute_scope.table.name}").distinct
    end

    # stop-gap, we shouldn't hit this
    return false if b.size > 1000

    InternalAttribute.transaction do
      b.each do |o|
        begin
          ::InternalAttribute.create!(
            controlled_vocabulary_term_id:,
            value: value_to,
            attribute_subject: o)
        rescue ActiveRecord::RecordInvalid => e
          # TODO: check necessity of this
          # This should not be necessary, but proceed
        end
      end
    end
  end

  # <some_object>_query={}&value_from=123&value_to=456&predicate_id=890
  def self.batch_update_or_create(params)
    a = Queries::Query::Filter.base_filter(params)

    q = params.keys.select{|z| z =~ /_query/}.first

    return false if q.nil?

    b = a.new(params[q])

    s = b.all.count

    return false if b.params.empty? || s > 1000 || s == 0

    transaction do
      update_value(b, params[:predicate_id], params[:value_from], params[:value_to])
      add_value(b, params[:predicate_id], params[:value_to])
    end

    true
  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

.add_value(attribute_scope, controlled_vocabulary_term_id, value_to) ⇒ Object

Add attributes to the objects in the filter that do not have them

Returns:

  • attribute_scope a Filter instance



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/internal_attribute.rb', line 47

def self.add_value(attribute_scope, controlled_vocabulary_term_id, value_to)
  return false if controlled_vocabulary_term_id.nil? || value_to.nil?

  # with (not these)
  a = attribute_scope.all.joins(:internal_attributes).where(
    data_attributes: {
      value: value_to,
      controlled_vocabulary_term_id:
    })

  # If the join is empty, we need to add to all
  if a.size == 0
    b = attribute_scope.all
  else
    s = 'WITH query_with_ia AS (' + a.to_sql + ') ' +
      attribute_scope.referenced_klass
      .joins("LEFT JOIN query_with_ia as query_with_ia1 ON query_with_ia1.id = #{attribute_scope.table.name}.id")
      .where("query_with_ia1.id is null")
      .to_sql

    b = attribute_scope.referenced_klass.from('(' + s + ") as #{attribute_scope.table.name}").distinct
  end

  # stop-gap, we shouldn't hit this
  return false if b.size > 1000

  InternalAttribute.transaction do
    b.each do |o|
      begin
        ::InternalAttribute.create!(
          controlled_vocabulary_term_id:,
          value: value_to,
          attribute_subject: o)
      rescue ActiveRecord::RecordInvalid => e
        # TODO: check necessity of this
        # This should not be necessary, but proceed
      end
    end
  end
end

.batch_create(params) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/internal_attribute.rb', line 110

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

.batch_update_or_create(params) ⇒ Object

<some_object>_query={}&value_from=123&value_to=456&predicate_id=890



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/internal_attribute.rb', line 89

def self.batch_update_or_create(params)
  a = Queries::Query::Filter.base_filter(params)

  q = params.keys.select{|z| z =~ /_query/}.first

  return false if q.nil?

  b = a.new(params[q])

  s = b.all.count

  return false if b.params.empty? || s > 1000 || s == 0

  transaction do
    update_value(b, params[:predicate_id], params[:value_from], params[:value_to])
    add_value(b, params[:predicate_id], params[:value_to])
  end

  true
end

.update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'app/models/internal_attribute.rb', line 33

def self.update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to)
  return false if controlled_vocabulary_term_id.nil? || value_from.nil? || value_to.nil?
  b = ::Queries::DataAttribute::Filter.new(
    attribute_scope.query_name.to_sym => attribute_scope.params,
    controlled_vocabulary_term_id:,
    value: value_from,
  )

  b.all.update_all(value: value_to)
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