Module: EasyCompliance::Ref

Defined in:
lib/easy_compliance/ref.rb

Overview

allows identifying records after submission

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.app_nameObject



31
32
33
# File 'lib/easy_compliance/ref.rb', line 31

def app_name
  EasyCompliance.app_name or raise Error, 'must set app_name'
end

.for(record_class:, record_id:) ⇒ String

Returns:

  • (String)


14
15
16
# File 'lib/easy_compliance/ref.rb', line 14

def for(record_class:, record_id:)
  [app_name, record_class, record_id].join('#')
end

.for_record(record) ⇒ String

Returns:

  • (String)


9
10
11
# File 'lib/easy_compliance/ref.rb', line 9

def for_record(record)
  self.for(record_class: record.class.name, record_id: record.id)
end

.look_up(ref) ⇒ ActiveRecord::Base | NilClass

Converts a previously created Ref string back into a record.

Returns:

  • (ActiveRecord::Base | NilClass)


21
22
23
24
25
26
27
28
29
# File 'lib/easy_compliance/ref.rb', line 21

def look_up(ref)
  app_name, record_class, record_id = ref.split('#')
  app_name == self.app_name or raise Error, "Ref `#{ref}` is from wrong app"
  begin
    record_class.constantize.find_by(id: record_id)
  rescue NameError, NoMethodError => e
    raise Error, "Ref `#{ref}` not supported: #{e.message}"
  end
end