Sha256: c31557f715a73d35e4bdd3822d29358341bc1f452ce2e7e1c47298ed06ec591d

Contents?: true

Size: 808 Bytes

Versions: 3

Compression:

Stored size: 808 Bytes

Contents

module ActiveData
  module Model
    module Parameterizable
      extend ActiveSupport::Concern

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_data-0.3.0 lib/active_data/model/parameterizable.rb
active_data-0.2.0 lib/active_data/model/parameterizable.rb
active_data-0.1.0 lib/active_data/model/parameterizable.rb