Class: ActiveModel::Serializer::Associations::HasMany

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?, #embeddable?, #initialize, #name, #option, refine, #root, #source_serializer, #target_serializer

Constructor Details

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

Instance Method Details

#embed_keyObject



114
115
116
117
118
119
120
# File 'lib/active_model/serializer/associations.rb', line 114

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

#keyObject



104
105
106
107
108
109
110
111
112
# File 'lib/active_model/serializer/associations.rb', line 104

def key
  if key = option(:key)
    key
  elsif embed_ids?
    "#{@name.to_s.singularize}_ids".to_sym
  else
    @name
  end
end

#serializablesObject



128
129
130
131
132
# File 'lib/active_model/serializer/associations.rb', line 128

def serializables
  associated_object.map do |item|
    find_serializable(item)
  end
end

#serializeObject



122
123
124
125
126
# File 'lib/active_model/serializer/associations.rb', line 122

def serialize
  associated_object.map do |item|
    find_serializable(item).serializable_hash
  end
end

#serialize_idsObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/active_model/serializer/associations.rb', line 134

def serialize_ids
  ids_key = "#{@name.to_s.singularize}_ids".to_sym
  if !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(ids_key)
    source_serializer.object.read_attribute_for_serialization(ids_key)
  else
    associated_object.map do |item|
      item.read_attribute_for_serialization(embed_key)
    end
  end
end