Class: JsonApiReflectionReceiver

Inherits:
Object
  • Object
show all
Includes:
ActiveModelSerializers::SerializationContext::UrlHelpers
Defined in:
lib/active_model_serializers/adapter/json_api_pg.rb

Overview

We use this when a serializer has a reflection with a block argument, like this:

has_many :users do
  include_data false
  link(:related) { users_company_path(object) }
end

The only way to find out what options get set in that block is to run it, so this class does that and records what is there.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer) ⇒ JsonApiReflectionReceiver

Returns a new instance of JsonApiReflectionReceiver.



334
335
336
337
338
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 334

def initialize(serializer)
  @serializer = serializer
  @result_include_data = true
  @result_links = {}
end

Instance Attribute Details

#result_include_dataObject (readonly)

Returns the value of attribute result_include_data.



332
333
334
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 332

def result_include_data
  @result_include_data
end

Returns the value of attribute result_links.



332
333
334
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 332

def result_links
  @result_links
end

#serializerObject (readonly)

Returns the value of attribute serializer.



332
333
334
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 332

def serializer
  @serializer
end

Instance Method Details

#include_data(val) ⇒ Object



340
341
342
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 340

def include_data(val)
  @result_include_data = val
end

Users may pass either a string or a block, so we accept both.



346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 346

def link(name, val=nil, &block)
  if not val.nil?
    @result_links[name] = val
  else
    lnk = ActiveModelSerializers::Adapter::JsonApi::Link.new(serializer.new(object), block)
    # TODO: Super hacky here, and only supports one level of path resources:
    template = lnk.as_json
    @result_links[name] = template.split("PARAM").map{|p| "'#{p}'"}
    # @result_links[name] = "CONCAT(template
    # @result_links[name] = instance_eval(&block)
  end
end

#objectObject



359
360
361
362
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 359

def object
  # TODO: Could even be a singleton
  JsonApiObjectProxy.new
end