Class: PaperTrail::Human::Adapters::Resolvers::Relation

Inherits:
Object
  • Object
show all
Includes:
Ports::Resolver
Defined in:
lib/paper_trail/human/adapters/resolvers/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(class_name: nil, attribute: :name, cache: nil, **options) ⇒ Relation

Returns a new instance of Relation.



10
11
12
13
14
# File 'lib/paper_trail/human/adapters/resolvers/relation.rb', line 10

def initialize(class_name: nil, attribute: :name, cache: nil, **options)
  @class_name = class_name || options[:class].to_s
  @attribute = attribute
  @cache = cache || {}
end

Instance Method Details

#resolve(value) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/paper_trail/human/adapters/resolvers/relation.rb', line 16

def resolve(value)
  return @cache[value] || @cache[value.to_i] || value if @cache.any?

  klass = safe_const_get(@class_name)
  return value unless klass

  record = klass.find_by(id: value)
  record&.public_send(@attribute) || value
end