Module: Vets::Model

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON, Attributes
Included in:
Preneeds::Base, Response
Defined in:
lib/vets/model.rb

Instance Method Summary collapse

Methods included from Attributes

included

Instance Method Details

#attributesObject

Acts as ActiveRecord::Base#attributes which is needed for serialization



33
34
35
# File 'lib/vets/model.rb', line 33

def attributes
  nested_attributes(instance_values)
end

#initialize(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/vets/model.rb', line 21

def initialize(params = {})
  # Remove attributes that aren't defined in the class aka unknown
  params.select! { |x| self.class.attribute_set.index(x.to_sym) }

  super
  # Ensure all attributes have a defined value (default to nil)
  self.class.attribute_set.each do |attr_name|
    instance_variable_set("@#{attr_name}", nil) unless instance_variable_defined?("@#{attr_name}")
  end
end

#nested_attributes(values) ⇒ Hash (private)

Collect values from attribute and nested objects

Parameters:

  • values (Hash)

Returns:

  • (Hash)

    nested attributes



44
45
46
47
48
49
50
51
52
# File 'lib/vets/model.rb', line 44

def nested_attributes(values)
  values.transform_values do |value|
    if value.respond_to?(:attributes)
      nested_attributes(value.instance_values)
    else
      value
    end
  end
end