Class: ActiveModel::Serializer::Associations::HasOne

Inherits:
Config
  • Object
show all
Defined in:
lib/active_model/serializer/associations.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from Config

#associated_object, #embed_ids?, #embed_in_root?, #embed_objects?, #initialize, #name, #option, refine, #source_serializer, #target_serializer

Constructor Details

This class inherits a constructor from ActiveModel::Serializer::Associations::Config

Instance Method Details

#embed_keyObject



179
180
181
182
183
184
185
# File 'lib/active_model/serializer/associations.rb', line 179

def embed_key
  if key = option(:embed_key)
    key
  else
    :id
  end
end

#embeddable?Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
# File 'lib/active_model/serializer/associations.rb', line 147

def embeddable?
  if polymorphic? && associated_object.nil?
    false
  else
    true
  end
end

#keyObject



169
170
171
172
173
174
175
176
177
# File 'lib/active_model/serializer/associations.rb', line 169

def key
  if key = option(:key)
    key
  elsif embed_ids? && !polymorphic?
    "#{@name}_id".to_sym
  else
    @name
  end
end

#polymorphic?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/active_model/serializer/associations.rb', line 155

def polymorphic?
  option :polymorphic
end

#polymorphic_keyObject



187
188
189
# File 'lib/active_model/serializer/associations.rb', line 187

def polymorphic_key
  associated_object.class.to_s.demodulize.underscore.to_sym
end

#rootObject



159
160
161
162
163
164
165
166
167
# File 'lib/active_model/serializer/associations.rb', line 159

def root
  if root = option(:root)
    root
  elsif polymorphic?
    associated_object.class.to_s.pluralize.demodulize.underscore.to_sym
  else
    @name.to_s.pluralize.to_sym
  end
end

#serializablesObject



204
205
206
207
208
# File 'lib/active_model/serializer/associations.rb', line 204

def serializables
  object = associated_object
  value = object && find_serializable(object)
  value ? [value] : []
end

#serializeObject



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/active_model/serializer/associations.rb', line 191

def serialize
  object = associated_object

  if object && polymorphic?
    {
      :type => polymorphic_key,
      polymorphic_key => find_serializable(object).serializable_hash
    }
  elsif object
    find_serializable(object).serializable_hash
  end
end

#serialize_idsObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/active_model/serializer/associations.rb', line 210

def serialize_ids
  id_key = "#{@name}_id".to_sym

  if polymorphic?
    if associated_object
      {
        :type => polymorphic_key,
        :id => associated_object.read_attribute_for_serialization(embed_key)
      }
    else
      nil
    end
  elsif !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key)
    source_serializer.object.read_attribute_for_serialization(id_key)
  elsif associated_object
    associated_object.read_attribute_for_serialization(embed_key)
  else
    nil
  end
end