Module: Locomotive::Wagon::ToHashConcern

Instance Method Summary collapse

Instance Method Details

#prepare_value_for_hash(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb', line 19

def prepare_value_for_hash(value)
  if value.is_a?(Array) && value.any? { |v| v.respond_to?(:__attributes__) }
    value.map(&:to_hash)
  elsif value.is_a?(Array) && value.empty?
    nil # reset the array
  elsif value.respond_to?(:translations)
    !value.translations.empty? ? value.translations : nil
  else
    value
  end
end

#to_hashObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/locomotive/wagon/decorators/concerns/to_hash_concern.rb', line 7

def to_hash
  {}.tap do |hash|
    __attributes__.each do |name|
      value = self.public_send(name)

      next if value.nil?

      hash[name] = prepare_value_for_hash(value)
    end
  end
end