jsonapi-resources 0.2.0 → 0.3.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/.travis.yml +5 -2
- data/Gemfile +3 -1
- data/README.md +52 -13
- data/jsonapi-resources.gemspec +1 -1
- data/lib/jsonapi-resources.rb +1 -0
- data/lib/jsonapi/association.rb +1 -9
- data/lib/jsonapi/error_codes.rb +1 -0
- data/lib/jsonapi/exceptions.rb +9 -5
- data/lib/jsonapi/formatter.rb +9 -18
- data/lib/jsonapi/paginator.rb +4 -15
- data/lib/jsonapi/request.rb +26 -42
- data/lib/jsonapi/resource.rb +35 -45
- data/lib/jsonapi/resource_controller.rb +6 -32
- data/lib/jsonapi/resource_serializer.rb +62 -33
- data/lib/jsonapi/resources/version.rb +1 -1
- data/lib/jsonapi/routing_ext.rb +4 -4
- data/test/config/database.yml +2 -1
- data/test/controllers/controller_test.rb +200 -160
- data/test/fixtures/active_record.rb +44 -201
- data/test/fixtures/book_comments.yml +11 -0
- data/test/fixtures/books.yml +6 -0
- data/test/fixtures/comments.yml +17 -0
- data/test/fixtures/comments_tags.yml +20 -0
- data/test/fixtures/expense_entries.yml +13 -0
- data/test/fixtures/facts.yml +11 -0
- data/test/fixtures/iso_currencies.yml +17 -0
- data/test/fixtures/people.yml +24 -0
- data/test/fixtures/posts.yml +96 -0
- data/test/fixtures/posts_tags.yml +59 -0
- data/test/fixtures/preferences.yml +18 -0
- data/test/fixtures/sections.yml +8 -0
- data/test/fixtures/tags.yml +39 -0
- data/test/helpers/hash_helpers.rb +0 -7
- data/test/integration/requests/request_test.rb +86 -28
- data/test/integration/routes/routes_test.rb +14 -25
- data/test/test_helper.rb +41 -17
- data/test/unit/jsonapi_request/jsonapi_request_test.rb +152 -0
- data/test/unit/operation/operations_processor_test.rb +13 -2
- data/test/unit/resource/resource_test.rb +68 -13
- data/test/unit/serializer/serializer_test.rb +328 -220
- metadata +33 -6
- data/lib/jsonapi/resource_for.rb +0 -29
@@ -1,9 +1,5 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
require 'jsonapi
|
3
|
-
require 'jsonapi/resource'
|
4
|
-
require 'jsonapi/exceptions'
|
5
|
-
require 'rails'
|
6
|
-
require 'rails/all'
|
2
|
+
require 'jsonapi-resources'
|
7
3
|
|
8
4
|
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
9
5
|
inflect.uncountable 'preferences'
|
@@ -16,7 +12,7 @@ ActiveRecord::Schema.define do
|
|
16
12
|
t.string :email
|
17
13
|
t.datetime :date_joined
|
18
14
|
t.belongs_to :preferences
|
19
|
-
t.timestamps
|
15
|
+
t.timestamps null: false
|
20
16
|
end
|
21
17
|
|
22
18
|
create_table :posts, force: true do |t|
|
@@ -24,14 +20,14 @@ ActiveRecord::Schema.define do
|
|
24
20
|
t.text :body
|
25
21
|
t.integer :author_id
|
26
22
|
t.belongs_to :section, index: true
|
27
|
-
t.timestamps
|
23
|
+
t.timestamps null: false
|
28
24
|
end
|
29
25
|
|
30
26
|
create_table :comments, force: true do |t|
|
31
27
|
t.text :body
|
32
28
|
t.belongs_to :post, index: true
|
33
29
|
t.integer :author_id
|
34
|
-
t.timestamps
|
30
|
+
t.timestamps null: false
|
35
31
|
end
|
36
32
|
|
37
33
|
create_table :tags, force: true do |t|
|
@@ -56,7 +52,7 @@ ActiveRecord::Schema.define do
|
|
56
52
|
t.string :name
|
57
53
|
t.string :country_name
|
58
54
|
t.string :minor_unit
|
59
|
-
t.timestamps
|
55
|
+
t.timestamps null: false
|
60
56
|
end
|
61
57
|
add_index :iso_currencies, :code, unique: true
|
62
58
|
|
@@ -115,7 +111,7 @@ ActiveRecord::Schema.define do
|
|
115
111
|
t.text :body
|
116
112
|
t.belongs_to :book, index: true
|
117
113
|
t.integer :author_id
|
118
|
-
t.timestamps
|
114
|
+
t.timestamps null: false
|
119
115
|
end
|
120
116
|
end
|
121
117
|
|
@@ -361,6 +357,9 @@ module Api
|
|
361
357
|
end
|
362
358
|
|
363
359
|
module V5
|
360
|
+
class AuthorsController < JSONAPI::ResourceController
|
361
|
+
end
|
362
|
+
|
364
363
|
class PostsController < JSONAPI::ResourceController
|
365
364
|
end
|
366
365
|
|
@@ -380,6 +379,8 @@ class PersonResource < JSONAPI::Resource
|
|
380
379
|
has_many :comments
|
381
380
|
has_many :posts
|
382
381
|
|
382
|
+
has_one :preferences
|
383
|
+
|
383
384
|
filter :name
|
384
385
|
|
385
386
|
def self.verify_custom_filter(filter, values, context)
|
@@ -395,29 +396,6 @@ class PersonResource < JSONAPI::Resource
|
|
395
396
|
end
|
396
397
|
end
|
397
398
|
|
398
|
-
class AuthorResource < JSONAPI::Resource
|
399
|
-
attributes :name, :email
|
400
|
-
model_name 'Person'
|
401
|
-
has_many :posts
|
402
|
-
|
403
|
-
filter :name
|
404
|
-
|
405
|
-
def self.find(filters, options = {})
|
406
|
-
resources = []
|
407
|
-
|
408
|
-
filters.each do |attr, filter|
|
409
|
-
_model_class.where("\"#{attr}\" LIKE \"%#{filter[0]}%\"").each do |model|
|
410
|
-
resources.push self.new(model, options[:context])
|
411
|
-
end
|
412
|
-
end
|
413
|
-
return resources
|
414
|
-
end
|
415
|
-
|
416
|
-
def fetchable_fields
|
417
|
-
super - [:email]
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
399
|
class CommentResource < JSONAPI::Resource
|
422
400
|
attributes :body
|
423
401
|
has_one :post
|
@@ -529,6 +507,10 @@ class IsoCurrencyResource < JSONAPI::Resource
|
|
529
507
|
attributes :name, :country_name, :minor_unit
|
530
508
|
|
531
509
|
filter :country_name
|
510
|
+
|
511
|
+
def self.verify_key(key, context = nil)
|
512
|
+
key && String(key)
|
513
|
+
end
|
532
514
|
end
|
533
515
|
|
534
516
|
class ExpenseEntryResource < JSONAPI::Resource
|
@@ -565,10 +547,6 @@ class BreedResource < JSONAPI::Resource
|
|
565
547
|
def self.find_by_key(id, options = {})
|
566
548
|
BreedResource.new($breed_data.breeds[id.to_i], options[:context])
|
567
549
|
end
|
568
|
-
|
569
|
-
def self.find_by_keys(keys, options = {})
|
570
|
-
keys.map { |key| self.find_by_key(key, options) }
|
571
|
-
end
|
572
550
|
end
|
573
551
|
|
574
552
|
class PlanetResource < JSONAPI::Resource
|
@@ -602,7 +580,7 @@ end
|
|
602
580
|
class PreferencesResource < JSONAPI::Resource
|
603
581
|
attribute :advanced_mode
|
604
582
|
|
605
|
-
has_one :author, foreign_key: :person_id
|
583
|
+
has_one :author, foreign_key: :person_id, class_name: 'Person'
|
606
584
|
has_many :friends, class_name: 'Person'
|
607
585
|
|
608
586
|
def self.find_by_key(key, options = {})
|
@@ -652,7 +630,7 @@ module Api
|
|
652
630
|
filters :writer
|
653
631
|
end
|
654
632
|
|
655
|
-
AuthorResource = AuthorResource.dup
|
633
|
+
# AuthorResource = AuthorResource.dup
|
656
634
|
PersonResource = PersonResource.dup
|
657
635
|
CommentResource = CommentResource.dup
|
658
636
|
TagResource = TagResource.dup
|
@@ -672,7 +650,7 @@ end
|
|
672
650
|
module Api
|
673
651
|
module V2
|
674
652
|
PreferencesResource = PreferencesResource.dup
|
675
|
-
AuthorResource = AuthorResource.dup
|
653
|
+
# AuthorResource = AuthorResource.dup
|
676
654
|
PersonResource = PersonResource.dup
|
677
655
|
PostResource = PostResource.dup
|
678
656
|
|
@@ -712,7 +690,30 @@ end
|
|
712
690
|
|
713
691
|
module Api
|
714
692
|
module V5
|
715
|
-
AuthorResource
|
693
|
+
class AuthorResource < JSONAPI::Resource
|
694
|
+
attributes :name, :email
|
695
|
+
model_name 'Person'
|
696
|
+
has_many :posts
|
697
|
+
|
698
|
+
filter :name
|
699
|
+
|
700
|
+
def self.find(filters, options = {})
|
701
|
+
resources = []
|
702
|
+
|
703
|
+
filters.each do |attr, filter|
|
704
|
+
_model_class.where("\"#{attr}\" LIKE \"%#{filter[0]}%\"").each do |model|
|
705
|
+
resources.push self.new(model, options[:context])
|
706
|
+
end
|
707
|
+
end
|
708
|
+
return resources
|
709
|
+
end
|
710
|
+
|
711
|
+
def fetchable_fields
|
712
|
+
super - [:email]
|
713
|
+
end
|
714
|
+
end
|
715
|
+
|
716
|
+
PersonResource = PersonResource.dup
|
716
717
|
PostResource = PostResource.dup
|
717
718
|
ExpenseEntryResource = ExpenseEntryResource.dup
|
718
719
|
IsoCurrencyResource = IsoCurrencyResource.dup
|
@@ -737,141 +738,7 @@ class BadlyNamedAttributesResource < JSONAPI::Resource
|
|
737
738
|
end
|
738
739
|
warn 'end testing Name Collisions'
|
739
740
|
|
740
|
-
### DATA
|
741
|
-
javascript = Section.create(name: 'javascript')
|
742
|
-
ruby = Section.create(name: 'ruby')
|
743
|
-
|
744
|
-
a = Person.create(name: 'Joe Author',
|
745
|
-
email: 'joe@xyz.fake',
|
746
|
-
date_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'))
|
747
|
-
|
748
|
-
b = Person.create(name: 'Fred Reader',
|
749
|
-
email: 'fred@xyz.fake',
|
750
|
-
date_joined: DateTime.parse('2013-10-31 20:25:00 UTC +00:00'))
|
751
|
-
|
752
|
-
c = Person.create(name: 'Lazy Author',
|
753
|
-
email: 'lazy@xyz.fake',
|
754
|
-
date_joined: DateTime.parse('2013-10-31 21:25:00 UTC +00:00'))
|
755
|
-
|
756
|
-
d = Person.create(name: 'Tag Crazy Author',
|
757
|
-
email: 'taggy@xyz.fake',
|
758
|
-
date_joined: DateTime.parse('2013-11-30 4:20:00 UTC +00:00'))
|
759
|
-
|
760
|
-
short_tag = Tag.create(name: 'short')
|
761
|
-
whiny_tag = Tag.create(name: 'whiny')
|
762
|
-
grumpy_tag = Tag.create(name: 'grumpy')
|
763
|
-
happy_tag = Tag.create(name: 'happy')
|
764
|
-
jr_tag = Tag.create(name: 'JR')
|
765
|
-
|
766
|
-
silly_tag = Tag.create(name: 'silly')
|
767
|
-
sleepy_tag = Tag.create(name: 'sleepy')
|
768
|
-
goofy_tag = Tag.create(name: 'goofy')
|
769
|
-
wacky_tag = Tag.create(name: 'wacky')
|
770
|
-
|
771
|
-
# id:1
|
772
|
-
Post.create(title: 'New post',
|
773
|
-
body: 'A body!!!',
|
774
|
-
author_id: a.id).tap do |post|
|
775
|
-
|
776
|
-
post.tags.concat short_tag, whiny_tag, grumpy_tag
|
777
|
-
|
778
|
-
post.comments.create(body: 'what a dumb post', author_id: a.id, post_id: post.id).tap do |comment|
|
779
|
-
comment.tags.concat whiny_tag, short_tag
|
780
|
-
end
|
781
|
-
|
782
|
-
post.comments.create(body: 'i liked it', author_id: b.id, post_id: post.id).tap do |comment|
|
783
|
-
comment.tags.concat happy_tag, short_tag
|
784
|
-
end
|
785
|
-
end
|
786
|
-
|
787
|
-
# id:2
|
788
|
-
Post.create(title: 'JR Solves your serialization woes!',
|
789
|
-
body: 'Use JR',
|
790
|
-
author_id: a.id,
|
791
|
-
section: Section.create(name: 'ruby')).tap do |post|
|
792
|
-
|
793
|
-
post.tags.concat jr_tag
|
794
|
-
|
795
|
-
post.comments.create(body: 'Thanks man. Great post. But what is JR?', author_id: b.id, post_id: post.id).tap do |comment|
|
796
|
-
comment.tags.concat jr_tag
|
797
|
-
end
|
798
|
-
end
|
799
|
-
|
800
|
-
# id:3
|
801
|
-
Post.create(title: 'Update This Later',
|
802
|
-
body: 'AAAA',
|
803
|
-
author_id: c.id)
|
804
|
-
|
805
|
-
# id:4
|
806
|
-
Post.create(title: 'Delete This Later - Single',
|
807
|
-
body: 'AAAA',
|
808
|
-
author_id: c.id)
|
809
|
-
|
810
|
-
# id:5
|
811
|
-
Post.create(title: 'Delete This Later - Multiple1',
|
812
|
-
body: 'AAAA',
|
813
|
-
author_id: c.id)
|
814
|
-
|
815
|
-
# id:6
|
816
|
-
Post.create(title: 'Delete This Later - Multiple2',
|
817
|
-
body: 'AAAA',
|
818
|
-
author_id: c.id)
|
819
|
-
|
820
|
-
# id:7
|
821
|
-
Post.create(title: 'Delete This Later - Single2',
|
822
|
-
body: 'AAAA',
|
823
|
-
author_id: c.id)
|
824
|
-
|
825
|
-
# id:8
|
826
|
-
Post.create(title: 'Delete This Later - Multiple2-1',
|
827
|
-
body: 'AAAA',
|
828
|
-
author_id: c.id)
|
829
|
-
|
830
|
-
# id:9
|
831
|
-
Post.create(title: 'Delete This Later - Multiple2-2',
|
832
|
-
body: 'AAAA',
|
833
|
-
author_id: c.id)
|
834
|
-
|
835
|
-
# id:9
|
836
|
-
Post.create(title: 'Update This Later - Multiple',
|
837
|
-
body: 'AAAA',
|
838
|
-
author_id: c.id)
|
839
|
-
|
840
|
-
# id:10
|
841
|
-
Post.create(title: 'JR How To',
|
842
|
-
body: 'Use JR to write API apps',
|
843
|
-
author_id: a.id).tap do |post|
|
844
|
-
post.tags.concat jr_tag
|
845
|
-
end
|
846
|
-
|
847
|
-
IsoCurrency.create(code: 'USD', name: 'United States Dollar', country_name: 'United States', minor_unit: 'cent')
|
848
|
-
IsoCurrency.create(code: 'EUR', name: 'Euro Member Countries', country_name: 'Euro Member Countries', minor_unit: 'cent')
|
849
|
-
IsoCurrency.create(code: 'CAD', name: 'Canadian dollar', country_name: 'Canada', minor_unit: 'cent')
|
850
|
-
|
851
|
-
ExpenseEntry.create(currency_code: 'USD',
|
852
|
-
employee_id: c.id,
|
853
|
-
cost: '12.05',
|
854
|
-
transaction_date: Date.parse('2014-04-15'))
|
855
|
-
|
856
|
-
ExpenseEntry.create(currency_code: 'USD',
|
857
|
-
employee_id: c.id,
|
858
|
-
cost: '12.06',
|
859
|
-
transaction_date: Date.parse('2014-04-15'))
|
860
|
-
|
861
|
-
# id:11
|
862
|
-
Post.create(title: 'Tagged up post 1',
|
863
|
-
body: 'AAAA',
|
864
|
-
author_id: d.id,
|
865
|
-
tag_ids: [6,7,8,9]
|
866
|
-
)
|
867
|
-
|
868
|
-
# id:12
|
869
|
-
Post.create(title: 'Tagged up post 2',
|
870
|
-
body: 'BBBB',
|
871
|
-
author_id: d.id,
|
872
|
-
tag_ids: [6,7,8,9]
|
873
|
-
)
|
874
|
-
|
741
|
+
### PORO DATA
|
875
742
|
gas_giant = PlanetType.create(name: 'Gas Giant')
|
876
743
|
planetoid = PlanetType.create(name: 'Planetoid')
|
877
744
|
terrestrial = PlanetType.create(name: 'Terrestrial')
|
@@ -888,28 +755,4 @@ jupiter = Planet.create(name: 'Jupiter', description: 'A gas giant.', planet_typ
|
|
888
755
|
betax = Planet.create(name: 'Beta X', description: 'Newly discovered Planet X', planet_type_id: unknown.id)
|
889
756
|
betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y', planet_type_id: unknown.id)
|
890
757
|
betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id)
|
891
|
-
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
|
892
|
-
|
893
|
-
preference = Preferences.create
|
894
|
-
|
895
|
-
fact = Fact.create(spouse_name: 'Jane Author',
|
896
|
-
bio: 'First man to run across Antartica.',
|
897
|
-
quality_rating: 23.89/45.6,
|
898
|
-
salary: BigDecimal('47000.56'),
|
899
|
-
date_time_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'),
|
900
|
-
birthday: Date.parse('1965-06-30'),
|
901
|
-
bedtime: Time.parse('2000-01-01 20:00:00 UTC +00:00'),
|
902
|
-
photo: "abc",
|
903
|
-
cool: false
|
904
|
-
)
|
905
|
-
|
906
|
-
for book_num in 0..999
|
907
|
-
Book.create(title: "Book #{book_num}", isbn: "12345-#{book_num}-67890") do |book|
|
908
|
-
book.save
|
909
|
-
if book_num < 5
|
910
|
-
for comment_num in 0..50
|
911
|
-
book.book_comments.create(body: "This is comment #{comment_num} on book #{book_num}.", author_id: a.id, book_id: book.id)
|
912
|
-
end
|
913
|
-
end
|
914
|
-
end
|
915
|
-
end
|
758
|
+
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<% comment_id = 0 %>
|
2
|
+
<% for book_num in 0..4 %>
|
3
|
+
<% for comment_num in 0..50 %>
|
4
|
+
book_<%= book_num %>_comment_<%= comment_num %>:
|
5
|
+
id: <%= comment_id %>
|
6
|
+
body: This is comment <%= comment_num %> on book <%= book_num %>.
|
7
|
+
author_id: 1
|
8
|
+
book_id: <%= book_num %>
|
9
|
+
<% comment_id = comment_id + 1 %>
|
10
|
+
<% end %>
|
11
|
+
<% end %>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
post_1_dumb_post:
|
2
|
+
id: 1
|
3
|
+
post_id: 1
|
4
|
+
body: what a dumb post
|
5
|
+
author_id: 1
|
6
|
+
|
7
|
+
post_1_i_liked_it:
|
8
|
+
id: 2
|
9
|
+
post_id: 1
|
10
|
+
body: i liked it
|
11
|
+
author_id: 2
|
12
|
+
|
13
|
+
post_2_thanks_man:
|
14
|
+
id: 3
|
15
|
+
post_id: 2
|
16
|
+
body: Thanks man. Great post. But what is JR?
|
17
|
+
author_id: 2
|
@@ -0,0 +1,20 @@
|
|
1
|
+
post_1_dumb_post_whiny:
|
2
|
+
comment_id: 1
|
3
|
+
tag_id: 2
|
4
|
+
|
5
|
+
post_1_dumb_post_short:
|
6
|
+
comment_id: 1
|
7
|
+
tag_id: 1
|
8
|
+
|
9
|
+
post_1_i_liked_it_happy:
|
10
|
+
comment_id: 2
|
11
|
+
tag_id: 4
|
12
|
+
|
13
|
+
post_1_i_liked_it_short:
|
14
|
+
comment_id: 2
|
15
|
+
tag_id: 1
|
16
|
+
|
17
|
+
post_2_thanks_man_jr:
|
18
|
+
comment_id: 3
|
19
|
+
tag_id: 5
|
20
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
fact_1:
|
2
|
+
id: 1
|
3
|
+
spouse_name: Jane Author
|
4
|
+
bio: First man to run across Antartica.
|
5
|
+
quality_rating: <%= 23.89/45.6 %>
|
6
|
+
salary: 47000.56
|
7
|
+
date_time_joined: 2013-08-07 20:25:00 UTC +00:00
|
8
|
+
birthday: 1965-06-30
|
9
|
+
bedtime: 2000-01-01 20:00:00 UTC +00:00
|
10
|
+
photo: abc
|
11
|
+
cool: false
|
@@ -0,0 +1,17 @@
|
|
1
|
+
usd:
|
2
|
+
code: USD
|
3
|
+
name: United States Dollar
|
4
|
+
country_name: United States
|
5
|
+
minor_unit: cent
|
6
|
+
|
7
|
+
eur:
|
8
|
+
code: EUR
|
9
|
+
name: Euro Member Countries
|
10
|
+
country_name: Euro Member Countries
|
11
|
+
minor_unit: cent
|
12
|
+
|
13
|
+
cad:
|
14
|
+
code: CAD
|
15
|
+
name: Canadian dollar
|
16
|
+
country_name: Canada
|
17
|
+
minor_unit: cent
|