Namespace
Methods
S
T
Constants
FORMATS = [ :xml, :json ]
 
Instance Public methods
setup()
# File activerecord/test/cases/serialization_test.rb, line 8
def setup
  @contact_attributes = {
    :name           => 'aaron stack',
    :age            => 25,
    :avatar         => 'binarydata',
    :created_at     => Time.utc(2006, 8, 1),
    :awesome        => false,
    :preferences    => { :gem => '<strong>ruby</strong>' },
    :alternative_id => nil
  }
end
test_except_include()
# File activemodel/test/cases/serialization_test.rb, line 136
def test_except_include
  expected = {"name"=>"David", "email"=>"david@example.com",
                              :friends => [{"name" => 'Joe', "email" => 'joe@example.com'},
                                           {"name" => "Sue", "email" => 'sue@example.com'}]}
  assert_equal expected , @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}})
end
test_include_option_with_empty_association()
# File activemodel/test/cases/serialization_test.rb, line 102
def test_include_option_with_empty_association
  @user.friends = []
  expected =  {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]}
  assert_equal expected , @user.serializable_hash(:include => :friends)
end
test_include_option_with_plural_association()
# File activemodel/test/cases/serialization_test.rb, line 95
def test_include_option_with_plural_association
  expected =  {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
               :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
                          {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
  assert_equal expected , @user.serializable_hash(:include => :friends)
end
test_include_option_with_singular_association()
# File activemodel/test/cases/serialization_test.rb, line 89
def test_include_option_with_singular_association
  expected =  {"name"=>"David", "gender"=>"male", "email"=>"david@example.com",
               :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}}
  assert_equal expected , @user.serializable_hash(:include => :address)
end
test_include_with_options()
# File activemodel/test/cases/serialization_test.rb, line 116
def test_include_with_options
  expected =  {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
               :address=>{"street"=>"123 Lane"}}
  assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}})
end
test_method_serializable_hash_should_work()
# File activemodel/test/cases/serialization_test.rb, line 45
def test_method_serializable_hash_should_work
  expected =  {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
  assert_equal expected , @user.serializable_hash
end
test_method_serializable_hash_should_work_with_except_and_methods()
# File activemodel/test/cases/serialization_test.rb, line 70
def test_method_serializable_hash_should_work_with_except_and_methods
  expected =  {"gender"=>"male", :foo=>"i_am_foo"}
  assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo])
end
test_method_serializable_hash_should_work_with_except_option()
# File activemodel/test/cases/serialization_test.rb, line 55
def test_method_serializable_hash_should_work_with_except_option
  expected =  {"gender"=>"male", "email"=>"david@example.com"}
  assert_equal expected , @user.serializable_hash(:except => [:name])
end
test_method_serializable_hash_should_work_with_methods_option()
# File activemodel/test/cases/serialization_test.rb, line 60
def test_method_serializable_hash_should_work_with_methods_option
  expected =  {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"}
  assert_equal expected , @user.serializable_hash(:methods => [:foo])
end
test_method_serializable_hash_should_work_with_only_and_methods()
# File activemodel/test/cases/serialization_test.rb, line 65
def test_method_serializable_hash_should_work_with_only_and_methods
  expected =  {:foo=>"i_am_foo"}
  assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo])
end
test_method_serializable_hash_should_work_with_only_option()
# File activemodel/test/cases/serialization_test.rb, line 50
def test_method_serializable_hash_should_work_with_only_option
  expected =  {"name"=>"David"}
  assert_equal expected , @user.serializable_hash(:only => [:name])
end
test_multiple_includes()
# File activemodel/test/cases/serialization_test.rb, line 108
def test_multiple_includes
  expected =  {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
               :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111},
               :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
                          {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
  assert_equal expected , @user.serializable_hash(:include => [:address, :friends])
end
test_multiple_includes_with_options()
# File activemodel/test/cases/serialization_test.rb, line 143
def test_multiple_includes_with_options
  expected =  {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
               :address=>{"street"=>"123 Lane"},
               :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
                          {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
  assert_equal expected , @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends])
end
test_nested_include()
# File activemodel/test/cases/serialization_test.rb, line 122
def test_nested_include
  @user.friends.first.friends = [@user]
  expected =  {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
               :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male',
                           :friends => [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]},
                          {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', :friends => []}]}
  assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}})
end
test_only_include()
# File activemodel/test/cases/serialization_test.rb, line 131
def test_only_include
  expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]}
  assert_equal expected , @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}})
end
test_serialize_should_allow_attribute_except_filtering()
# File activerecord/test/cases/serialization_test.rb, line 44
def test_serialize_should_allow_attribute_except_filtering
  for format in FORMATS
    @serialized = Contact.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ])
    contact = Contact.new.send("from_#{format}", @serialized)
    assert_nil contact.name, "For #{format}"
    assert_nil contact.age, "For #{format}"
    assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
  end
end
test_serialize_should_allow_attribute_only_filtering()
# File activerecord/test/cases/serialization_test.rb, line 35
def test_serialize_should_allow_attribute_only_filtering
  for format in FORMATS
    @serialized = Contact.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ])
    contact = Contact.new.send("from_#{format}", @serialized)
    assert_equal @contact_attributes[:name], contact.name, "For #{format}"
    assert_nil contact.avatar, "For #{format}"
  end
end
test_serialize_should_be_reversible()
# File activerecord/test/cases/serialization_test.rb, line 26
def test_serialize_should_be_reversible
  for format in FORMATS
    @serialized = Contact.new.send("to_#{format}")
    contact = Contact.new.send("from_#{format}", @serialized)

    assert_equal @contact_attributes.keys.collect(&:to_s).sort, contact.attributes.keys.collect(&:to_s).sort, "For #{format}"
  end
end
test_serialized_init_with()
# File activerecord/test/cases/serialization_test.rb, line 20
def test_serialized_init_with
  topic = Topic.allocate
  topic.init_with('attributes' => { 'content' => '--- foo' })
  assert_equal 'foo', topic.content
end
test_should_not_call_methods_that_dont_respond()
# File activemodel/test/cases/serialization_test.rb, line 75
def test_should_not_call_methods_that_dont_respond
  expected =  {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
  assert_equal expected , @user.serializable_hash(:methods => [:bar])
end
test_should_use_read_attribute_for_serialization()
# File activemodel/test/cases/serialization_test.rb, line 80
def test_should_use_read_attribute_for_serialization
  def @user.read_attribute_for_serialization(n)
    "Jon"
  end

  expected = { "name" => "Jon" }
  assert_equal expected, @user.serializable_hash(:only => :name)
end