Class: ActiveModel::Serializer

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/active_model/serializer.rb,
lib/active_model/serializer/config.rb,
lib/active_model/serializer/version.rb,
lib/active_model/serializer/associations.rb

Defined Under Namespace

Classes: Association, Config

Constant Summary collapse

EMBED_IN_ROOT_OPTIONS =
[
  :include,
  :embed_in_root,
  :embed_in_root_key,
  :embed_namespace
].freeze
CONFIG =

:nodoc:

Config.new('embed' => :objects)
VERSION =
'0.9.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#as_json, #serializable_data

Constructor Details

#initialize(object, options = {}) ⇒ Serializer

Returns a new instance of Serializer.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/active_model/serializer.rb', line 129

def initialize(object, options={})
  @object        = object
  @scope         = options[:scope]
  @root          = options.fetch(:root, self.class._root)
  @meta_key      = options[:meta_key] || :meta
  @meta          = options[@meta_key]
  @wrap_in_array = options[:_wrap_in_array]
  @only          = options[:only] ? Array(options[:only]) : nil
  @except        = options[:except] ? Array(options[:except]) : nil
  @key_format    = options[:key_format]
  @context       = options[:context]
end

Class Attribute Details

._associationsObject

Returns the value of attribute _associations.



88
89
90
# File 'lib/active_model/serializer.rb', line 88

def _associations
  @_associations
end

._attributesObject

Returns the value of attribute _attributes.



88
89
90
# File 'lib/active_model/serializer.rb', line 88

def _attributes
  @_attributes
end

._rootObject

Returns the value of attribute _root.



88
89
90
# File 'lib/active_model/serializer.rb', line 88

def _root
  @_root
end

.key_formatObject (readonly)

Returns the value of attribute key_format.



56
57
58
# File 'lib/active_model/serializer.rb', line 56

def key_format
  @key_format
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def context
  @context
end

#key_formatObject

Returns the value of attribute key_format.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def key_format
  @key_format
end

#metaObject

Returns the value of attribute meta.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def meta
  @meta
end

#meta_keyObject

Returns the value of attribute meta_key.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def meta_key
  @meta_key
end

#objectObject

Returns the value of attribute object.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def object
  @object
end

#rootObject

Returns the value of attribute root.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def root
  @root
end

#scopeObject

Returns the value of attribute scope.



141
142
143
# File 'lib/active_model/serializer.rb', line 141

def scope
  @scope
end

Class Method Details

.attributes(*attrs) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/active_model/serializer.rb', line 96

def attributes(*attrs)
  @_attributes.concat attrs

  attrs.each do |attr|
    define_method attr do
      object.read_attribute_for_serialization attr
    end unless method_defined?(attr)
  end
end

.embed(type, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_model/serializer.rb', line 34

def embed(type, options={})
  CONFIG.embed = type
  if EMBED_IN_ROOT_OPTIONS.any? { |opt| options[opt].present? }
    CONFIG.embed_in_root = true
  end
  if options[:embed_in_root_key].present?
    CONFIG.embed_in_root_key = options[:embed_in_root_key]
  end
  ActiveSupport::Deprecation.warn <<-WARN
** Notice: embed is deprecated. **
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
Please use the global .setup method instead:
ActiveModel::Serializer.setup do |config|
  config.embed = :#{type}
  config.embed_in_root = #{CONFIG.embed_in_root || false}
end
  WARN
end

.format_keys(format) ⇒ Object



53
54
55
# File 'lib/active_model/serializer.rb', line 53

def format_keys(format)
  @key_format = format
end

.has_many(*attrs) ⇒ Object



110
111
112
# File 'lib/active_model/serializer.rb', line 110

def has_many(*attrs)
  associate(Association::HasMany, *attrs)
end

.has_one(*attrs) ⇒ Object



106
107
108
# File 'lib/active_model/serializer.rb', line 106

def has_one(*attrs)
  associate(Association::HasOne, *attrs)
end

.inherited(base) ⇒ Object



15
16
17
18
19
# File 'lib/active_model/serializer.rb', line 15

def inherited(base)
  base._root = _root
  base._attributes = (_attributes || []).dup
  base._associations = (_associations || {}).dup
end

.root_nameObject



92
93
94
# File 'lib/active_model/serializer.rb', line 92

def root_name
  name.demodulize.underscore.sub(/_serializer$/, '') if name
end

.setupObject



21
22
23
24
25
# File 'lib/active_model/serializer.rb', line 21

def setup
  @mutex.synchronize do
    yield CONFIG
  end
end

Instance Method Details

#associationsObject



159
160
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.rb', line 159

def associations
  associations = self.class._associations
  included_associations = filter(associations.keys)
  associations.each_with_object({}) do |(name, association), hash|
    if included_associations.include? name
      if association.embed_ids?
        ids = serialize_ids association
        if association.embed_namespace?
          hash = hash[association.embed_namespace] ||= {}
          hash[association.key] = ids
        else
          hash[association.key] = ids
        end
      elsif association.embed_objects?
        if association.embed_namespace?
          hash = hash[association.embed_namespace] ||= {}
        end
        hash[association.embedded_key] = serialize association
      end
    end
  end
end

#attributesObject



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

def attributes
  filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
    hash[name] = send(name)
  end
end

#build_serializer(association) ⇒ Object



216
217
218
219
# File 'lib/active_model/serializer.rb', line 216

def build_serializer(association)
  object = send(association.name)
  association.build_serializer(object, scope: scope)
end

#convert_keys(hash) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/active_model/serializer.rb', line 246

def convert_keys(hash)
  Hash[hash.map do |k,v|
    key = if k.is_a?(Symbol)
      format_key(k).to_sym
    else
      format_key(k)
    end

    [key ,v]
  end]
end

#embedded_in_root_associationsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/active_model/serializer.rb', line 192

def embedded_in_root_associations
  associations = self.class._associations
  included_associations = filter(associations.keys)
  associations.each_with_object({}) do |(name, association), hash|
    if included_associations.include? name
      if association.embed_in_root?
        if association.embed_in_root_key?
          hash = hash[association.embed_in_root_key] ||= {}
        end
        association_serializer = build_serializer(association)
        hash.merge!(association_serializer.embedded_in_root_associations) {|key, oldval, newval| [newval, oldval].flatten }

        serialized_data = association_serializer.serializable_object
        key = association.root_key
        if hash.has_key?(key)
          hash[key].concat(serialized_data).uniq!
        else
          hash[key] = serialized_data
        end
      end
    end
  end
end

#filter(keys) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/active_model/serializer.rb', line 182

def filter(keys)
  if @only
    keys & @only
  elsif @except
    keys - @except
  else
    keys
  end
end

#format_key(key) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/active_model/serializer.rb', line 238

def format_key(key)
  if key_format == :lower_camel
    key.to_s.camelize(:lower)
  else
    key
  end
end

#json_keyObject



143
144
145
146
147
148
149
150
151
# File 'lib/active_model/serializer.rb', line 143

def json_key
  key = if root == true || root.nil?
    self.class.root_name
  else
    root
  end

  key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
end

#serializable_object(options = {}) ⇒ Object Also known as: serializable_hash



258
259
260
261
262
263
264
# File 'lib/active_model/serializer.rb', line 258

def serializable_object(options={})
  return @wrap_in_array ? [] : nil if @object.nil?
  hash = attributes
  hash.merge! associations
  hash = convert_keys(hash) if key_format.present?
  @wrap_in_array ? [hash] : hash
end

#serialize(association) ⇒ Object



221
222
223
# File 'lib/active_model/serializer.rb', line 221

def serialize(association)
  build_serializer(association).serializable_object
end

#serialize_ids(association) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/active_model/serializer.rb', line 225

def serialize_ids(association)
  associated_data = send(association.name)
  if associated_data.respond_to?(:to_ary)
    associated_data.map { |elem| elem.read_attribute_for_serialization(association.embed_key) }
  else
    associated_data.read_attribute_for_serialization(association.embed_key) if associated_data
  end
end

#serializer_for(resource) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_model/serializer.rb', line 59

def serializer_for(resource)
  if resource.respond_to?(:to_ary)
    if Object.constants.include?(:ArraySerializer)
      ::ArraySerializer
    else
      ArraySerializer
    end
  else
    begin
      Object.const_get "#{resource.class.name}Serializer"
    rescue NameError
      nil
    end
  end
end