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) ⇒ Object
- #meta(value = nil) ⇒ 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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_model/serializer/reflection.rb', line 53 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.
51 52 53 |
# File 'lib/active_model/serializer/reflection.rb', line 51 def foreign_key @foreign_key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
51 52 53 |
# File 'lib/active_model/serializer/reflection.rb', line 51 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.
192 193 194 195 196 197 198 199 |
# File 'lib/active_model/serializer/reflection.rb', line 192 def build_association(parent_serializer, , include_slice = {}) = { parent_serializer: parent_serializer, parent_serializer_options: , include_slice: include_slice } Association.new(self, ) end |
#collection? ⇒ Boolean
136 137 138 |
# File 'lib/active_model/serializer/reflection.rb', line 136 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.
168 169 170 |
# File 'lib/active_model/serializer/reflection.rb', line 168 def foreign_key_on :related end |
#include_data(value = true) ⇒ Object
131 132 133 134 |
# File 'lib/active_model/serializer/reflection.rb', line 131 def include_data(value = true) [:include_data_setting] = value :nil end |
#include_data?(include_slice) ⇒ Boolean
140 141 142 143 144 145 146 147 148 |
# File 'lib/active_model/serializer/reflection.rb', line 140 def include_data?(include_slice) include_data_setting = [:include_data_setting] case include_data_setting when :if_sideloaded then include_slice.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) ⇒ Object
88 89 90 91 |
# File 'lib/active_model/serializer/reflection.rb', line 88 def link(name, value = nil) [:links][name] = block_given? ? Proc.new : value :nil end |
#meta(value = nil) ⇒ Object
103 104 105 106 |
# File 'lib/active_model/serializer/reflection.rb', line 103 def (value = nil) [:meta] = block_given? ? Proc.new : value :nil end |
#value(serializer, include_slice) {|ActiveModel::Serializer| ... } ⇒ :nil, associated resource or resource collection
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/active_model/serializer/reflection.rb', line 153 def value(serializer, include_slice) @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 |