Module: Her::Model::Parse::ClassMethods

Defined in:
lib/her_extension/model/parse.rb

Instance Method Summary collapse

Instance Method Details

#embeded_params(attributes) ⇒ Object

TODO: Handle has_one



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/her_extension/model/parse.rb', line 39

def embeded_params(attributes)
  associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association|
    params = attributes[association[:data_key]].map(&:to_params)
    # <PATCH> - Return hash
    next hash if params.empty?
    # </PATCH>
    if association[:class_name].constantize.include_root_in_json?
      root = association[:class_name].constantize.root_element
      hash[association[:data_key]] = params.map { |n| n[root] }
    else
      hash[association[:data_key]] = params
    end
    hash
  end
end

#to_params(attributes, changes = nil) ⇒ Object



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

def to_params(attributes, changes = nil)
  filtered_attributes = attributes.dup.symbolize_keys
  filtered_attributes.merge!(embeded_params(attributes) || {})
  if her_api && her_api.options[:send_only_modified_attributes] && !changes.nil?
    filtered_attributes = changes.symbolize_keys.keys.inject({}) do |hash, attribute|
      hash[attribute] = filtered_attributes[attribute]
      hash
    end
  end

  if include_root_in_json?
    if json_api_format?
      { included_root_element => [filtered_attributes] }
    else
      { included_root_element => filtered_attributes }
    end
  else
    filtered_attributes
  end
end