mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Rename attribute with :key (0.8.x compatibility)
This commit is contained in:
parent
ac37570bff
commit
08716d20c9
12
README.md
12
README.md
@ -69,6 +69,18 @@ ActiveModel::Serializer.config.adapter = :hal
|
|||||||
You won't need to implement an adapter unless you wish to use a new format or
|
You won't need to implement an adapter unless you wish to use a new format or
|
||||||
media type with AMS.
|
media type with AMS.
|
||||||
|
|
||||||
|
If you would like the key in the outputted JSON to be different from its name in ActiveRecord, you can use the :key option to customize it:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :id, :body
|
||||||
|
|
||||||
|
# look up :subject on the model, but use +title+ in the JSON
|
||||||
|
attribute :subject, :key => :title
|
||||||
|
has_many :comments
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
In your controllers, when you use `render :json`, Rails will now first search
|
In your controllers, when you use `render :json`, Rails will now first search
|
||||||
for a serializer for the object and use it if available.
|
for a serializer for the object and use it if available.
|
||||||
|
|
||||||
|
|||||||
@ -21,7 +21,6 @@ module ActiveModel
|
|||||||
def self.attributes(*attrs)
|
def self.attributes(*attrs)
|
||||||
@_attributes.concat attrs
|
@_attributes.concat attrs
|
||||||
|
|
||||||
|
|
||||||
attrs.each do |attr|
|
attrs.each do |attr|
|
||||||
define_method attr do
|
define_method attr do
|
||||||
object.read_attribute_for_serialization(attr)
|
object.read_attribute_for_serialization(attr)
|
||||||
@ -29,6 +28,14 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.attribute(attr, options = {})
|
||||||
|
key = options.fetch(:key, attr)
|
||||||
|
@_attributes.concat [key]
|
||||||
|
define_method key do
|
||||||
|
object.read_attribute_for_serialization(attr)
|
||||||
|
end unless method_defined?(key)
|
||||||
|
end
|
||||||
|
|
||||||
# Defines an association in the object should be rendered.
|
# Defines an association in the object should be rendered.
|
||||||
#
|
#
|
||||||
# The serializer object should implement the association name
|
# The serializer object should implement the association name
|
||||||
|
|||||||
5
test/fixtures/poro.rb
vendored
5
test/fixtures/poro.rb
vendored
@ -95,3 +95,8 @@ PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
|
|||||||
'paginated'
|
'paginated'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attribute :id
|
||||||
|
attribute :name, key: :title
|
||||||
|
end
|
||||||
|
|||||||
23
test/serializers/attribute_test.rb
Normal file
23
test/serializers/attribute_test.rb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class AttributeTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@blog = Blog.new({ id: 1, name: 'AMS Hints' })
|
||||||
|
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_attributes_definition
|
||||||
|
assert_equal([:id, :title],
|
||||||
|
@blog_serializer.class._attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_json_serializable_hash
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
||||||
|
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user