Class: ImpExp::Serializers::PolymorphicRelationOneToOne
- Inherits:
-
Object
- Object
- ImpExp::Serializers::PolymorphicRelationOneToOne
- Defined in:
- app/services/imp_exp/serializers/polymorphic_relation_one_to_one.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#relation_classes ⇒ Object
readonly
Returns the value of attribute relation_classes.
Instance Method Summary collapse
- #dump(record, relation_name) ⇒ Object
-
#initialize(column, relation_classes) ⇒ PolymorphicRelationOneToOne
constructor
A new instance of PolymorphicRelationOneToOne.
-
#load(data, scoping_parent:, model: nil, relation_name: nil) ⇒ Object
rubocop: disable Lint/UnusedMethodArgument.
Constructor Details
#initialize(column, relation_classes) ⇒ PolymorphicRelationOneToOne
Returns a new instance of PolymorphicRelationOneToOne.
8 9 10 11 |
# File 'app/services/imp_exp/serializers/polymorphic_relation_one_to_one.rb', line 8 def initialize(column, relation_classes) @column = column @relation_classes = relation_classes end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
6 7 8 |
# File 'app/services/imp_exp/serializers/polymorphic_relation_one_to_one.rb', line 6 def column @column end |
#relation_classes ⇒ Object (readonly)
Returns the value of attribute relation_classes.
6 7 8 |
# File 'app/services/imp_exp/serializers/polymorphic_relation_one_to_one.rb', line 6 def relation_classes @relation_classes end |
Instance Method Details
#dump(record, relation_name) ⇒ Object
13 14 15 16 17 18 |
# File 'app/services/imp_exp/serializers/polymorphic_relation_one_to_one.rb', line 13 def dump(record, relation_name) relation = record.send(relation_name) value = relation&.send(column) "#{relation.model_name.name}|#{value}" if relation && value.present? end |
#load(data, scoping_parent:, model: nil, relation_name: nil) ⇒ Object
rubocop: disable Lint/UnusedMethodArgument
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/services/imp_exp/serializers/polymorphic_relation_one_to_one.rb', line 21 def load(data, scoping_parent:, model: nil, relation_name: nil) return if data.blank? relation_class, value = data.strip.split("|") return if relation_class.blank? || value.blank? || !relation_class.in?(relation_classes) record = ImpExp::Utils::RelationScoper.call(relation_class.constantize, relation_class, scoping_parent) .find_by({ column => value }) return record if record ImpExp::Serializers::Errors::RecordNotFound.new(relation_class, { column => value }) end |