Class: JsonApiReflection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers/adapter/json_api_pg.rb

Overview

Wraps what we know about a reflection. Includes the ActiveRecord::Reflection, the ActiveModel::Serializer::Reflection, and the JsonApiReflectionReceiver results (i.e. the contents of a has_many block from the serializer definition).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ar_class, serializer_class) ⇒ JsonApiReflection

‘ar_class` should be the source ActiveRecord class, so that `ar_class.name` is one or more things of `klass`.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 251

def initialize(name, ar_class, serializer_class)
  @name = name
  @ar_class = ar_class
  @original_name = @ar_class.instance_method(name).original_name

  @serializer_reflection = serializer_class._reflections[name.to_sym]

  @ar_reflection = ar_class.reflections[name.to_s]
  @reflection_sql = nil
  if @ar_reflection.nil?
    # See if it's an alias:
    @ar_reflection = ar_class.reflections[@original_name.to_s]
  end
  if @ar_reflection.nil?
    m = "#{name}__sql".to_sym
    if ar_class.respond_to? m
      rel = ar_class.send(m)
      # Must be an ActiveRecord::Relation (or ActiveModel::Base) so we can determine klass
      @reflection_sql = rel
      @klass = ActiveRecord::Relation === rel ? rel.klass : rel
    else
      raise "Can't find an association named #{name} for class #{ar_class.name}"
    end
  else
    @klass = @ar_reflection.klass
  end
  @include_data = true
  @links = {}

  if serializer_reflection.try(:block).present?
    x = JsonApiReflectionReceiver.new(serializer_class)
    x.instance_eval &serializer_reflection.block
    @include_data = x.result_include_data
    @links = x.result_links
  end
end

Instance Attribute Details

#ar_classObject (readonly)

Returns the value of attribute ar_class.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def ar_class
  @ar_class
end

#ar_reflectionObject (readonly)

Returns the value of attribute ar_reflection.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def ar_reflection
  @ar_reflection
end

#include_dataObject (readonly)

Returns the value of attribute include_data.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def include_data
  @include_data
end

#klassObject (readonly)

Returns the value of attribute klass.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def klass
  @klass
end

Returns the value of attribute links.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def links
  @links
end

#nameObject (readonly)

Returns the value of attribute name.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def name
  @name
end

#original_nameObject (readonly)

Returns the value of attribute original_name.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def original_name
  @original_name
end

#reflection_sqlObject (readonly)

Returns the value of attribute reflection_sql.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def reflection_sql
  @reflection_sql
end

#serializer_reflectionObject (readonly)

Returns the value of attribute serializer_reflection.



245
246
247
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 245

def serializer_reflection
  @serializer_reflection
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


288
289
290
291
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 288

def belongs_to?
  ar_reflection.is_a? ActiveRecord::Reflection::BelongsToReflection
  # TODO: fall back to AMS reflection
end

#has_many?Boolean

Returns:

  • (Boolean)


293
294
295
296
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 293

def has_many?
  ar_reflection.try(:is_a?, ActiveRecord::Reflection::HasManyReflection) ||
    serializer_reflection.is_a?(ActiveModel::Serializer::HasManyReflection)
end

#has_one?Boolean

Returns:

  • (Boolean)


298
299
300
301
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 298

def has_one?
  ar_reflection.is_a? ActiveRecord::Reflection::HasOneReflection
  # TODO: fall back to AMS reflection
end