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.



320
321
322
323
324
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 320

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.



318
319
320
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 318

def result_include_data
  @result_include_data
end

Returns the value of attribute result_links.



318
319
320
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 318

def result_links
  @result_links
end

#serializerObject (readonly)

Returns the value of attribute serializer.



318
319
320
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 318

def serializer
  @serializer
end

Instance Method Details

#include_data(val) ⇒ Object



326
327
328
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 326

def include_data(val)
  @result_include_data = val
end

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



332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 332

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



345
346
347
348
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 345

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