Module: Raif::Concerns::RunWith

Extended by:
ActiveSupport::Concern
Included in:
Agent, Task
Defined in:
app/models/raif/concerns/run_with.rb

Class Method Summary collapse

Class Method Details

.deserialize_run_with_value(value) ⇒ Object

Inverse of serialize_run_with_value: turn GID strings (or arrays of them) back into records. RecordNotFound becomes nil so callers see the same "live link, gone-record = nil" semantics for arrays as for singles.



106
107
108
109
110
111
112
113
114
# File 'app/models/raif/concerns/run_with.rb', line 106

def self.deserialize_run_with_value(value)
  if gid_string?(value)
    locate_gid(value)
  elsif value.is_a?(Array) && value.any? && value.all? { |v| v.nil? || gid_string?(v) }
    value.map { |v| v.nil? ? nil : locate_gid(v) }
  else
    value
  end
end

.gid_string?(value) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/raif/concerns/run_with.rb', line 116

def self.gid_string?(value)
  value.is_a?(String) && value.start_with?("gid://")
end

.locate_gid(gid) ⇒ Object



120
121
122
123
124
# File 'app/models/raif/concerns/run_with.rb', line 120

def self.locate_gid(gid)
  GlobalID::Locator.locate(gid)
rescue ActiveRecord::RecordNotFound
  nil
end

.serialize_run_with_value(value) ⇒ Object

Serialize a single run_with value, converting GlobalID-able records (and arrays of them) into their GID string form so the JSONB column doesn't bloat with the full record payload.



93
94
95
96
97
98
99
100
101
# File 'app/models/raif/concerns/run_with.rb', line 93

def self.serialize_run_with_value(value)
  if value.respond_to?(:to_global_id)
    value.to_global_id.to_s
  elsif value.is_a?(Array) && value.any? && value.all? { |v| v.nil? || v.respond_to?(:to_global_id) }
    value.map { |v| v&.to_global_id&.to_s }
  else
    value
  end
end