Class: ProtocolRelationship

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::BatchByFilterScope, Shared::IsData, Shared::PolymorphicAnnotator
Defined in:
app/models/protocol_relationship.rb

Class Method Summary collapse

Instance Method Summary collapse

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::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Class Method Details

.process_batch_by_filter_scope(batch_response: nil, query: nil, hash_query: nil, mode: nil, params: nil, async: nil, project_id: nil, user_id: nil, called_from_async: false) ⇒ Object



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
# File 'app/models/protocol_relationship.rb', line 16

def self.process_batch_by_filter_scope(
  batch_response: nil, query: nil, hash_query: nil, mode: nil, params: nil,
  async: nil, project_id: nil, user_id: nil,
  called_from_async: false
)
  # Don't call async from async (the point is we do the same processing in
  # async and not in async, and this function handles both that processing and
  # making the async call, so it's this much janky).
  async = false if called_from_async == true
  r = batch_response

  case mode.to_sym
  when :replace
    # TODO: Return response
    if params[:replace_protocol_id].nil?
      r.errors['no replacement protocol provided'] = 1
      return r
    end

    if async && !called_from_async
      BatchByFilterScopeJob.perform_later(
        klass: self.name,
        hash_query: hash_query,
        mode:,
        params:,
        project_id:,
        user_id:
      )
    else
      ProtocolRelationship
        .where(
          protocol_relationship_object_id: query.pluck(:id),
          protocol_relationship_object_type: query.klass.name,
          protocol_id: params[:replace_protocol_id]
        ).find_each do |o|
          o.update(protocol_id: params[:protocol_id])
          if o.valid?
            r.updated.push o.id
          else
            r.not_updated.push o.id
          end
        end
    end

  when :remove
    # Just delete, async or not
    ProtocolRelationship
      .where(
        protocol_relationship_object_id: query.pluck(:id),
        protocol_relationship_object_type: query.klass.name
      ).delete_all

  when :add
    if async && !called_from_async
      BatchByFilterScopeJob.perform_later(
        klass: self.name,
        hash_query: hash_query,
        mode:,
        params:,
        project_id:,
        user_id:
      )
    else
      query.find_each do |o|
        o = ProtocolRelationship.create(protocol_relationship_object: o,
          protocol_id: params[:protocol_id])

        if o.valid?
          r.updated.push o.id
        else
          r.not_updated.push nil
        end
      end
    end

  end

  r
end

Instance Method Details

#dwc_occurrencesObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/protocol_relationship.rb', line 96

def dwc_occurrences
  case attribute_subject_type
  when 'FieldOccurrence'
    if protocol_relationship_object.protocols.pluck(&:is_machine_output).uniq == [true] # All protocols are machine, not a mix
      ::DwcOccurrence.where(
        dwc_occurrence_object_type: 'FieldOccurrence',
        dwc_occurrence_object_id: protocol_relationship_object_id)
    end
  else
    ::DwcOccurrence.none
  end
end