1- require 'jsonapi/renderer '
1+ require 'spec_helper '
22
3- class Model
4- def initialize ( params )
5- params . each { |k , v | instance_variable_set ( "@#{ k } " , v ) }
6- end
7- end
8-
9- class User < Model
3+ class UserResource
104 attr_accessor :id , :name , :address , :posts
11- end
125
13- class Post < Model
14- attr_accessor :id , :title , :date , :author
15- end
16-
17- class UserResource
18- def initialize ( user )
19- @user = user
6+ def initialize ( id , name , address , posts )
7+ @id = id
8+ @name = name
9+ @address = address
10+ @posts = posts
2011 end
2112
2213 def jsonapi_type
2314 'users'
2415 end
2516
2617 def jsonapi_id
27- @user . id . to_s
18+ @id . to_s
2819 end
2920
3021 def jsonapi_related ( included )
3122 if included . include? ( :posts )
32- { posts : @user . posts . map { |p | PostResource . new ( p ) } }
23+ { posts : @posts . map { |p | p } }
3324 else
3425 { }
3526 end
@@ -40,15 +31,15 @@ def as_jsonapi(options = {})
4031 included = options [ :include ] || [ ]
4132
4233 hash = { id : jsonapi_id , type : jsonapi_type }
43- hash [ :attributes ] = { name : @user . name , address : @user . address }
34+ hash [ :attributes ] = { name : @name , address : @address }
4435 . select { |k , _ | fields . include? ( k ) }
4536 if fields . include? ( :posts )
4637 hash [ :relationships ] = { posts : { } }
4738 hash [ :relationships ] [ :posts ] = {
4839 links : {
49- self : "http://api.example.com/users/#{ @user . id } /relationships/posts" ,
40+ self : "http://api.example.com/users/#{ @id } /relationships/posts" ,
5041 related : {
51- href : "http://api.example.com/users/#{ @user . id } /posts" ,
42+ href : "http://api.example.com/users/#{ @id } /posts" ,
5243 meta : {
5344 do_not_use : true
5445 }
@@ -59,14 +50,14 @@ def as_jsonapi(options = {})
5950 }
6051 }
6152 if included . include? ( :posts )
62- hash [ :relationships ] [ :posts ] [ :data ] = @user . posts . map do |p |
53+ hash [ :relationships ] [ :posts ] [ :data ] = @posts . map do |p |
6354 { type : 'posts' , id : p . id . to_s }
6455 end
6556 end
6657 end
6758
6859 hash [ :links ] = {
69- self : "http://api.example.com/users/#{ @user . id } "
60+ self : "http://api.example.com/users/#{ @id } "
7061 }
7162 hash [ :meta ] = { user_meta : 'is_meta' }
7263
@@ -75,46 +66,51 @@ def as_jsonapi(options = {})
7566end
7667
7768class PostResource
78- def initialize ( post )
79- @post = post
69+ attr_accessor :id , :title , :date , :author
70+
71+ def initialize ( id , title , date , author )
72+ @id = id
73+ @title = title
74+ @date = date
75+ @author = author
8076 end
8177
8278 def jsonapi_type
8379 'posts'
8480 end
8581
8682 def jsonapi_id
87- @post . id . to_s
83+ @id . to_s
8884 end
8985
9086 def jsonapi_related ( included )
91- included . include? ( :author ) ? { author : UserResource . new ( @post . author ) } : { }
87+ included . include? ( :author ) ? { author : @ author } : { }
9288 end
9389
9490 def as_jsonapi ( options = { } )
9591 fields = options [ :fields ] || [ :title , :date , :author ]
9692 included = options [ :include ] || [ ]
9793 hash = { id : jsonapi_id , type : jsonapi_type }
9894
99- hash [ :attributes ] = { title : @post . title , date : @post . date }
95+ hash [ :attributes ] = { title : @title , date : @date }
10096 . select { |k , _ | fields . include? ( k ) }
10197 if fields . include? ( :author )
10298 hash [ :relationships ] = { author : { } }
10399 hash [ :relationships ] [ :author ] = {
104100 links : {
105- self : "http://api.example.com/posts/#{ @post . id } /relationships/author" ,
106- related : "http://api.example.com/posts/#{ @post . id } /author"
101+ self : "http://api.example.com/posts/#{ @id } /relationships/author" ,
102+ related : "http://api.example.com/posts/#{ @id } /author"
107103 } ,
108104 meta : {
109105 author_active : true
110106 }
111107 }
112108 if included . include? ( :author )
113109 hash [ :relationships ] [ :author ] [ :data ] =
114- if @post . author . nil?
110+ if @author . nil?
115111 nil
116112 else
117- { type : 'users' , id : @post . author . id . to_s }
113+ { type : 'users' , id : @author . id . to_s }
118114 end
119115 end
120116 end
@@ -126,15 +122,15 @@ def as_jsonapi(options = {})
126122describe JSONAPI , '#render' do
127123 before ( :all ) do
128124 @users = [
129- User . new ( id : 1 , name : 'User 1' , address : '123 Example st.' , posts : [ ] ) ,
130- User . new ( id : 2 , name : 'User 2' , address : '234 Example st.' , posts : [ ] ) ,
131- User . new ( id : 3 , name : 'User 3' , address : '345 Example st.' , posts : [ ] ) ,
132- User . new ( id : 4 , name : 'User 4' , address : '456 Example st.' , posts : [ ] )
125+ UserResource . new ( 1 , 'User 1' , '123 Example st.' , [ ] ) ,
126+ UserResource . new ( 2 , 'User 2' , '234 Example st.' , [ ] ) ,
127+ UserResource . new ( 3 , 'User 3' , '345 Example st.' , [ ] ) ,
128+ UserResource . new ( 4 , 'User 4' , '456 Example st.' , [ ] )
133129 ]
134130 @posts = [
135- Post . new ( id : 1 , title : 'Post 1' , date : 'yesterday' , author : @users [ 1 ] ) ,
136- Post . new ( id : 2 , title : 'Post 2' , date : 'today' , author : @users [ 0 ] ) ,
137- Post . new ( id : 3 , title : 'Post 3' , date : 'tomorrow' , author : @users [ 1 ] )
131+ PostResource . new ( 1 , 'Post 1' , 'yesterday' , @users [ 1 ] ) ,
132+ PostResource . new ( 2 , 'Post 2' , 'today' , @users [ 0 ] ) ,
133+ PostResource . new ( 3 , 'Post 3' , 'tomorrow' , @users [ 1 ] )
138134 ]
139135 @users [ 0 ] . posts = [ @posts [ 1 ] ]
140136 @users [ 1 ] . posts = [ @posts [ 0 ] , @posts [ 2 ] ]
@@ -159,7 +155,7 @@ def as_jsonapi(options = {})
159155 end
160156
161157 it 'renders a single resource' do
162- actual = JSONAPI . render ( data : UserResource . new ( @users [ 0 ] ) )
158+ actual = JSONAPI . render ( data : @users [ 0 ] )
163159 expected = {
164160 data : {
165161 type : 'users' ,
@@ -197,8 +193,8 @@ def as_jsonapi(options = {})
197193 end
198194
199195 it 'renders a collection of resources' do
200- actual = JSONAPI . render ( data : [ UserResource . new ( @users [ 0 ] ) ,
201- UserResource . new ( @users [ 1 ] ) ] )
196+ actual = JSONAPI . render ( data : [ @users [ 0 ] ,
197+ @users [ 1 ] ] )
202198 expected = {
203199 data : [
204200 {
@@ -268,7 +264,7 @@ def as_jsonapi(options = {})
268264 end
269265
270266 it 'renders included relationships' do
271- actual = JSONAPI . render ( data : UserResource . new ( @users [ 0 ] ) ,
267+ actual = JSONAPI . render ( data : @users [ 0 ] ,
272268 include : 'posts' )
273269 expected = {
274270 data : {
@@ -329,7 +325,7 @@ def as_jsonapi(options = {})
329325 end
330326
331327 it 'filters out fields' do
332- actual = JSONAPI . render ( data : UserResource . new ( @users [ 0 ] ) ,
328+ actual = JSONAPI . render ( data : @users [ 0 ] ,
333329 fields : { users : [ :name ] } )
334330 expected = {
335331 data : {
0 commit comments