Module: Mutils::Serialization::Methods::Relations

Defined in:
lib/mutils/serialization/methods/relations.rb

Overview

Module Relations

Instance Method Summary collapse

Instance Method Details

#prepare_options(relationship_name, options, option_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mutils/serialization/methods/relations.rb', line 23

def prepare_options(relationship_name, options, option_name)
  class_name = options[:serializer]
  if class_name.nil?
    raise "Serializer is Required for belongs_to :#{relationship_name}." \
          "\nDefine it like:\n#{option_name} :#{relationship_name}, " \
          'serializer: SERIALIZER_CLASS'
  end
  raise "Serializer class not defined for relationship: #{relationship_name}" unless class_exists? class_name

  options[:serializer] = class_name.to_s
  options[:always_include] = options[:always_include].nil? ? false : options[:always_include]
  options[:label] = Lib::Helper.instance.underscore options[:label] || relationship_name
  options
end

#relationship(relationship_name, options = {}) ⇒ Object Also known as: belongs_to, has_many, has_one



11
12
13
14
15
16
17
# File 'lib/mutils/serialization/methods/relations.rb', line 11

def relationship(relationship_name, options = {})
  raise "if: should be a Proc object for attribute #{relationship_name}" if options[:if] && !options[:if].instance_of?(Proc)

  options = prepare_options(relationship_name, options, __callee__)
  self.relationships = {} if relationships.nil?
  relationships[relationship_name] = options
end