Module: NestedAttributesSerializer

Defined in:
lib/nested_attributes_serializer.rb,
lib/nested_attributes_serializer/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rename_includes(hash, options) ⇒ Object

Renames the keys of associations to conform to rails nested attributes naming convention. This is a implemented as a module method in order to not pollute the method namespace of the including class/module.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nested_attributes_serializer.rb', line 20

def self.rename_includes(hash, options)
  # inspired by activemodel/lib/active_model/serialization.rb
  return unless includes = options[:include]

  unless includes.is_a?(Hash)
    includes = Hash[Array(includes).map { |n| n.is_a?(Hash) ? n.to_a.first : [n, {}] }]
  end

  includes.each do |association, opts|
    old_key = association.to_s
    new_key = "#{old_key}_attributes"
    hash[new_key] = hash.delete(old_key)
    rename_includes(hash[new_key], opts)
  end
end

Instance Method Details

#as_nested_attributes(options = {}) ⇒ Object

Returns a hash representing the model. It accepts the some options as serializable_hash.



6
7
8
9
10
# File 'lib/nested_attributes_serializer.rb', line 6

def as_nested_attributes(options = {})
  hash = serializable_hash(options)
  NestedAttributesSerializer.rename_includes(hash, options)
  hash
end

#to_nested_attributes(*args) ⇒ Object

see #as_nested_attributes



13
14
15
# File 'lib/nested_attributes_serializer.rb', line 13

def to_nested_attributes(*args)
  as_nested_attributes(*args)
end