aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/serializer_test.rb
blob: 91ed6987a1fe4a386ef1dcc81b34b479f3468eac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require "cases/helper"

class SerializerTest < ActiveModel::TestCase
  class User
    attr_accessor :superuser

    def super_user?
      @superuser
    end

    def read_attribute_for_serialization(name)
      hash = { :first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password" }
      hash[name]
    end
  end

  class UserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name
  end

  class User2Serializer < ActiveModel::Serializer
    attributes :first_name, :last_name

    def serializable_hash(*)
      attributes.merge(:ok => true).merge(scope)
    end
  end

  class MyUserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name

    def serializable_hash(*)
      hash = attributes
      hash = hash.merge(:super_user => true) if my_user.super_user?
      hash
    end
  end

  def test_attributes
    user = User.new
    user_serializer = UserSerializer.new(user, nil)

    hash = user_serializer.as_json

    assert_equal({ :first_name => "Jose", :last_name => "Valim" }, hash)
  end

  def test_attributes_method
    user = User.new
    user_serializer = User2Serializer.new(user, {})

    hash = user_serializer.as_json

    assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true }, hash)
  end

  def test_serializer_receives_scope
    user = User.new
    user_serializer = User2Serializer.new(user, {:scope => true})

    hash = user_serializer.as_json

    assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true, :scope => true }, hash)
  end

  def test_pretty_accessors
    user = User.new
    user.superuser = true
    user_serializer = MyUserSerializer.new(user, nil)

    hash = user_serializer.as_json

    assert_equal({ :first_name => "Jose", :last_name => "Valim", :super_user => true }, hash)
  end
end