Module: Caprese::Serializer::Lookup::ClassMethods

Defined in:
lib/caprese/serializer/concerns/lookup.rb

Instance Method Summary collapse

Instance Method Details

#route_for(record) ⇒ String, Nil

Gets a versioned route for a given record

Parameters:

  • record (ActiveRecord::Base)

    the record to get a route for

Returns:

  • (String, Nil)

    the route for the given record



42
43
44
45
46
# File 'lib/caprese/serializer/concerns/lookup.rb', line 42

def route_for(record)
  return nil unless record

  get_route_for(record.class)
end

#serializer_for(record, options = {}) ⇒ Serializer, Nil

Note:

Overrides the AMS default since the default does not do namespaced lookup

Gets a versioned serializer for a given record

Parameters:

  • record (ActiveRecord::Base)

    the record to get a serializer for

  • options (Hash) (defaults to: {})

    options to use when getting the serializer

Returns:

  • (Serializer, Nil)

    the serializer for the given record



16
17
18
19
20
21
22
# File 'lib/caprese/serializer/concerns/lookup.rb', line 16

def serializer_for(record, options = {})
  return ActiveModel::Serializer::CollectionSerializer if record.respond_to?(:to_ary)

  if valid_for_serialization(record)
    options.fetch(:serializer) { get_serializer_for(record.class) }
  end
end

#valid_for_serialization(record) ⇒ True

Note:

The only requirement right now is that the record model has Caprese::Record included

Indicates whether or not the record specified can be serialized by Caprese

Parameters:

  • record (Object)

    the record to check if is valid for serialization

Returns:

  • (True)

    this method either returns true, or fails - breaking control flow



30
31
32
33
34
35
36
# File 'lib/caprese/serializer/concerns/lookup.rb', line 30

def valid_for_serialization(record)
  if record && !record.class.included_modules.include?(Caprese::Record)
    fail 'All models managed by Caprese must include Caprese::Record'
  end

  true
end