Method: OldApiResource::Base#serializable_hash

Defined in:
lib/old_api_resource/base.rb

#serializable_hash(options = {}) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/old_api_resource/base.rb', line 461

def serializable_hash(options = {})
  options[:include_associations] = options[:include_associations] ? options[:include_associations].symbolize_array : []
  options[:include_extras] = options[:include_extras] ? options[:include_extras].symbolize_array : []
  options[:except] ||= []
  ret = self.attributes.inject({}) do |accum, (key,val)|
    # If this is an association and it's in include_associations then include it
    if self.association?(key) && options[:include_associations].include?(key.to_sym)
      accum.merge(key => val.serializable_hash({:include_id => true}))
    elsif options[:include_extras].include?(key.to_sym)
      accum.merge(key => val)
    elsif options[:except].include?(key.to_sym)
      accum
    else
      self.association?(key) || !self.attribute?(key) || self.protected_attribute?(key) ? accum : accum.merge(key => val)
    end
  end
  # include id - this is for nested updates
  ret[:id] = self.id if options[:include_id] && !self.id.nil?
  ret
end