Module: ActiveData::Model::Parameterizable

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_data/model/parameterizable.rb

Instance Method Summary collapse

Instance Method Details

#to_params(options = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_data/model/parameterizable.rb', line 6

def to_params options = nil
  hash = serializable_hash options

  self.class.association_names.each do |association_name|
    if self.class.nested_attributes? association_name
      records = send(association_name)
      hash["#{association_name}_attributes"] = if records.is_a?(Enumerable)
        attributes = {}
        records.each_with_index do |a, i|
          key = a.has_attribute?(:id) && a.id? ? a.id : i
          attributes[key.to_s] = a.serializable_hash
        end
        attributes
      else
        records.serializable_hash
      end
    end
  end

  hash
end