Class: PaperTrail::Human::Adapters::Resolvers::Relation
- Inherits:
-
Object
- Object
- PaperTrail::Human::Adapters::Resolvers::Relation
- Includes:
- Ports::Resolver
- Defined in:
- lib/paper_trail/human/adapters/resolvers/relation.rb
Instance Method Summary collapse
-
#initialize(class_name: nil, attribute: :name, cache: nil, **options) ⇒ Relation
constructor
A new instance of Relation.
- #resolve(value) ⇒ Object
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, **) @class_name = class_name || [: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 |