module Caprese::Serializer::Relationships::ClassMethods

Public Instance Methods

belongs_to(name, options = {}, &block) click to toggle source
Calls superclass method
# File lib/caprese/serializer/concerns/relationships.rb, line 46
def belongs_to(name, options = {}, &block)
  super(
    name,
    merge_serializer_option(name, options),
    &build_association_block(name)
  )
end
has_many(name, options = {}, &block) click to toggle source
Calls superclass method
# File lib/caprese/serializer/concerns/relationships.rb, line 30
def has_many(name, options = {}, &block)
  super(
    name,
    merge_serializer_option(name, options),
    &build_association_block(name)
  )
end
has_one(name, options = {}, &block) click to toggle source
Calls superclass method
# File lib/caprese/serializer/concerns/relationships.rb, line 38
def has_one(name, options = {}, &block)
  super(
    name,
    merge_serializer_option(name, options),
    &build_association_block(name)
  )
end

Private Instance Methods

build_association_block(reflection_name) click to toggle source

Builds a block that is passed into an association when it is defined in a specific serializer The block is run, and links are added to each association so when it is rendered in the `relationships` object of the `data` for record, it contains links to the particular association

@example

object = Order<@token=5, @product_id=10>
reflection_name = 'product'
# => {
  id: 'asd27h',
  type: 'orders',
  relationships: {
    product: {
      id: 'hy7sql',
      type: 'products',
      links: {
        self: '/api/v1/orders/asd27h/relationships/product',
        related: '/api/v1/orders/asd27h/product'
      }
    }
  }
}

@param [String] reflection_name the name of the relationship @return [Block] a block to build links for the relationship

# File lib/caprese/serializer/concerns/relationships.rb, line 80
def build_association_block(reflection_name)
  primary_key = Caprese.config.resource_primary_key

  reflection_name = reflection_name.to_sym

  Proc.new do |serializer|
    link :self do
      url = "relationship_definition_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
      if serializer.url_helpers.respond_to? url
        serializer.url_helpers.send(
          url,
          id: object.read_attribute(primary_key),
          relationship: reflection_name,
          host: serializer.class.send(:caprese_default_url_options_host)
        )
      end
    end

    link :related do
      url = "relationship_data_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
      if serializer.url_helpers.respond_to? url
        serializer.url_helpers.send(
          url,
          id: object.read_attribute(primary_key),
          relationship: reflection_name,
          host: serializer.class.send(:caprese_default_url_options_host)
        )
      end
    end

    serializer.relationship_scope(reflection_name, object.send(object.class.caprese_unalias_field(name)))
  end
end
merge_serializer_option(name, options) click to toggle source

Adds a default serializer for relationship based on the relationship name

@example

has_many :answers => AnswerSerializer

@param [String] name the name of the relationship @param [Hash] options options to add default serializer to @return [Hash] the default options

# File lib/caprese/serializer/concerns/relationships.rb, line 122
def merge_serializer_option(name, options)
  { serializer: get_serializer_for(name.to_s.classify) }.merge(options)
end