Skip to content

Commit 8a9728f

Browse files
author
Lee Richmond
committed
Add simple generator
1 parent 34360cd commit 8a9728f

11 files changed

+257
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 -%>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
JsonapiSpecHelpers::Payload.register(:<%= file_name %>) do
2+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 -%>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% module_namespacing do -%>
2+
class Serializable<%= class_name %> < JSONAPI::Serializable::Resource
3+
type :<%= type %>
4+
end
5+
<% end -%>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)