jsonapi_compliable 0.6.9 → 0.6.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f1eac7962a18b73dc60232ec1607f09b7f45eed
4
- data.tar.gz: f7707bda77c712fdf7a7b1b34169156399c76a52
3
+ metadata.gz: 6a7102c3603b10fd5d326a2a28ecd9dffa56cc3a
4
+ data.tar.gz: fc0590f0cf33a1683708fa771673416e06f44b98
5
5
  SHA512:
6
- metadata.gz: e0d21c56a530c7a9fe811b2f892e78b6f6a7140af521ea32fc70b654d62f6ba1c9c401b53e0c2d4b089a355adcdae7e7df1790f5ce79b14dcaff066db5eeea84
7
- data.tar.gz: a560bd12cecdc55c81f909e7dd908aeb501d51fdc8fd72ef16935141d0ae8847d7a4fcb366510a6290c6a90ecceb81a18bfa4fd8696866858c8604bd7ed4e89f
6
+ metadata.gz: 32a8770105144995432463bb6b747243d607aef3b6ae6ba94bd9cca22be046e69ce1a909ee8f1643fb6d15215698da19ba68173120e1f2c1f9394ea4629a4bd5
7
+ data.tar.gz: 52b59fc2e047a1eeaf29c87c322739b930f0460b85008e111710186b15145b563caf3b31572f640b5439dbb80ae31192b6d5d2133eb947075e191bfc7e8b694f
@@ -0,0 +1,96 @@
1
+ module Jsonapi
2
+ class ResourceGenerator < ::Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ class_option :'no-controller', type: :boolean, default: false
6
+ class_option :'no-serializer', type: :boolean, default: false
7
+ class_option :'no-payload', type: :boolean, default: false
8
+ class_option :'no-strong-resources', type: :boolean, default: false
9
+ class_option :'no-test', type: :boolean, default: false
10
+
11
+ desc "This generator creates a resource file at app/resources"
12
+ def copy_resource_file
13
+ unless @options['no-controller']
14
+ to = File.join('app/controllers', class_path, "#{file_name.pluralize}_controller.rb")
15
+ template('controller.rb.erb', to)
16
+ end
17
+
18
+ unless @options['no-serializer']
19
+ to = File.join('app/serializers', class_path, "serializable_#{file_name}.rb")
20
+ template('serializer.rb.erb', to)
21
+ end
22
+
23
+ unless 'ApplicationResource'.safe_constantize
24
+ to = File.join('app/resources', class_path, "application_resource.rb")
25
+ template('application_resource.rb.erb', to)
26
+ end
27
+
28
+ unless @options['no-payload']
29
+ to = File.join('spec/payloads', class_path, "#{file_name}.rb")
30
+ template('payload.rb.erb', to)
31
+ end
32
+
33
+ unless @options['no-strong-resources']
34
+ inject_into_file 'config/initializers/strong_resources.rb', after: "StrongResources.configure do\n" do <<-STR
35
+ strong_resource :#{file_name} do
36
+ # Your attributes go here, e.g.
37
+ # attribute :name, :string
38
+ end
39
+
40
+ STR
41
+ end
42
+ end
43
+
44
+ unless @options['no-route']
45
+ inject_into_file 'config/routes.rb', after: "scope '/api' do\n scope '/v1' do\n" do <<-STR
46
+ resources :#{type}
47
+ STR
48
+ end
49
+ end
50
+
51
+ unless @options['no-test']
52
+ to = File.join "spec/api/v1/#{file_name.pluralize}",
53
+ class_path,
54
+ "index_spec.rb"
55
+ template('index_request_spec.rb.erb', to)
56
+
57
+ to = File.join "spec/api/v1/#{file_name.pluralize}",
58
+ class_path,
59
+ "show_spec.rb"
60
+ template('show_request_spec.rb.erb', to)
61
+
62
+ to = File.join "spec/api/v1/#{file_name.pluralize}",
63
+ class_path,
64
+ "create_spec.rb"
65
+ template('create_request_spec.rb.erb', to)
66
+
67
+ to = File.join "spec/api/v1/#{file_name.pluralize}",
68
+ class_path,
69
+ "update_spec.rb"
70
+ template('update_request_spec.rb.erb', to)
71
+
72
+ to = File.join "spec/api/v1/#{file_name.pluralize}",
73
+ class_path,
74
+ "destroy_spec.rb"
75
+ template('destroy_request_spec.rb.erb', to)
76
+ end
77
+
78
+ to = File.join('app/resources', class_path, "#{file_name}_resource.rb")
79
+ template('resource.rb.erb', to)
80
+ end
81
+
82
+ def do_thing
83
+ puts "DO THING"
84
+ end
85
+
86
+ private
87
+
88
+ def model_klass
89
+ class_name.safe_constantize
90
+ end
91
+
92
+ def type
93
+ model_klass.name.underscore.pluralize
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ require 'jsonapi_compliable/adapters/active_record'
2
+
3
+ class ApplicationResource < JsonapiCompliable::Resource
4
+ use_adapter JsonapiCompliable::Adapters::ActiveRecord
5
+ end
@@ -0,0 +1,45 @@
1
+ <% module_namespacing do -%>
2
+ class <%= model_klass.name.pluralize %>Controller < ApplicationController
3
+ jsonapi resource: <%= model_klass %>Resource
4
+
5
+ strong_resource :<%= file_name %>
6
+
7
+ before_action :apply_strong_params, only: [:create, :update]
8
+
9
+ def index
10
+ <%= file_name.pluralize %> = <%= model_klass %>.all
11
+ render_jsonapi(<%= file_name.pluralize %>)
12
+ end
13
+
14
+ def show
15
+ scope = jsonapi_scope(<%= model_klass %>.where(id: params[:id]))
16
+ render_jsonapi(scope.resolve.first, scope: false)
17
+ end
18
+
19
+ def create
20
+ <%= file_name %>, success = jsonapi_create.to_a
21
+
22
+ if success
23
+ render_jsonapi(<%= file_name %>, scope: false)
24
+ else
25
+ render_errors_for(<%= file_name %>)
26
+ end
27
+ end
28
+
29
+ def update
30
+ <%= file_name %>, success = jsonapi_update.to_a
31
+
32
+ if success
33
+ render_jsonapi(<%= file_name %>, scope: false)
34
+ else
35
+ render_errors_for(<%= file_name %>)
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ <%= file_name %> = <%= model_klass %>.find(params[:id])
41
+ <%= file_name %>.destroy
42
+ return head(:no_content)
43
+ end
44
+ end
45
+ <% end -%>
@@ -0,0 +1,25 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= type %>#create", type: :request do
4
+ context 'basic create' do
5
+ let(:payload) do
6
+ {
7
+ data: {
8
+ type: '<%= type %>',
9
+ attributes: {
10
+ # ... your attrs here
11
+ }
12
+ }
13
+ }
14
+ end
15
+
16
+ it 'creates the resource' do
17
+ expect {
18
+ jsonapi_post "/api/v1/<%= type %>", payload
19
+ }.to change { <%= model_klass %>.count }.by(1)
20
+ <%= file_name %> = <%= model_klass %>.last
21
+
22
+ assert_payload(:<%= file_name %>, <%= file_name %>, json_item)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= type %>#destroy", type: :request do
4
+ context 'basic destroy' do
5
+ let!(:<%= file_name %>) { create(:<%= file_name %>) }
6
+
7
+ it 'updates the resource' do
8
+ expect {
9
+ delete "/api/v1/<%= type %>/#{<%= file_name %>.id}"
10
+ }.to change { <%= model_klass %>.count }.by(-1)
11
+
12
+ expect(response.status).to eq(204)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= file_name.pluralize %>#index", type: :request do
4
+ context 'basic fetch' do
5
+ let!(:<%= file_name %>1) { create(:<%= file_name %>) }
6
+ let!(:<%= file_name %>2) { create(:<%= file_name %>) }
7
+
8
+ it 'serializes the list correctly' do
9
+ get "/api/v1/<%= file_name.pluralize %>"
10
+
11
+ expect(json_ids(true)).to match_array([<%= file_name %>1.id, <%= file_name %>2.id])
12
+ assert_payload(:<%= file_name %>, <%= file_name %>1, json_items[0])
13
+ assert_payload(:<%= file_name %>, <%= file_name %>2, json_items[1])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ JsonapiSpecHelpers::Payload.register(:<%= file_name %>) do
2
+ end
@@ -0,0 +1,6 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Resource < ApplicationResource
3
+ type :<%= type %>
4
+ model <%= model_klass %>
5
+ end
6
+ <% end -%>
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>
2
+ class Serializable<%= class_name %> < JSONAPI::Serializable::Resource
3
+ type :<%= type %>
4
+ end
5
+ <% end -%>
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= file_name.pluralize %>#show", type: :request do
4
+ context 'basic fetch' do
5
+ let!(:<%= file_name %>) { create(:<%= file_name %>) }
6
+
7
+ it 'serializes the resource correctly' do
8
+ get "/api/v1/<%= file_name.pluralize %>/#{<%= file_name %>.id}"
9
+
10
+ assert_payload(:<%= file_name %>, <%= file_name %>, json_item)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= type %>#update", type: :request do
4
+ context 'basic update' do
5
+ let!(:<%= file_name %>) { create(:<%= file_name %>) }
6
+
7
+ let(:payload) do
8
+ {
9
+ data: {
10
+ id: <%= file_name %>.id.to_s,
11
+ type: '<%= type %>',
12
+ attributes: {
13
+ # ... your attrs here
14
+ }
15
+ }
16
+ }
17
+ end
18
+
19
+ # Replace 'xit' with 'it' after adding attributes
20
+ xit 'updates the resource' do
21
+ expect {
22
+ jsonapi_put "/api/v1/<%= type %>/#{<%= file_name %>.id}", payload
23
+ }.to change { <%= file_name %>.reload.attributes }
24
+ assert_payload(:<%= file_name %>, <%= file_name %>, json_item)
25
+
26
+ # ... assert updates attributes ...
27
+ end
28
+ end
29
+ end
@@ -22,7 +22,7 @@ module JsonapiCompliable
22
22
  # (see Adapters::Abstract#count)
23
23
  def count(scope, attr)
24
24
  column = attr == :total ? :all : attr
25
- scope.uniq.count(column)
25
+ scope.distinct.count(column)
26
26
  end
27
27
 
28
28
  # (see Adapters::Abstract#average)
@@ -1,3 +1,3 @@
1
1
  module JsonapiCompliable
2
- VERSION = "0.6.9"
2
+ VERSION = "0.6.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_compliable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.9
4
+ version: 0.6.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-05-18 00:00:00.000000000 Z
12
+ date: 2017-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jsonapi-serializable
@@ -199,6 +199,17 @@ files:
199
199
  - gemfiles/rails_4.gemfile
200
200
  - gemfiles/rails_5.gemfile
201
201
  - jsonapi_compliable.gemspec
202
+ - lib/generators/jsonapi/resource_generator.rb
203
+ - lib/generators/jsonapi/templates/application_resource.rb.erb
204
+ - lib/generators/jsonapi/templates/controller.rb.erb
205
+ - lib/generators/jsonapi/templates/create_request_spec.rb.erb
206
+ - lib/generators/jsonapi/templates/destroy_request_spec.rb.erb
207
+ - lib/generators/jsonapi/templates/index_request_spec.rb.erb
208
+ - lib/generators/jsonapi/templates/payload.rb.erb
209
+ - lib/generators/jsonapi/templates/resource.rb.erb
210
+ - lib/generators/jsonapi/templates/serializer.rb.erb
211
+ - lib/generators/jsonapi/templates/show_request_spec.rb.erb
212
+ - lib/generators/jsonapi/templates/update_request_spec.rb.erb
202
213
  - lib/jsonapi_compliable.rb
203
214
  - lib/jsonapi_compliable/adapters/abstract.rb
204
215
  - lib/jsonapi_compliable/adapters/active_record.rb