Class: ActiveModel::Serializer::Association::HasOne
Overview
Instance Attribute Summary
#embed_ids, #embed_in_root, #embed_objects, #name, #object
Instance Method Summary
collapse
#key
Constructor Details
#initialize(name, options = {}, serializer_options = {}) ⇒ HasOne
Returns a new instance of HasOne.
120
121
122
123
|
# File 'lib/active_model/serializer/associations.rb', line 120
def initialize(name, options={}, serializer_options={})
super
@polymorphic = options[:polymorphic]
end
|
Instance Method Details
#embeddable? ⇒ Boolean
139
140
141
|
# File 'lib/active_model/serializer/associations.rb', line 139
def embeddable?
super || !polymorphic?
end
|
#id_key ⇒ Object
135
136
137
|
# File 'lib/active_model/serializer/associations.rb', line 135
def id_key
"#{name}_id".to_sym
end
|
#root ⇒ Object
125
126
127
128
129
130
131
132
133
|
# File 'lib/active_model/serializer/associations.rb', line 125
def root
if root = options[:root]
root
elsif polymorphic?
object.class.to_s.pluralize.demodulize.underscore.to_sym
else
name.to_s.pluralize.to_sym
end
end
|
#serializables ⇒ Object
143
144
145
146
|
# File 'lib/active_model/serializer/associations.rb', line 143
def serializables
value = object && find_serializable(object)
value ? [value] : []
end
|
#serialize ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/active_model/serializer/associations.rb', line 148
def serialize
if object
if polymorphic?
{
:type => polymorphic_key,
polymorphic_key => find_serializable(object).serializable_hash
}
else
find_serializable(object).serializable_hash
end
end
end
|
#serialize_ids ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/active_model/serializer/associations.rb', line 161
def serialize_ids
if object
serializer = find_serializable(object)
id =
if serializer.respond_to?(embed_key)
serializer.send(embed_key)
else
object.read_attribute_for_serialization(embed_key)
end
if polymorphic?
{
type: polymorphic_key,
id: id
}
else
id
end
end
end
|