Class: JsonApiReflection
- Inherits:
-
Object
- Object
- JsonApiReflection
- 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
-
#ar_class ⇒ Object
readonly
Returns the value of attribute ar_class.
-
#ar_reflection ⇒ Object
readonly
Returns the value of attribute ar_reflection.
-
#include_data ⇒ Object
readonly
Returns the value of attribute include_data.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#original_name ⇒ Object
readonly
Returns the value of attribute original_name.
-
#reflection_sql ⇒ Object
readonly
Returns the value of attribute reflection_sql.
-
#serializer_reflection ⇒ Object
readonly
Returns the value of attribute serializer_reflection.
Instance Method Summary collapse
- #belongs_to? ⇒ Boolean
- #has_many? ⇒ Boolean
- #has_one? ⇒ Boolean
-
#initialize(name, ar_class, serializer_class) ⇒ JsonApiReflection
constructor
ar_classshould be the source ActiveRecord class, so thatar_class.nameis one or more things ofklass.
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.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 265 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_class ⇒ Object (readonly)
Returns the value of attribute ar_class.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def ar_class @ar_class end |
#ar_reflection ⇒ Object (readonly)
Returns the value of attribute ar_reflection.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def ar_reflection @ar_reflection end |
#include_data ⇒ Object (readonly)
Returns the value of attribute include_data.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def include_data @include_data end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def klass @klass end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def links @links end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def name @name end |
#original_name ⇒ Object (readonly)
Returns the value of attribute original_name.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def original_name @original_name end |
#reflection_sql ⇒ Object (readonly)
Returns the value of attribute reflection_sql.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def reflection_sql @reflection_sql end |
#serializer_reflection ⇒ Object (readonly)
Returns the value of attribute serializer_reflection.
259 260 261 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 259 def serializer_reflection @serializer_reflection end |
Instance Method Details
#belongs_to? ⇒ Boolean
302 303 304 305 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 302 def belongs_to? ar_reflection.is_a? ActiveRecord::Reflection::BelongsToReflection # TODO: fall back to AMS reflection end |
#has_many? ⇒ Boolean
307 308 309 310 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 307 def has_many? ar_reflection.try(:is_a?, ActiveRecord::Reflection::HasManyReflection) || serializer_reflection.is_a?(ActiveModel::Serializer::HasManyReflection) end |
#has_one? ⇒ Boolean
312 313 314 315 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 312 def has_one? ar_reflection.is_a? ActiveRecord::Reflection::HasOneReflection # TODO: fall back to AMS reflection end |