introspective_grape 0.0.4 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +3 -0
  4. data/.rubocop.yml +1164 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +4 -2
  7. data/CHANGELOG.md +58 -0
  8. data/Gemfile +5 -3
  9. data/README.md +70 -17
  10. data/introspective_grape.gemspec +8 -8
  11. data/lib/.DS_Store +0 -0
  12. data/lib/introspective_grape/api.rb +177 -216
  13. data/lib/introspective_grape/camel_snake.rb +28 -59
  14. data/lib/introspective_grape/filters.rb +66 -0
  15. data/lib/introspective_grape/formatter/camel_json.rb +14 -0
  16. data/lib/introspective_grape/helpers.rb +63 -0
  17. data/lib/introspective_grape/traversal.rb +54 -0
  18. data/lib/introspective_grape/version.rb +1 -1
  19. data/lib/introspective_grape.rb +11 -0
  20. data/spec/.DS_Store +0 -0
  21. data/spec/dummy/Gemfile +5 -3
  22. data/spec/dummy/app/api/.DS_Store +0 -0
  23. data/spec/dummy/app/api/api_helpers.rb +5 -6
  24. data/spec/dummy/app/api/dummy/chat_api.rb +1 -2
  25. data/spec/dummy/app/api/dummy/company_api.rb +16 -1
  26. data/spec/dummy/app/api/dummy/location_api.rb +3 -3
  27. data/spec/dummy/app/api/dummy/project_api.rb +1 -0
  28. data/spec/dummy/app/api/dummy/sessions.rb +4 -8
  29. data/spec/dummy/app/api/dummy/user_api.rb +3 -1
  30. data/spec/dummy/app/api/dummy_api.rb +6 -6
  31. data/spec/dummy/app/api/error_handlers.rb +2 -2
  32. data/spec/dummy/app/models/chat_user.rb +1 -1
  33. data/spec/dummy/app/models/image.rb +2 -2
  34. data/spec/dummy/app/models/role.rb +1 -1
  35. data/spec/dummy/app/models/user/chatter.rb +6 -6
  36. data/spec/dummy/app/models/user_project_job.rb +3 -3
  37. data/spec/dummy/config/application.rb +1 -1
  38. data/spec/dummy/db/migrate/20150824215701_create_images.rb +3 -3
  39. data/spec/dummy/db/schema.rb +1 -1
  40. data/spec/models/image_spec.rb +1 -1
  41. data/spec/models/role_spec.rb +5 -5
  42. data/spec/models/user_location_spec.rb +2 -2
  43. data/spec/models/user_project_job_spec.rb +1 -1
  44. data/spec/rails_helper.rb +3 -1
  45. data/spec/requests/company_api_spec.rb +28 -0
  46. data/spec/requests/location_api_spec.rb +19 -2
  47. data/spec/requests/project_api_spec.rb +34 -3
  48. data/spec/requests/sessions_api_spec.rb +1 -1
  49. data/spec/requests/user_api_spec.rb +24 -3
  50. data/spec/support/blueprints.rb +3 -3
  51. data/spec/support/location_helper.rb +26 -21
  52. data/spec/support/request_helpers.rb +1 -3
  53. metadata +58 -28
  54. data/spec/dummy/app/api/active_record_helpers.rb +0 -17
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/.travis.yml CHANGED
@@ -1,14 +1,16 @@
1
1
  language: ruby
2
+ before_install:
3
+ - gem install bundler
2
4
  install: bundle install --jobs=1 --retry=1
3
5
  script:
4
6
  - bundle install
5
7
  - bundle exec rspec
6
8
 
7
9
  rvm:
10
+ - 2.3.0
8
11
  - 2.2
9
12
  - 2.1
10
13
  - 2.0.0
11
- - rbx-2.5.8
12
14
  - jruby-9.0.4.0
13
15
  - ruby-head
14
16
  - jruby-head
@@ -22,7 +24,7 @@ env:
22
24
  matrix:
23
25
  - RAILS=3.2.22
24
26
  - RAILS=4.1.13
25
- - RAILS=4.2.4
27
+ - RAILS=4.2.5.1
26
28
  - RAILS=master
27
29
  global:
28
30
  - JRUBY_OPTS="-J-Xmx1024m --debug"
data/CHANGELOG.md ADDED
@@ -0,0 +1,58 @@
1
+
2
+ 0.1.9 9/27/2016
3
+ ==============
4
+ ### Features
5
+
6
+ Allow identifier filters for foreign and primary keys to accept comma separated lists.
7
+
8
+ 0.1.8 9/25/2016
9
+ ==============
10
+
11
+ ### Features
12
+
13
+ Add date range filters for the index page.
14
+
15
+ 0.1.6 9/10/2016
16
+ ==============
17
+
18
+ ### Bug Fix
19
+
20
+ The before hook snake casing parameters in the API was preventing the assignment of default values, this was moved to an after_validation hook.
21
+
22
+ ### Features
23
+
24
+ Grape::Kaminari pagination was added to the index actions if configured in the parent class.
25
+
26
+ 0.1.5 6/26/2016
27
+ ==============
28
+
29
+ ### Bug Fix
30
+
31
+ Reload the model from the database before presenting it to the user after create/update, as
32
+ some deeply nested association changes will not be properly loaded by active record.
33
+
34
+ 0.1.4 5/11/2016
35
+ ==============
36
+
37
+ ### Features
38
+
39
+ Added an include_actions declaration as the inverse of exclude_actions.
40
+
41
+ 0.1.1 5/11/2016
42
+ ==============
43
+
44
+ ### Features
45
+
46
+ Stop monkey patching Grape's json formatter and instead use Grape's "formatter"
47
+ with our own CamelJson module.
48
+
49
+ 0.1.0 5/8/2016
50
+ ==============
51
+
52
+ ### Features
53
+
54
+ Add simple filter for index endpoints.
55
+
56
+ ### Fixes
57
+
58
+ Refactor API generation to reduce the code complexity.
data/Gemfile CHANGED
@@ -5,11 +5,13 @@ source 'https://rubygems.org'
5
5
  # development dependencies will be added by default to the :development group.
6
6
  gemspec
7
7
 
8
+ gem 'coveralls', require: false
9
+
8
10
  # Declare any dependencies that are still in development here instead of in
9
11
  # your gemspec. These might include edge Rails or gems from your path or
10
12
  # Git. Remember to move these dependencies to your gemspec before releasing
11
13
  # your gem to rubygems.org.
12
14
 
13
- # To use a debugger
14
- # gem 'byebug', group: [:development, :test]
15
-
15
+ group :development do
16
+ # gem 'byebug'
17
+ end
data/README.md CHANGED
@@ -18,20 +18,22 @@
18
18
 
19
19
 
20
20
  IntrospectiveGrape is a Rails Plugin for DRYing up Grape APIs by laying out simple
21
- defaults and including deeply nested relations according to the models'
22
- accepts_nested_attributes_for :relation declarations.
21
+ RESTful defaults based on the model definitions. If you use a schema validator
22
+ like [SchemaPlus](https://github.com/SchemaPlus/schema_plus) it will, by
23
+ extension, define your endpoints according to your database schema.
23
24
 
24
- IntrospectiveGrape supports file uploads via Paperclip and hypothetically supports CarrierWave.
25
+ It provides handling for deeply nested relations according to the models'
26
+ `accepts_nested_attributes_for` declarations, generating all the necessary
27
+ boilerplate for flexible and consistent bulk endpoints on plural associations,
28
+ and building nested routes for the same.
25
29
 
26
- Presently it is tightly coupled with two behaviors that I like but should be abstracted out:
30
+ It also snake cases everything coming in and camelizes parameters in your swagger docs
31
+ by default if you `require 'introspective_grape/camel_snake'` in your API.
32
+ This behavior can be disabled.
27
33
 
28
- 1. It is dependent on Pundit for authorization and permissions.
29
-
30
- 2. Parameters in the front end of the API will be accepted in camel case and passed to the backend in snake case, following javascript conventions on the one and rails conventions in the other.
31
-
32
- Libraries for Grape and Swagger docs are rather invasively duck typed to support this behavior. It modifies Grape's JSON Formatter module and Grape Swagger's documentation classes to camelize parameter keys, and then converts the keys back to snake case for handling in the API.
33
-
34
- To include this behavior in your test coverage you need to either access the API's params hash or you can `include IntrospectiveGrape::CamelSnake` in your test helper and `snake_keys(JSON.parse(response.body))` to format the params in a helper method.
34
+ In addition it provides a `IntrospectiveGrape::Formatter::CamelJson` json formatter to
35
+ recursively camelize the keys of all your outputs, so ruby and javascript developers
36
+ can speak in their own idioms.
35
37
 
36
38
  ## Documentation
37
39
 
@@ -41,16 +43,49 @@ In your Gemfile:
41
43
  gem 'introspective_grape'
42
44
  ```
43
45
 
44
- And bundle install. In app/api/v1/my_model_api.rb:
46
+ And bundle install.
47
+
48
+
49
+ ## Grape Configuration
50
+
51
+ IntrospectiveGrape's default behavior is to camelize all outputs and snake case all inputs. To camel case all your json output you'll need to use its formatter in your API:
52
+
53
+ ```
54
+ formatter :json, IntrospectiveGrape::Formatter::CamelJson
55
+ ```
56
+
57
+ It also defaults to monkey patching Grape::Swagger to camelize the API's parameters in the swagger docs and, vice-versa, snake casing the parameters that are sent to your API.
58
+
59
+ You can disable this behavior by setting `IntrospectiveGrape.config.camelize_parameters = false`.
60
+
61
+ To include this behavior in your test coverage you need to either access the API's params hash or you can format the response body to `JSON.parse(response.body).with_snake_keys` in a helper method.
62
+
63
+ ## Authentication and authorization
64
+
65
+ Authentication and authorization are presently enforced on every endpoint. If you have named the authentication helper method in Grape something other than "authenticate!" or "authorize!" you can set it with:
66
+
67
+ ```
68
+ IntrospectiveGrape::API.authentication_method = "whatever!"
69
+ ```
70
+
71
+ Pundit authorization is invoked against index?, show?, update?, create?, and destroy? methods with the model instance in question (or a new instance in the case of index).
72
+
73
+
74
+ ## Generate End Points for Models
75
+
76
+ In app/api/v1/my_model_api.rb:
45
77
 
46
78
  ```
47
79
  class MyModelAPI < IntrospectiveGrape::API
80
+ skip_presence_validations :attribute
48
81
  exclude_actions Model, <:index,:show,:create,:update,:destroy>
49
82
  default_includes Model, <associations for eager loading>
50
83
 
51
- exclude_actions NestedModel, <:index,:show,:create,:update,:destroy>
84
+ include_actions NestedModel, <:index,:show,:create,:update,:destroy>
52
85
  default_includes NestedModel, <associations for eager loading>
53
86
 
87
+ paginate per_page 25, offset: 0, max_per_page: false
88
+
54
89
  restful MyModel, [:strong, :param, :fields, :and, { nested_attributes: [:nested,:fields, :_destroy] }] do
55
90
  # Add additional end points to the model's namespace
56
91
  end
@@ -66,10 +101,10 @@ class MyModelAPI < IntrospectiveGrape::API
66
101
  end
67
102
  ```
68
103
 
69
- A Pundit policy will need to be defined for :index?,:show?,:update?,:create?, and
70
- :destroy? as well as a policy scope for the index action. IntrospectiveGrape
71
- automatically enforces Pundit's authorize! before all actions.
72
-
104
+ If a model has, say, a procedurally generated default for a not-null field
105
+ `skip_presence_validations` will make IntrospectiveGrape declare the parameter
106
+ optional rather than required.
107
+
73
108
  To define a Grape param type for a virtual attribute or override the defaut param
74
109
  type from model introspection, define a class method in the model with the param
75
110
  types for the attributes specified in a hash, e.g.:
@@ -85,6 +120,24 @@ nested params as well as nested routes will be declared, allowing for
85
120
  a good deal of flexibility for API consumers out of the box, such as implicitly
86
121
  creating bulk update endpoints for nested models.
87
122
 
123
+ ## Pagination
124
+
125
+ The index action by default will not be paginated, simply declared `paginate` before the `restful` declaration will enable [Kaminari](https://github.com/amatsuda/kaminari) pagination on the index results using a default 25 results per page with an offset of 0.
126
+
127
+ ## Excluding Endpoints
128
+
129
+ By default any association included in the strong params argument will have all
130
+ RESTful (`:index,:show,:create,:update, :destroy`) endpoints defined. These can
131
+ be excluded (or conversely included) with the `exclude_actions` or `include_actions`
132
+ declarations on the model. You can also include or exclude :all or :none as shorthand.
133
+
134
+ ## Grape Hooks
135
+
136
+ Grape only applies hooks in the order they were declared, so to hook into the default
137
+ RESTful actions defined by IntrospectiveGrape you need to declare any hooks before the
138
+ `restful` declaration, rather than inside its block, where the hook will only apply to
139
+ subsequently declared endpoints.
140
+
88
141
 
89
142
  ## Dependencies
90
143
 
@@ -20,13 +20,14 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.required_ruby_version = '~> 2.0'
22
22
 
23
- s.add_dependency "rails"
24
- s.add_dependency "activerecord"
23
+ s.add_dependency "rails", '>= 3.0.0', '< 5.0.0'
25
24
 
26
- s.add_dependency 'grape'
27
- s.add_dependency 'grape-entity'
28
- s.add_dependency 'grape-swagger', '~> 0.10.4'
25
+ s.add_dependency 'grape', '~> 0.16.2'
26
+ s.add_dependency 'grape-entity', '< 0.5.0'
27
+ s.add_dependency 'grape-swagger', '~>0.11.0'
28
+ s.add_dependency 'grape-kaminari', '~>0.1.9'
29
29
  s.add_dependency 'pundit'
30
+ s.add_dependency 'camel_snake_keys', '~>0.0.2'
30
31
 
31
32
  if RUBY_PLATFORM == 'java'
32
33
  #s.add_development_dependency "jdbc-sqlite3"
@@ -35,15 +36,14 @@ Gem::Specification.new do |s|
35
36
  s.add_development_dependency "sqlite3"
36
37
  end
37
38
 
39
+ #s.add_development_dependency "byebug"
38
40
  s.add_development_dependency "rspec-rails", '>= 3.0'
39
41
  s.add_development_dependency 'devise'
40
42
  s.add_development_dependency 'devise-async'
41
- s.add_development_dependency 'paperclip'
43
+ s.add_development_dependency 'paperclip', '< 5.0'
42
44
  s.add_development_dependency 'machinist'
43
45
  s.add_development_dependency 'simplecov'
44
46
  s.add_development_dependency 'rufus-mnemo'
45
- #s.add_development_dependency "schema_plus", "2.0.0.pre12" # For compatibility of schema_validations with AR 4.2.1+
46
- #s.add_development_dependency "schema_validations"
47
47
  s.add_development_dependency "activerecord-tableless", "~> 1.0"
48
48
 
49
49
  end
data/lib/.DS_Store ADDED
Binary file