class ActiveModelSerializers::Adapter::JsonApiPg

Public Class Methods

new(serializer, options={}) click to toggle source
Calls superclass method
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 21
def initialize(serializer, options={})
  super
end

Private Class Methods

warn_about_collection_serializer() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 79
def self.warn_about_collection_serializer
    msg = "You are using an ordinary AMS CollectionSerializer with the json_api_pg adapter, which probably means Rails is pointlessly loading all your ActiveRecord instances *and* running the JSON-building query in Postgres."
    if Object.const_defined? 'Rails'
      Rails.logger.warn msg
    else
      puts "WARN: #{msg}"
    end
end

Public Instance Methods

relation() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 38
def relation
  @relation ||= _relation
end
to_json(options={}) click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 25
def to_json(options={})
  if relation.nil?
    ret = { data: [] }
    if includes.any?
      # TODO: Can included ever be non-empty when the main data is empty?
      ret[:included] = []
    end
    ret.to_json
  else
    connection.select_value serializer_sql
  end
end

Private Instance Methods

_relation() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 52
def _relation
  o = serializer.object
  case o
  when ActiveRecord::Relation
    o
  when Array
    o2 = o.first
    if o2.nil?
      nil
    else
      o2.class.where(id: o.map(&:id))
    end
  when ActiveRecord::Base
    o.class.where(id: o.id)
  else
    raise "not sure what to do with #{o.class}: #{o}"
  end
end
connection() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 48
def connection
  @connection ||= relation.connection
end
includes() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 44
def includes
  instance_options && instance_options[:include] || []
end
serializer_sql() click to toggle source
# File lib/active_model_serializers/adapter/json_api_pg.rb, line 71
def serializer_sql
  # TODO: There should be a better way....
  opts = serializer.instance_variable_get("@options") || {}
  sql = JsonApiPgSql.new(serializer, relation, instance_options, opts)
  sql = sql.to_sql
  sql
end