mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
commit
f4b17166eb
14
README.md
14
README.md
@ -36,8 +36,6 @@ class PostSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
|
||||
url :post
|
||||
end
|
||||
```
|
||||
|
||||
@ -48,8 +46,6 @@ class CommentSerializer < ActiveModel::Serializer
|
||||
attributes :name, :body
|
||||
|
||||
belongs_to :post
|
||||
|
||||
url [:post, :comment]
|
||||
end
|
||||
```
|
||||
|
||||
@ -239,8 +235,6 @@ class PostSerializer < ActiveModel::Serializer
|
||||
|
||||
has_many :comments
|
||||
has_one :author
|
||||
|
||||
url :post
|
||||
end
|
||||
```
|
||||
|
||||
@ -251,8 +245,6 @@ class CommentSerializer < ActiveModel::Serializer
|
||||
attributes :name, :body
|
||||
|
||||
belongs_to :post_id
|
||||
|
||||
url [:post, :comment]
|
||||
end
|
||||
```
|
||||
|
||||
@ -274,8 +266,6 @@ And you can change the JSON key that the serializer should use for a particular
|
||||
has_many :comments, key: :reviews
|
||||
```
|
||||
|
||||
The `url` declaration describes which named routes to use while generating URLs
|
||||
for your JSON. Not every adapter will require URLs.
|
||||
## Pagination
|
||||
|
||||
Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter.
|
||||
@ -307,8 +297,6 @@ class PostSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
|
||||
url :post
|
||||
end
|
||||
```
|
||||
|
||||
@ -332,8 +320,6 @@ class PostSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
|
||||
url :post
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
@ -31,7 +31,6 @@ module ActiveModel
|
||||
class << self
|
||||
attr_accessor :_attributes
|
||||
attr_accessor :_attributes_keys
|
||||
attr_accessor :_urls
|
||||
attr_accessor :_cache
|
||||
attr_accessor :_fragmented
|
||||
attr_accessor :_cache_key
|
||||
@ -44,7 +43,6 @@ module ActiveModel
|
||||
def self.inherited(base)
|
||||
base._attributes = self._attributes.try(:dup) || []
|
||||
base._attributes_keys = self._attributes_keys.try(:dup) || {}
|
||||
base._urls = []
|
||||
base._cache_digest = digest_caller_file(caller.first)
|
||||
super
|
||||
end
|
||||
@ -86,14 +84,6 @@ module ActiveModel
|
||||
@_cache_options = (options.empty?) ? nil : options
|
||||
end
|
||||
|
||||
def self.url(attr)
|
||||
@_urls.push attr
|
||||
end
|
||||
|
||||
def self.urls(*attrs)
|
||||
@_urls.concat attrs
|
||||
end
|
||||
|
||||
def self.serializer_for(resource, options = {})
|
||||
if resource.respond_to?(:serializer_class)
|
||||
resource.serializer_class
|
||||
|
||||
1
test/fixtures/active_record.rb
vendored
1
test/fixtures/active_record.rb
vendored
@ -40,7 +40,6 @@ module ARModels
|
||||
|
||||
has_many :comments
|
||||
belongs_to :author
|
||||
url :comments
|
||||
end
|
||||
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
|
||||
5
test/fixtures/poro.rb
vendored
5
test/fixtures/poro.rb
vendored
@ -59,8 +59,6 @@ end
|
||||
class ProfileSerializer < ActiveModel::Serializer
|
||||
attributes :name, :description
|
||||
|
||||
urls :posts, :comments
|
||||
|
||||
def arguments_passed_in?
|
||||
options[:my_options] == :accessible
|
||||
end
|
||||
@ -68,8 +66,6 @@ end
|
||||
|
||||
class ProfilePreviewSerializer < ActiveModel::Serializer
|
||||
attributes :name
|
||||
|
||||
urls :posts, :comments
|
||||
end
|
||||
|
||||
Post = Class.new(Model)
|
||||
@ -100,7 +96,6 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||
has_many :comments
|
||||
belongs_to :blog
|
||||
belongs_to :author
|
||||
url :comments
|
||||
|
||||
def blog
|
||||
Blog.new(id: 999, name: 'Custom blog')
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class UrlsTest < Minitest::Test
|
||||
def setup
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||
@post.comments = [@comment]
|
||||
|
||||
@profile_serializer = ProfileSerializer.new(@profile)
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
end
|
||||
|
||||
def test_urls_definition
|
||||
assert_equal([:posts, :comments], @profile_serializer.class._urls)
|
||||
end
|
||||
|
||||
def test_url_definition
|
||||
assert_equal([:comments], @post_serializer.class._urls)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user