class ZuoraConnectUi::Serializer::Relationship
The configuration for a relationship between models
Attributes
id_method_name[R]
key may be plural, record_type
will always be singular
key[R]
key may be plural, record_type
will always be singular
plural_type[R]
key may be plural, record_type
will always be singular
record_type[R]
key may be plural, record_type
will always be singular
relationship_type[R]
key may be plural, record_type
will always be singular
Public Class Methods
new(key:, relationship_type:)
click to toggle source
# File lib/zuora_connect_ui/serializer/relationship.rb, line 11 def initialize(key:, relationship_type:) @camel_fields = {} @key = key @relationship_type = relationship_type set_id_method_name end
Public Instance Methods
serialize(resources, fields)
click to toggle source
# File lib/zuora_connect_ui/serializer/relationship.rb, line 19 def serialize(resources, fields) relationship_hash = {} resources.each do |resource| relations = resource.public_send(key) next if relations.nil? relations = [relations] unless @relationship_type == :has_many relations.each do |relation| next if relationship_hash.key? relation.id relationship_hash[relation.id] = record_hash(relation, fields) end end relationship_hash end
Private Instance Methods
key_transform(input)
click to toggle source
# File lib/zuora_connect_ui/serializer/relationship.rb, line 66 def key_transform(input) input.to_s.camelize(:lower).to_sym end
record_hash(resource, fields)
click to toggle source
# File lib/zuora_connect_ui/serializer/relationship.rb, line 41 def record_hash(resource, fields) hash = { id: resource.id } fields.each do |field| key = @camel_fields[field] ||= key_transform(field) hash[key] = resource.public_send(field) end hash end
set_id_method_name()
click to toggle source
# File lib/zuora_connect_ui/serializer/relationship.rb, line 52 def set_id_method_name if @relationship_type == :has_many singular_key = @key.to_s.singularize id_postfix = '_ids' else singular_key = @key.to_s id_postfix = '_id' end @record_type = singular_key.to_sym @plural_type = singular_key.pluralize.to_sym @id_method_name = "#{singular_key}#{id_postfix}".to_sym end