rails/rails — pull request #6597 by frodsan (2012-06-01 22:57:18+00:00)
Title: change AMS::JSON.include_root_in_json default value to false

* Update `include_root_in_json` default value to false for default value
to false for `ActiveModel::Serializers::JSON`.
* Remove unnecessary change to include_root_in_json option in
wrap_parameters template.
* Update `as_json` documentation.
* Fix JSONSerialization tests.

It's confusing that AM serializers behave differently from AR,
even when AR objects include AM serializers module.

class User < ActiveRecord::Base; end

class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON

attr_accessor :name, :age

def attributes
instance_values
end
end

user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included

person.as_json
=> {"person"=>{"name"=>"Francesco", "age"=>22}}
# root is included

# different default values for include_root_in_json
ActiveRecord::Base.include_root_in_json
=> false

Person.include_root_in_json
=> true

Change the default value of AM serializers to false, update
the misleading documentation and remove unnecessary change
to false of include_root_in_json option with AR objects.

Result:

class User < ActiveRecord::Base; end

class Person
include ActiveModel::Model
include ActiveModel::AttributeMethods
include ActiveModel::Serializers::JSON

attr_accessor :name, :age

def attributes
instance_values
end
end

user.as_json
=> {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
# root is not included

person.as_json
=> {"name"=>"Francesco", "age"=>22}
# root is not included

# same behaviour, more consistent
ActiveRecord::Base.include_root_in_json
=> false

Person.include_root_in_json
=> true

Fixes #6578.