active_model_serializers/test/fixtures/poro.rb
2013-10-18 17:49:18 -02:00

46 lines
687 B
Ruby

class Model
def initialize(hash={})
@attributes = hash
end
def read_attribute_for_serialization(name)
@attributes[name]
end
def id
object_id
end
end
###
## Models
###
class User < Model
def profile
@profile ||= Profile.new(name: 'N1', description: 'D1')
end
end
class Profile < Model
end
###
## Serializers
###
class UserSerializer < ActiveModel::Serializer
attributes :name, :email
has_one :profile
end
class ProfileSerializer < ActiveModel::Serializer
def description
description = object.read_attribute_for_serialization(:description)
scope ? "#{description} - #{scope}" : description
end
attributes :name, :description
end