Skip to content

Instantly share code, notes, and snippets.

@andreaseger
Last active April 5, 2016 17:56
Show Gist options
  • Save andreaseger/84edad45b690b142c401a170b742aafb to your computer and use it in GitHub Desktop.
Save andreaseger/84edad45b690b142c401a170b742aafb to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile true do
source 'https://rubygems.org'
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', ref: 'd30aa4c'
end
print "==================================================\n\n"
class Shoe
attr_accessor :id, :name, :vendor
def initialize(args)
args.each{ |k,v| send("#{k}=",v)}
end
alias read_attribute_for_serialization public_send
end
class ShoeSerializer < ActiveModel::Serializer
type "shoe"
attribute :id
attribute :name
attribute :vendor
end
shoe = Shoe.new(id: 3, name: "samba", vendor: "adidas")
require 'json'
print ActiveModelSerializers::SerializableResource.new(shoe,
adapter: :json_api,
).to_json
print "\n==================================================\n"
print ActiveModelSerializers::SerializableResource.new(shoe,
adapter: :json_api,
).as_json.to_json
# require 'active_support/json/encoding'
module ActiveModelSerializers
module Adapter
class Base
def to_json(options=nil)
as_json(options).to_json
end
end
end
end
print "\n==================================================\n"
print ActiveModelSerializers::SerializableResource.new(shoe,
adapter: :json_api,
).to_json
__END__
==================================================
[active_model_serializers] Rendered ShoeSerializer with ActiveModelSerializers::Adapter::JsonApi (0.06ms)
"#<ActiveModelSerializers::Adapter::JsonApi:0x0055ad9b412820>"
==================================================
[active_model_serializers] Rendered ShoeSerializer with ActiveModelSerializers::Adapter::JsonApi (3.09ms)
{"data":{"id":"3","type":"shoe","attributes":{"name":"samba","vendor":"adidas"}}}
==================================================
[active_model_serializers] Rendered ShoeSerializer with ActiveModelSerializers::Adapter::JsonApi (0.34ms)
{"data":{"id":"3","type":"shoe","attributes":{"name":"samba","vendor":"adidas"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment