Sha256: fb950c73d754e22348668ce8388df7ba28c294fbfbeed962d732a34961d1f619

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'active_support/inflector'
require 'ivy/serializers/formats/json'

module Ivy
  module Serializers
    module Formats
      class ActiveModelSerializers < JSON
        def belongs_to(name, resource, options={})
          if options[:polymorphic]
            if resource
              @hash_gen.store_object(name) { polymorphic_resource(resource) }
            else
              @hash_gen.store(name, nil)
            end
          else
            id(:"#{name}_id", resource)
          end
        end

        def has_many(name, resources, options={})
          if options[:polymorphic]
            @hash_gen.store_array(name) { polymorphic_resources(resources) }
          else
            ids(:"#{singularize(name)}_ids", resources)
          end
        end

        def primary_resource(primary_resource_name, primary_resource)
          super(singularize(primary_resource_name).to_sym, primary_resource)
        end

        def primary_resources(primary_resources_name, primary_resources)
          super(primary_resources_name, primary_resources)
        end

        private

        def polymorphic_resource(resource)
          id(:id, resource)
          type(:type, resource)
        end

        def polymorphic_resources(resources)
          resources.each { |resource| @hash_gen.push_object { polymorphic_resource(resource) } }
        end

        def singularize(name)
          ActiveSupport::Inflector.singularize(name.to_s)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ivy-serializers-0.2.0 lib/ivy/serializers/formats/active_model_serializers.rb