Class: Mongoid::Archivable::ProcessLocalizedFields

Inherits:
Struct
  • Object
show all
Defined in:
lib/mongoid/archivable/process_localized_fields.rb

Overview

loops through all fields on document, as well as on its embedded relations and makes sure all attributes that belong to localized fields are assigned via their *_translations writer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls, attrs = {}) ⇒ ProcessLocalizedFields

Returns a new instance of ProcessLocalizedFields.



11
12
13
# File 'lib/mongoid/archivable/process_localized_fields.rb', line 11

def initialize(cls, attrs = {})
  super(cls, attrs)
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs

Returns:

  • (Object)

    the current value of attrs



6
7
8
# File 'lib/mongoid/archivable/process_localized_fields.rb', line 6

def attrs
  @attrs
end

#clsObject

Returns the value of attribute cls

Returns:

  • (Object)

    the current value of cls



6
7
8
# File 'lib/mongoid/archivable/process_localized_fields.rb', line 6

def cls
  @cls
end

Class Method Details

.call(*args) ⇒ Object



7
8
9
# File 'lib/mongoid/archivable/process_localized_fields.rb', line 7

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/archivable/process_localized_fields.rb', line 15

def call
  attrs = convert_fields

  embedded_relations.each do |relation|
    relation_name = relation.name.to_s

    # convert embeds_many
    if attrs[relation_name].is_a?(Array)
      attrs[relation_name] = attrs[relation_name].map do |att|
        relation_class = Hash[att].fetch('_type', relation.class_name).constantize
        ProcessLocalizedFields.call(relation_class, Hash[att])
      end
    # convert embeds_one
    elsif att = attrs[relation_name]
      relation_class = Hash[att].fetch('_type', relation.class_name).constantize
      attrs[relation_name] = ProcessLocalizedFields.call(relation_class, Hash[att])
    end
  end

  attrs
end