restful_json 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjYzYjM4ODY4NjIyNWM4ZDdjMGQxODY0M2QyMGM5Y2MzMTVlMjFjZg==
4
+ YjBiNTMxMDFhMDg0NDIwMTMzNzg1ZDVhYmQxYTlkNzM0OThiZTg2ZQ==
5
5
  data.tar.gz: !binary |-
6
- MjA0NzNiYzc0OTJiMjU0OGE3ZjQ4ZWU5YmUyZTJhMTliYjdlZDg0ZA==
6
+ NjhjODQ3Yzc2Nzk4MTI0ZTkxYjI3MmMwYTBlNTZlY2U1ZTkwOTM1Ng==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YjIxMjNjYTFhZmM5M2E4Yjg3Mzc4MmMzZDA3MTIyMTgxMzYyZjBjZTRmODJi
10
- YjgyOWI3M2EzMGIzNGI4ZmI5YzI3ODRmZDBiMDc1NGMxY2IzNTBmYTFmMmEy
11
- ZjAzZmRlMjAzMWIyYWRmODVlYmFlNzhiODg0YTBmMWQ1Y2JiNTI=
9
+ YzhkZGIwMzA4YzAzNjYyYWRjMmMzOTgwYTJiZDk1OGEwNjQ5MGQ1N2EzNGMx
10
+ NGM3MmQ4NDU2N2U4YmQzYWVlYmQzMTBiMTcxOTI4ZGY5Y2JkMDdjMzUyNTgx
11
+ NzYxYzk1YWZiY2I1NzUxYTQ5NTA0MzJmMGU2YWRlODQzOGI1MzM=
12
12
  data.tar.gz: !binary |-
13
- NDJlYjExZThhNzU0OWJmYmJiOTQ3YTI3ZmQ0NDZhNDU4YmQxODYzYTk2ODBi
14
- NzgzNGZlOGJkMzZlMGJmODU3NDdkN2JhOTkxZDljZjc2NWE5ODliZTk4YjEy
15
- NzM0NzFiODEzZDNhNWM1ZDdiNjdjNGEwOTBmZjNjYzY4NGUzNjk=
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 requests if you run into anything.
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
- # If you want any additional inline class stuff, it goes here...
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 ::ActionController::Serialization
776
- include ::ActionController::StrongParameters
777
- include ::TwinTurbo::Controller
778
- include ::RestfulJson::Controller
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?
@@ -1,3 +1,3 @@
1
1
  module RestfulJson
2
- VERSION = '4.0.0'
2
+ VERSION = '4.1.0'
3
3
  end
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.0.0
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-04-29 00:00:00.000000000 Z
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport