jsonapi_actions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f7715c24debc264384b44d0d58e7c35bb233933a5d7ce65dac9cf1d5b7442a20
4
+ data.tar.gz: d4280a01b00fa35db370ffa051a6bdab451400d3a4517097e0901ddf2237dbc5
5
+ SHA512:
6
+ metadata.gz: 1b3ede4f07f319d0fbef81a168ce9d98c84e81a2685df7ea826c347f85ae6502e4a45a275e8e5886e539aeab8c12fad19090f9a7db163242383214772b2e2338
7
+ data.tar.gz: 3a3e17111e6759c9c79494873b61a4da4993e7d0c59cd03419368ebd6bf8a385c3b136068b5da4692d9d5917ff9eb74bdb4b3e0e541892508ad94d10bc66e664
File without changes
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ jsonapi_actions
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kevin@kpsoftware.io. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jsonapi_actions.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,155 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jsonapi_actions (0.1.0)
5
+ kaminari (>= 1.0, < 2.0)
6
+ rails (>= 4.0, < 6.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.2.2)
12
+ actionpack (= 5.2.2)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailer (5.2.2)
16
+ actionpack (= 5.2.2)
17
+ actionview (= 5.2.2)
18
+ activejob (= 5.2.2)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.2.2)
22
+ actionview (= 5.2.2)
23
+ activesupport (= 5.2.2)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.2.2)
29
+ activesupport (= 5.2.2)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.2.2)
35
+ activesupport (= 5.2.2)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.2.2)
38
+ activesupport (= 5.2.2)
39
+ activerecord (5.2.2)
40
+ activemodel (= 5.2.2)
41
+ activesupport (= 5.2.2)
42
+ arel (>= 9.0)
43
+ activestorage (5.2.2)
44
+ actionpack (= 5.2.2)
45
+ activerecord (= 5.2.2)
46
+ marcel (~> 0.3.1)
47
+ activesupport (5.2.2)
48
+ concurrent-ruby (~> 1.0, >= 1.0.2)
49
+ i18n (>= 0.7, < 2)
50
+ minitest (~> 5.1)
51
+ tzinfo (~> 1.1)
52
+ arel (9.0.0)
53
+ builder (3.2.3)
54
+ concurrent-ruby (1.1.5)
55
+ crass (1.0.4)
56
+ diff-lcs (1.3)
57
+ erubi (1.8.0)
58
+ globalid (0.4.2)
59
+ activesupport (>= 4.2.0)
60
+ i18n (1.6.0)
61
+ concurrent-ruby (~> 1.0)
62
+ kaminari (1.1.1)
63
+ activesupport (>= 4.1.0)
64
+ kaminari-actionview (= 1.1.1)
65
+ kaminari-activerecord (= 1.1.1)
66
+ kaminari-core (= 1.1.1)
67
+ kaminari-actionview (1.1.1)
68
+ actionview
69
+ kaminari-core (= 1.1.1)
70
+ kaminari-activerecord (1.1.1)
71
+ activerecord
72
+ kaminari-core (= 1.1.1)
73
+ kaminari-core (1.1.1)
74
+ loofah (2.2.3)
75
+ crass (~> 1.0.2)
76
+ nokogiri (>= 1.5.9)
77
+ mail (2.7.1)
78
+ mini_mime (>= 0.1.1)
79
+ marcel (0.3.3)
80
+ mimemagic (~> 0.3.2)
81
+ method_source (0.9.2)
82
+ mimemagic (0.3.3)
83
+ mini_mime (1.0.1)
84
+ mini_portile2 (2.4.0)
85
+ minitest (5.11.3)
86
+ nio4r (2.3.1)
87
+ nokogiri (1.10.1)
88
+ mini_portile2 (~> 2.4.0)
89
+ rack (2.0.6)
90
+ rack-test (1.1.0)
91
+ rack (>= 1.0, < 3)
92
+ rails (5.2.2)
93
+ actioncable (= 5.2.2)
94
+ actionmailer (= 5.2.2)
95
+ actionpack (= 5.2.2)
96
+ actionview (= 5.2.2)
97
+ activejob (= 5.2.2)
98
+ activemodel (= 5.2.2)
99
+ activerecord (= 5.2.2)
100
+ activestorage (= 5.2.2)
101
+ activesupport (= 5.2.2)
102
+ bundler (>= 1.3.0)
103
+ railties (= 5.2.2)
104
+ sprockets-rails (>= 2.0.0)
105
+ rails-dom-testing (2.0.3)
106
+ activesupport (>= 4.2.0)
107
+ nokogiri (>= 1.6)
108
+ rails-html-sanitizer (1.0.4)
109
+ loofah (~> 2.2, >= 2.2.2)
110
+ railties (5.2.2)
111
+ actionpack (= 5.2.2)
112
+ activesupport (= 5.2.2)
113
+ method_source
114
+ rake (>= 0.8.7)
115
+ thor (>= 0.19.0, < 2.0)
116
+ rake (10.5.0)
117
+ rspec (3.8.0)
118
+ rspec-core (~> 3.8.0)
119
+ rspec-expectations (~> 3.8.0)
120
+ rspec-mocks (~> 3.8.0)
121
+ rspec-core (3.8.0)
122
+ rspec-support (~> 3.8.0)
123
+ rspec-expectations (3.8.2)
124
+ diff-lcs (>= 1.2.0, < 2.0)
125
+ rspec-support (~> 3.8.0)
126
+ rspec-mocks (3.8.0)
127
+ diff-lcs (>= 1.2.0, < 2.0)
128
+ rspec-support (~> 3.8.0)
129
+ rspec-support (3.8.0)
130
+ sprockets (3.7.2)
131
+ concurrent-ruby (~> 1.0)
132
+ rack (> 1, < 3)
133
+ sprockets-rails (3.2.1)
134
+ actionpack (>= 4.0)
135
+ activesupport (>= 4.0)
136
+ sprockets (>= 3.0.0)
137
+ thor (0.20.3)
138
+ thread_safe (0.3.6)
139
+ tzinfo (1.2.5)
140
+ thread_safe (~> 0.1)
141
+ websocket-driver (0.7.0)
142
+ websocket-extensions (>= 0.1.0)
143
+ websocket-extensions (0.1.3)
144
+
145
+ PLATFORMS
146
+ ruby
147
+
148
+ DEPENDENCIES
149
+ bundler (~> 2.0)
150
+ jsonapi_actions!
151
+ rake (~> 10.0)
152
+ rspec (~> 3.0)
153
+
154
+ BUNDLED WITH
155
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kevin Pheasey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,253 @@
1
+ # JsonapiActions
2
+
3
+ Instantly create flexible API controllers that are compatible with [JSON:API](https://jsonapi.org/).
4
+ Utilize your existing [FastJsonapi](https://github.com/Netflix/fast_jsonapi) or
5
+ [ActiveModel::Serializer](https://github.com/rails-api/active_model_serializers) serialization library, or bring your
6
+ own. Scope and authenticate with optional [Pundit](https://github.com/varvet/pundit) policies and/or Controller specific
7
+ methods.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'jsonapi_actions'
15
+ ```
16
+
17
+ And then execute:
18
+ ```bash
19
+ $ bundle install
20
+ ```
21
+
22
+ ## Usage
23
+
24
+
25
+ ### Basic Setup
26
+
27
+ Include the `JsonapiActions::ErrorHandling` and `JsonapiActions::ErrorHandling` modules in
28
+ your base controller.
29
+
30
+ ```ruby
31
+ # app/controllers/api/v1/base_controller.rb
32
+ module Api::V1
33
+ class BaseController < ApplicationController
34
+ include JsonApiActions
35
+
36
+ respond_to :json
37
+ end
38
+ end
39
+ ```
40
+
41
+ Define the Model for each child Controller.
42
+
43
+ ```ruby
44
+ # app/controllers/api/v1/projects_controller.rb
45
+ module Api::V1
46
+ class ProjectsController < BaseController
47
+ self.model = Project
48
+ end
49
+ end
50
+ ```
51
+
52
+ Define your routes.
53
+
54
+ ```ruby
55
+ # config/routes.rb
56
+ Rails.application.routes.draw do
57
+ namespace :api, defaults: { format: 'json' } do
58
+ namespace :v1 do
59
+ resources :projects
60
+ end
61
+ end
62
+ end
63
+
64
+ ```
65
+
66
+ ### parent_scope
67
+ Index actions can be scoped by parent associations. i.e. nested routes.
68
+
69
+ ```ruby
70
+ # app/models/project.rb
71
+ class Project < ApplicationRecord
72
+ belongs_to :user
73
+ end
74
+
75
+ # config/routes.rb
76
+ Rails.application.routes.draw do
77
+ namespace :api, defaults: { format: 'json' } do
78
+ namespace :v1 do
79
+ # /api/v1/projects
80
+ resources :projects
81
+
82
+ resources :users do
83
+ # /api/v1/users/:user_id/projects
84
+ resources :projects
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ # app/controllers/api/v1/projects_controller.rb
91
+ module Api::V1
92
+ class ProjectsController < BaseController
93
+ self.model = Project
94
+ self.parent_associations = [
95
+ # When Using using attribute on the model
96
+ # Project.where(user_id: params[:user_id])
97
+ { param: :user_id, attribute: :user_id },
98
+
99
+ # When using attribute on the associated model
100
+ # Project.joins(:user).where(users: { id: params[:user_id] })
101
+ { param: :user_id, association: :user, table: :users, attribute: :id }
102
+ ]
103
+ end
104
+ end
105
+ ```
106
+
107
+ ### Custom Actions (Non-CRUD)
108
+ You can easily utilize JsonapiActions in custom controller actions too. Just call `#set_record` to
109
+ initialize `@record` and when you're done `render response(@record)`
110
+
111
+ ```ruby
112
+ # app/controllers/api/v1/projects_controller.rb
113
+ module Api::V1
114
+ class ProjectsController < BaseController
115
+ self.model = Project
116
+
117
+ def activate
118
+ set_record
119
+ @record.activate!
120
+ render response(@record)
121
+ rescue ActiveRecord::RecordInvalid
122
+ render unprocessable_entity(@record)
123
+ end
124
+ end
125
+ end
126
+ ```
127
+
128
+ ### #serializer
129
+ Controller actions render JSON data via a Serializer. We assume a `Model` has a `ModelSerializer`. To use a
130
+ different serializer, define `self.serializer = OtherSerializer` on the Controller.
131
+
132
+ ```ruby
133
+ # app/controllers/api/v1/base_controller.rb
134
+ module Api::V1
135
+ class ProjectsController < BaseController
136
+ self.model = Project
137
+ self.serializer = SecretProjectSerializer
138
+ end
139
+ end
140
+ ```
141
+
142
+ ### #json_response
143
+ Response data is formatted so that it can be rendered with `FastJsonapi` or `ActiveModel::Serializer`.
144
+ If you are using a different serializer, or would like to further change the response. Then you will need to override
145
+ `#response`, which defines the arguments for `render`.
146
+
147
+ ```ruby
148
+ # app/controllers/api/v1/base_controller.rb
149
+ module Api::V1
150
+ class BaseController < ApplicationController
151
+ include JsonapiActions
152
+
153
+ respond_to :json
154
+
155
+ private
156
+
157
+ def json_response(data, options = {})
158
+ { json: data }.merge(options)
159
+ end
160
+ end
161
+ end
162
+ ```
163
+
164
+ ## Pundit
165
+
166
+ JsonapiActions are built to use Pundit for authorization. We utilize action authorization, policy scope,
167
+ and permitted params.
168
+
169
+ ```ruby
170
+ # app/policies/project_policy.rb
171
+ class ProjectPolicy < ApplicationPolicy
172
+ def index?
173
+ true
174
+ end
175
+
176
+ def show?
177
+ record.user == user
178
+ end
179
+
180
+ def create?
181
+ record.user == user
182
+ end
183
+
184
+ def update?
185
+ record.user == user
186
+ end
187
+
188
+ def destroy?
189
+ record.user == user
190
+ end
191
+
192
+ def permitted_params
193
+ %i[user_id name]
194
+ end
195
+
196
+ class Scope < Scope
197
+ def resolve
198
+ scope.where(user_id: user.id)
199
+ end
200
+ end
201
+ end
202
+ ```
203
+
204
+ ### Usage without Pundit
205
+
206
+ If you are not using Pundit for authorization, then you will need to defined `#permitted_params`.
207
+ You can optionally override methods for `#policy_scope` and `#authorize` too.
208
+
209
+ ```ruby
210
+ module Api::V1
211
+ class BaseController < ApplicationController
212
+ include JsonapiActions::ErrorHandling
213
+
214
+ respond_to :json
215
+
216
+ private
217
+
218
+ def permitted_params
219
+ %i[user_id name]
220
+ end
221
+
222
+ # This override is optional
223
+ def policy_scope(scope)
224
+ scope.where(user_id: current_user.id)
225
+ end
226
+
227
+ # This override is optional
228
+ def authorize(record, query = nil)
229
+ return if record.user == current_user
230
+
231
+ raise NotAuthorized
232
+ end
233
+ end
234
+ end
235
+ ````
236
+
237
+ ## Development
238
+
239
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
240
+
241
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
242
+
243
+ ## Contributing
244
+
245
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsonapi_actions. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
246
+
247
+ ## License
248
+
249
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
250
+
251
+ ## Code of Conduct
252
+
253
+ Everyone interacting in the JsonapiActions project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jsonapi_actions/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonapi_actions"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jsonapi_actions/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonapi_actions"
8
+ spec.version = JsonapiActions::VERSION
9
+ spec.authors = ["Kevin Pheasey"]
10
+ spec.email = ["kevin@kpsoftware.io"]
11
+
12
+ spec.summary = "Rails JSONAPI Controller Actions"
13
+ spec.description = "Implement Rails JSONAPI compliant controller actions."
14
+ spec.homepage = "https://github.com/kp-software/jsonapi_actions"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+
30
+ spec.add_dependency 'rails', '>= 4.0', '< 6.0'
31
+ spec.add_dependency 'kaminari', '>= 1.0', '< 2.0'
32
+ end
@@ -0,0 +1,211 @@
1
+ require 'active_support/concern'
2
+
3
+ require 'jsonapi_actions/authorization'
4
+ require 'jsonapi_actions/eager_loader'
5
+ require 'jsonapi_actions/error_handling'
6
+ require 'jsonapi_actions/inclusion_mapper'
7
+ require 'jsonapi_actions/version'
8
+
9
+ module JsonapiActions
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include ErrorHandling
14
+ include Authorization
15
+
16
+ # The model's Class OR the class name
17
+ self.model = nil
18
+ self.serializer = nil
19
+ self.model_name = 'ApplicationRecord'
20
+
21
+ # defines a set of parent associations to scope the index result by
22
+ # i.e. [{ param: :project_id, attribute: :project_id }]
23
+ # OR [{ param: :project_id, association: :project }]
24
+ self.parent_associations = []
25
+
26
+ before_action :set_record, only: %i[show update destroy]
27
+
28
+ def index
29
+ authorize model, :index?
30
+ @records = policy_scope(model.all)
31
+ @records = parent_scope(@records)
32
+ @records = filter(@records)
33
+ @records = sort(@records)
34
+ @records = paginate(@records)
35
+ @records = eager_load(@records)
36
+
37
+ render json_response(@records, meta: pagination_meta(@records).merge(metadata))
38
+ end
39
+
40
+ def show
41
+ render json_response(@record)
42
+ end
43
+
44
+ def create
45
+ @record = model.new
46
+ @record.assign_attributes(record_params)
47
+ @record.id = id_param if id_param
48
+ authorize(@record)
49
+
50
+ if @record.save
51
+ @record.reload # ensure we have after commit stuff from things like carrierwave
52
+ render json_response(@record, status: :created)
53
+ else
54
+ render unprocessable_entity(@record)
55
+ end
56
+ end
57
+
58
+ def update
59
+ if @record.update(record_params)
60
+ @record.reload # ensure we have after commit stuff from things like carrierwave
61
+ render json_response(@record)
62
+ else
63
+ render unprocessable_entity(@record)
64
+ end
65
+ end
66
+
67
+ def destroy
68
+ if @record.destroy
69
+ render json: {}, status: 204
70
+ else
71
+ render unprocessable_entity(@record)
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def metadata
78
+ {}
79
+ end
80
+
81
+ def parent_scope(records)
82
+ return records if self.class.parent_associations.blank?
83
+
84
+ self.class.parent_associations.each do |parent|
85
+ next if params[parent[:param]].blank?
86
+
87
+ records = records.joins(parent[:association]) unless parent[:association].blank?
88
+ records = if parent[:table]
89
+ records.where(parent[:table] => { parent[:attribute] => params[parent[:param]] })
90
+ else
91
+ records.where(parent[:attribute] => params[parent[:param]])
92
+ end
93
+ end
94
+
95
+ records
96
+ end
97
+
98
+ def filter(records)
99
+ records
100
+ end
101
+
102
+ def sort(records)
103
+ return records if params[:sort].blank?
104
+ order = {}
105
+
106
+ params[:sort].split(',').each do |sort|
107
+ if sort[0] == '-'
108
+ order[sort[1..-1]] = :desc
109
+ else
110
+ order[sort] = :asc
111
+ end
112
+ end
113
+
114
+ return records.order(order)
115
+ end
116
+
117
+ def paginate(records)
118
+ records.page(page[:number]).per(page[:size])
119
+ end
120
+
121
+ def page
122
+ @page ||= begin
123
+ page = {}
124
+ page[:number] = (params.dig(:page, :number) || 1).to_i
125
+ page[:size] = [[(params.dig(:page, :size) || 20).to_i, 1000].min, 1].max
126
+ page
127
+ end
128
+ end
129
+
130
+ def set_record
131
+ @record = model.find(params[:id])
132
+ authorize(@record)
133
+ end
134
+
135
+ def id_param
136
+ params.require(:data).permit(policy(@record).permitted_attributes)[:id]
137
+ end
138
+
139
+ def record_params
140
+ params.require(:data).require(:attributes).permit(policy(@record).permitted_attributes)
141
+ end
142
+
143
+ def include_param
144
+ if %w[* **].include? params[:include]
145
+ inclusion_map
146
+ else
147
+ params[:include].to_s.split(',').reject(&:blank?).map(&:to_sym)
148
+ end
149
+ end
150
+
151
+ def inclusion_map
152
+ InclusionMapper.new(serializer, include: params[:include]).map
153
+ end
154
+
155
+ def unprocessable_entity(record)
156
+ Rails.logger.debug(record.errors.messages)
157
+ { json: record.errors.messages, status: :unprocessable_entity }
158
+ end
159
+
160
+ def pagination_meta(collection)
161
+ return {} if collection.nil?
162
+
163
+ {
164
+ current_page: collection.current_page,
165
+ next_page: collection.next_page,
166
+ prev_page: collection.prev_page,
167
+ total_pages: collection.total_pages,
168
+ total_count: collection.total_count
169
+ }
170
+ end
171
+
172
+ def model
173
+ self.class.model || self.class.model_name.constantize
174
+ end
175
+
176
+ def json_response(data, options = {})
177
+ if Gem::Specification.find_by_name('fast_jsonapi')
178
+ {
179
+ json: serializer(data).new(data, options.deep_merge(
180
+ include: include_param,
181
+ meta: metadata,
182
+ params: {
183
+ current_user: try(:current_user)
184
+ })
185
+ )
186
+ }
187
+
188
+ elsif Gem::Specification.find_by_name('active_model_serializers')
189
+ { json: data }.merge(meta: metadata, include: include_param).merge(options)
190
+
191
+ else
192
+ { json: { data: data }.merge(options) }
193
+ end
194
+ end
195
+
196
+ def serializer(data = nil)
197
+ self.class.serializer ||
198
+ data.try(:serializer_class) ||
199
+ data.try(:first).try(:serializer_class) ||
200
+ "#{model.name}Serializer".constantize
201
+ end
202
+
203
+ def eager_load(records)
204
+ records
205
+ end
206
+ end
207
+
208
+ module ClassMethods
209
+ attr_accessor :model, :parent_associations, :model_name, :serializer
210
+ end
211
+ end
@@ -0,0 +1,23 @@
1
+ module JsonapiActions
2
+ module Authorization
3
+ extend ActiveSupport::Concern
4
+
5
+ if !Gem::Specification.find_by_name('pundit')
6
+ def policy_scope(scope)
7
+ scope
8
+ end
9
+
10
+ def policy(record)
11
+ OpenStruct.new(permitted_attributes: permitted_attributes)
12
+ end
13
+
14
+ def permitted_attributes
15
+ []
16
+ end
17
+
18
+ def authorize(record, query = nil)
19
+ # do nothing
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ module JsonapiActions
2
+ class EagerLoader
3
+ attr_reader :records, :serializer, :includes
4
+
5
+ # @param records [ActiveRecord::Relation]
6
+ # @param serializer
7
+ # @param includes [Array<Symbol>]
8
+ def initialize(records, serializer, includes)
9
+ @records = records
10
+ @serializer = serializer
11
+ @includes = includes
12
+ end
13
+
14
+ # @return [ActiveRecord::Relation]
15
+ def eager_load
16
+ serializer.relationships_to_serialize&.each do |rel|
17
+ next if @records.eager_load_values.include?(rel[0])
18
+ @records = @records.eager_load(rel[0])
19
+ end
20
+
21
+ includes.each do |include|
22
+ rel = path_to_relationship(include.to_s.split('.'))
23
+ next if @records.eager_load_values.include?(rel)
24
+ @records = records.eager_load(rel)
25
+ end
26
+
27
+ @records
28
+ end
29
+
30
+ private
31
+
32
+ def path_to_relationship(parts)
33
+ if parts.length == 1
34
+ parts[0].to_sym
35
+ else parts.length == 2
36
+ { parts[0].to_sym => path_to_relationship(parts[1..-1]) }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonapiActions
4
+ module ErrorHandling
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ rescue_from ActionController::ParameterMissing, with: :bad_request
9
+ rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
10
+
11
+ def bad_request(error)
12
+ render json: { errors: [{ status: 400, title: 'Bad Request', detail: error.message }] }, status: :bad_request
13
+ end
14
+
15
+ def record_not_found(error)
16
+ render json: { errors: [{ status: 404, title: 'Not Found', detail: error.message }] }, status: :not_found
17
+ end
18
+
19
+ def user_not_authorized(error)
20
+ render json: { errors: [{ status: 403, title: 'Forbidden', detail: error.message }] }, status: :forbidden
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,48 @@
1
+ module JsonapiActions
2
+ class InclusionMapper
3
+ attr_reader :map, :root
4
+
5
+ def initialize(serializer, include: '*')
6
+ @included = {}
7
+ @recursive = include == '**'
8
+ @root = serializer.record_type
9
+ @map = include_relationships(serializer)
10
+ end
11
+
12
+ private
13
+
14
+ # TODO: figure out how to avoid joining an existing relationship;
15
+ # community.units.projects.features.room.project.project_services.features.feature_images
16
+ # SHOULD BE community.units.projects.features.room
17
+ def include_relationships(serializer, parent_key: nil)
18
+ include = []
19
+
20
+ serializer.relationships_to_serialize&.each do |k, v|
21
+ child_serializer = v.serializer.to_s.safe_constantize
22
+ next if child_serializer.nil? ||
23
+ included?(serializer.record_type, child_serializer.record_type) ||
24
+ parent_key.to_s.include?("#{k}.") ||
25
+ parent_key.to_s.include?("#{k.to_s.pluralize}.")
26
+
27
+ child_key = parent_key ? "#{parent_key}.#{k}".to_sym : k
28
+ include << child_key
29
+
30
+ @included[serializer.record_type] << child_serializer.record_type
31
+
32
+ next unless @recursive
33
+ include << include_relationships(child_serializer, parent_key: child_key)
34
+ end
35
+
36
+ include.flatten.compact
37
+ end
38
+
39
+ def included?(parent, child)
40
+ return true if parent == child
41
+
42
+ @included[parent] ||= []
43
+ @included[child] ||= []
44
+
45
+ @included[parent].include?(child)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module JsonapiActions
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi_actions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Pheasey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '6.0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '4.0'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '6.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: kaminari
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '1.0'
82
+ - - "<"
83
+ - !ruby/object:Gem::Version
84
+ version: '2.0'
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '1.0'
92
+ - - "<"
93
+ - !ruby/object:Gem::Version
94
+ version: '2.0'
95
+ description: Implement Rails JSONAPI compliant controller actions.
96
+ email:
97
+ - kevin@kpsoftware.io
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - ".circleci/config.yml"
103
+ - ".gitignore"
104
+ - ".rspec"
105
+ - ".ruby-gemset"
106
+ - ".ruby-version"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - jsonapi_actions.gemspec
116
+ - lib/jsonapi_actions.rb
117
+ - lib/jsonapi_actions/authorization.rb
118
+ - lib/jsonapi_actions/eager_loader.rb
119
+ - lib/jsonapi_actions/error_handling.rb
120
+ - lib/jsonapi_actions/inclusion_mapper.rb
121
+ - lib/jsonapi_actions/version.rb
122
+ homepage: https://github.com/kp-software/jsonapi_actions
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.7.8
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Rails JSONAPI Controller Actions
146
+ test_files: []