Sha256: c31c7e7fb61afff4e233e74f8fc820edf710712a2d65b699372066854e0df1d9

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

class Model
  def initialize(hash={})
    @attributes = hash
  end

  def read_attribute_for_serialization(name)
    if name == :id || name == 'id'
      object_id
    else
      @attributes[name]
    end
  end
end


###
## Models
###
class User < Model
  def profile
    @profile ||= Profile.new(name: 'N1', description: 'D1')
  end
end

class Profile < Model
end

class Post < Model
  def comments
    @comments ||= [Comment.new(content: 'C1'),
                   Comment.new(content: 'C2')]
  end
end

class Comment < 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

class PostSerializer < ActiveModel::Serializer
  attributes :title, :body

  has_many :comments
end

class CommentSerializer < ActiveModel::Serializer
  attributes :content
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
active_model_serializers_rails_2.3-0.9.0.alpha1 test/fixtures/poro.rb
active_model_serializers-0.9.0.alpha1 test/fixtures/poro.rb
active_model_serializers_rails_2.3-0.9.0.pre2 test/fixtures/poro.rb
active_model_serializers_rails_2.3-0.9.0.pre1 test/fixtures/poro.rb