Class: LcpRuby::Presenter::FieldValueResolver
- Inherits:
-
Object
- Object
- LcpRuby::Presenter::FieldValueResolver
- Includes:
- MetadataLookup
- Defined in:
- lib/lcp_ruby/presenter/field_value_resolver.rb
Instance Attribute Summary collapse
-
#model_definition ⇒ Object
readonly
Returns the value of attribute model_definition.
-
#permission_evaluator ⇒ Object
readonly
Returns the value of attribute permission_evaluator.
Class Method Summary collapse
-
.association_label(record) ⇒ Object
Coerce an associated record to a display string.
- .dot_path?(field) ⇒ Boolean
- .template_field?(field) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(model_definition, permission_evaluator) ⇒ FieldValueResolver
constructor
A new instance of FieldValueResolver.
-
#resolve(record, field_path, fk_map: {}, raw: false) ⇒ Object?
Resolve a field value from a record using various path types.
Constructor Details
#initialize(model_definition, permission_evaluator) ⇒ FieldValueResolver
Returns a new instance of FieldValueResolver.
8 9 10 11 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 8 def initialize(model_definition, ) @model_definition = model_definition @permission_evaluator = end |
Instance Attribute Details
#model_definition ⇒ Object (readonly)
Returns the value of attribute model_definition.
6 7 8 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 6 def model_definition @model_definition end |
#permission_evaluator ⇒ Object (readonly)
Returns the value of attribute permission_evaluator.
6 7 8 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 6 def @permission_evaluator end |
Class Method Details
.association_label(record) ⇒ Object
Coerce an associated record to a display string. Callers that get back an AR instance (FK resolution, terminal-belongs_to dot-path, plain belongs_to field) run through this to get a human-readable label.
51 52 53 54 55 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 51 def self.association_label(record) return nil if record.nil? record.respond_to?(:to_label) ? record.to_label : record.to_s end |
.dot_path?(field) ⇒ Boolean
40 41 42 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 40 def self.dot_path?(field) field.to_s.include?(".") && !field.to_s.include?("{") end |
.template_field?(field) ⇒ Boolean
44 45 46 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 44 def self.template_field?(field) field.to_s.include?("{") && field.to_s.include?("}") end |
Instance Method Details
#resolve(record, field_path, fk_map: {}, raw: false) ⇒ Object?
Resolve a field value from a record using various path types.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 21 def resolve(record, field_path, fk_map: {}, raw: false) field_path = field_path.to_s return nil if field_path.blank? if self.class.template_field?(field_path) resolve_template(record, field_path, fk_map: fk_map) elsif self.class.dot_path?(field_path) resolve_dot_path(record, field_path, raw: raw) elsif virtual_column_field?(field_path) resolve_virtual_column(record, field_path) elsif fk_map.key?(field_path) resolve_fk(record, fk_map[field_path]) elsif (assoc = model_definition.find_belongs_to(field_path)) resolve_association_record(record, assoc.name) else resolve_simple(record, field_path, raw: raw) end end |