Class: ActiveModel::Serializer::Reflection
- Defined in:
- lib/active_model/serializer/reflection.rb
Overview
Holds all the meta-data about an association as it was specified in the ActiveModel::Serializer class.
Specifically, the association 'comments' is evaluated two different ways:
1) as 'comments' and named 'comments'.
2) as 'object.comments.last(1)' and named 'last_comments'.
PostSerializer._reflections # =>
# {
# author: HasOneReflection.new(:author, serializer: AuthorSerializer),
# comments: HasManyReflection.new(:comments)
# last_comments: HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
# secret_meta_data: HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
# }
So you can inspect reflections in your Adapters.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from Field
Instance Method Summary collapse
-
#build_association(parent_serializer, parent_serializer_options, include_slice = {}) ⇒ Object
private
Build association.
- #collection? ⇒ Boolean
- #foreign_key_on ⇒ Object private
- #include_data(value = true) ⇒ Object
- #include_data?(include_slice) ⇒ Boolean
-
#initialize ⇒ Reflection
constructor
A new instance of Reflection.
- #link(name, value = nil, &block) ⇒ Object
- #meta(value = nil, &block) ⇒ Object
- #value(serializer, include_slice) {|ActiveModel::Serializer| ... } ⇒ :nil, associated resource or resource collection
Methods inherited from Field
Constructor Details
#initialize ⇒ Reflection
Returns a new instance of Reflection.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_model/serializer/reflection.rb', line 55 def initialize(*) super [:links] = {} [:include_data_setting] = Serializer.config.include_data_default [:meta] = nil @type = .fetch(:type) do class_name = .fetch(:class_name, name.to_s.camelize.singularize) class_name.underscore.pluralize.to_sym end @foreign_key = .fetch(:foreign_key) do if collection? "#{name.to_s.singularize}_ids".to_sym else "#{name}_id".to_sym end end end |
Instance Attribute Details
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
53 54 55 |
# File 'lib/active_model/serializer/reflection.rb', line 53 def foreign_key @foreign_key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
53 54 55 |
# File 'lib/active_model/serializer/reflection.rb', line 53 def type @type end |
Instance Method Details
#build_association(parent_serializer, parent_serializer_options, include_slice = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build association. This method is used internally to build serializer’s association by its reflection.
197 198 199 200 201 202 203 204 |
# File 'lib/active_model/serializer/reflection.rb', line 197 def build_association(parent_serializer, , include_slice = {}) = { parent_serializer: parent_serializer, parent_serializer_options: , include_slice: include_slice } Association.new(self, ) end |
#collection? ⇒ Boolean
138 139 140 |
# File 'lib/active_model/serializer/reflection.rb', line 138 def collection? false end |
#foreign_key_on ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
173 174 175 |
# File 'lib/active_model/serializer/reflection.rb', line 173 def foreign_key_on :related end |
#include_data(value = true) ⇒ Object
133 134 135 136 |
# File 'lib/active_model/serializer/reflection.rb', line 133 def include_data(value = true) [:include_data_setting] = value :nil end |
#include_data?(include_slice) ⇒ Boolean
142 143 144 145 146 147 148 149 150 |
# File 'lib/active_model/serializer/reflection.rb', line 142 def include_data?(include_slice) include_data_setting = [:include_data_setting] case include_data_setting when :if_sideloaded then include_slice.key?(.fetch(:key, name)) when true then true when false then false else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'" end end |
#link(name, value = nil, &block) ⇒ Object
90 91 92 93 |
# File 'lib/active_model/serializer/reflection.rb', line 90 def link(name, value = nil, &block) [:links][name] = block_given? ? block : value :nil end |
#meta(value = nil, &block) ⇒ Object
105 106 107 108 |
# File 'lib/active_model/serializer/reflection.rb', line 105 def (value = nil, &block) [:meta] = block_given? ? block : value :nil end |
#value(serializer, include_slice) {|ActiveModel::Serializer| ... } ⇒ :nil, associated resource or resource collection
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/active_model/serializer/reflection.rb', line 155 def value(serializer, include_slice) # NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe # Therefore, when we build associations from reflections, we dup the entire reflection instance. # Better solutions much appreciated! @object = serializer.object @scope = serializer.scope block_value = instance_exec(serializer, &block) if block return unless include_data?(include_slice) if block && block_value != :nil block_value else serializer.read_attribute_for_serialization(name) end end |