restful_json 4.0.0 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +38 -11
- data/lib/restful_json/controller.rb +12 -0
- data/lib/restful_json/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YjBiNTMxMDFhMDg0NDIwMTMzNzg1ZDVhYmQxYTlkNzM0OThiZTg2ZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjhjODQ3Yzc2Nzk4MTI0ZTkxYjI3MmMwYTBlNTZlY2U1ZTkwOTM1Ng==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzhkZGIwMzA4YzAzNjYyYWRjMmMzOTgwYTJiZDk1OGEwNjQ5MGQ1N2EzNGMx
|
10
|
+
NGM3MmQ4NDU2N2U4YmQzYWVlYmQzMTBiMTcxOTI4ZGY5Y2JkMDdjMzUyNTgx
|
11
|
+
NzYxYzk1YWZiY2I1NzUxYTQ5NTA0MzJmMGU2YWRlODQzOGI1MzM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MGY3NTJiOWNiOGE5MDZjMTBjMTllYTNiYmIzZjg1MDgyZDQ1OTc3MzZiZGMx
|
14
|
+
ZWZhMjliZTAwNTExNmFlZTcwZjM2MDZiZTQ0MTUyMDQ5YjRiMjcyYWIzMDMx
|
15
|
+
ZTI3MGU5OTM5NDQ0YjZkMWY0ZTAzZDQ0ZGEwMmMyZWIzZjcyODE=
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ Why do you need this if Rails controllers already make it easy to provide RESTfu
|
|
9
9
|
|
10
10
|
The goal of the project is to reduce service controller code in an intuitive way, not to be a be-everything DSL or limit what you can do in a controller. Choose what features to expose, and you can still define/redefine actions etc. at will.
|
11
11
|
|
12
|
-
We test with travis-ci with with Rails 3.1, 3.2, and Rails 4. Feel free to submit issues and/or do a pull
|
12
|
+
We test with travis-ci with with Rails 3.1, 3.2, and Rails 4. Feel free to submit issues and/or do a pull request if you run into anything.
|
13
13
|
|
14
14
|
You can use any of these for the JSON response (the view):
|
15
15
|
* [ActiveModel::Serializers][active_model_serializers]
|
@@ -614,6 +614,8 @@ query_for :index, is: ->(t,q) {
|
|
614
614
|
}
|
615
615
|
```
|
616
616
|
|
617
|
+
To avoid n+1 queries, use `.includes(...)` in your query to eager load any associations that you will need in the JSON view.
|
618
|
+
|
617
619
|
##### Define Custom Actions with Custom Queries
|
618
620
|
|
619
621
|
You are still working with regular controllers here, so add or override methods if you want more!
|
@@ -702,6 +704,32 @@ And by default restful_json allows action specific `(action)_(model)_params` met
|
|
702
704
|
self.allow_action_specific_params_methods = true
|
703
705
|
```
|
704
706
|
|
707
|
+
##### Avoid n+1 Queries
|
708
|
+
|
709
|
+
Unless using a custom query, if you specify `including {some hash}`, that will add `.includes({some hash})` to any automatically generated queries (in the proper place) such that eager loading can be used to avoid n+1 queries:
|
710
|
+
|
711
|
+
```ruby
|
712
|
+
class PostsController < ApplicationController
|
713
|
+
include RestfulJson::DefaultController
|
714
|
+
|
715
|
+
# eager loads all the posts and the associated category and comments for each post (note: have to define .includes(...) in query_for query)
|
716
|
+
including :category, :comments
|
717
|
+
end
|
718
|
+
```
|
719
|
+
|
720
|
+
or
|
721
|
+
|
722
|
+
```ruby
|
723
|
+
class PostsController < ApplicationController
|
724
|
+
include RestfulJson::DefaultController
|
725
|
+
|
726
|
+
# eager load all of the associated posts, the associated posts’ tags and comments, and every comment’s guest association
|
727
|
+
including posts: [{comments: :guest}, :tags]
|
728
|
+
end
|
729
|
+
```
|
730
|
+
|
731
|
+
Be careful- Rails doesn't raise an error if it includes associations that don't exist (at least in Rails 3.1-4.0).
|
732
|
+
|
705
733
|
### With Rails-api
|
706
734
|
|
707
735
|
If you want to try out [rails-api][rails-api]:
|
@@ -727,13 +755,12 @@ module MyServiceController
|
|
727
755
|
|
728
756
|
# use Permitters and AMS
|
729
757
|
include RestfulJson::DefaultController
|
730
|
-
# or comment that last line and uncomment whatever you want to use
|
731
|
-
#include ::ActionController::Serialization # AMS
|
732
|
-
#include ::ActionController::StrongParameters
|
733
|
-
#include ::TwinTurbo::Controller # Permitters which uses CanCan and Strong Parameters
|
734
|
-
#include ::RestfulJson::Controller
|
735
758
|
|
736
|
-
#
|
759
|
+
# or comment that last line and uncomment whatever you want to use
|
760
|
+
#include ActionController::Serialization # AMS
|
761
|
+
#include ActionController::StrongParameters
|
762
|
+
#include ActionController::Permittance # Permitters
|
763
|
+
#include RestfulJson::Controller
|
737
764
|
end
|
738
765
|
end
|
739
766
|
|
@@ -772,10 +799,10 @@ Don't do this:
|
|
772
799
|
|
773
800
|
```ruby
|
774
801
|
class ServiceController < ApplicationController
|
775
|
-
include
|
776
|
-
include
|
777
|
-
include ::
|
778
|
-
include
|
802
|
+
include ActionController::Serialization
|
803
|
+
include ActionController::StrongParameters
|
804
|
+
include ActionController::Permittance
|
805
|
+
include RestfulJson::Controller
|
779
806
|
end
|
780
807
|
|
781
808
|
class FoobarsController < ServiceController
|
@@ -36,6 +36,7 @@ module RestfulJson
|
|
36
36
|
class_attribute :param_to_through, instance_writer: true
|
37
37
|
class_attribute :action_to_serializer, instance_writer: true
|
38
38
|
class_attribute :action_to_serializer_for, instance_writer: true
|
39
|
+
class_attribute :query_includes, instance_writer: true
|
39
40
|
|
40
41
|
# use values from config
|
41
42
|
RestfulJson::CONTROLLER_OPTIONS.each do |key|
|
@@ -103,6 +104,10 @@ module RestfulJson
|
|
103
104
|
args.extract_options! # remove hash from array- we're not using it yet
|
104
105
|
self.supported_functions += args
|
105
106
|
end
|
107
|
+
|
108
|
+
def including(*args)
|
109
|
+
self.query_includes = args
|
110
|
+
end
|
106
111
|
|
107
112
|
# Specify a custom query. If action specified does not have a method, it will alias_method index to create a new action method with that query.
|
108
113
|
#
|
@@ -270,6 +275,9 @@ module RestfulJson
|
|
270
275
|
if @model_class.primary_key.is_a? Array
|
271
276
|
c = @model_class
|
272
277
|
c.primary_key.each {|pkey|c.where(pkey.to_sym => params[pkey].to_s)}
|
278
|
+
if self.query_includes
|
279
|
+
value.includes(*(self.query_includes))
|
280
|
+
end
|
273
281
|
# raise exception if not found
|
274
282
|
@value = c.first!
|
275
283
|
else
|
@@ -437,6 +445,10 @@ module RestfulJson
|
|
437
445
|
end
|
438
446
|
end
|
439
447
|
|
448
|
+
if self.query_includes
|
449
|
+
value.includes(*(self.query_includes))
|
450
|
+
end
|
451
|
+
|
440
452
|
if p_params[:page] && self.supported_functions.include?(:page)
|
441
453
|
page = p_params[:page].to_i
|
442
454
|
page = 1 if page < 1 # to avoid people using this as a way to get all records unpaged, as that probably isn't the intent?
|
data/lib/restful_json/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary S. Weaver
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|