Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scaffold generator #1266

Open
wants to merge 1 commit into
base: release-0-9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.config
.yardoc
.ruby-version
.tool-versions
.idea
Gemfile.lock
InstalledFiles
_yardoc
Expand All @@ -21,3 +23,4 @@ coverage
test/log
test_db
test_db-journal
vendor
28 changes: 28 additions & 0 deletions lib/generators/jsonapi/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,31 @@ Examples:

This will create:
app/controllers/posts_controller.rb

rails generate jsonapi:scaffold PoolParty

This will create:
app/models/pool_party.rb
app/resources/application_resource.rb
app/resources/pool_party_resource.rb
app/controller/pool_parties_controller.rb
app/config/routes/pool_parties.rb

This will modify:
app/controller/application_controller.rb
config/routes.rb

rails generate jsonapi:scaffold Api::V1::PoolParty

This will create:
app/models/api/v1/application_record.rb
app/models/api/v1/pool_party.rb
app/resources/api/v1/application_resource.rb
app/resources/api/v1/pool_party_resource.rb
app/controller/api/v1/application_controller.rb
app/controller/api/v1/pool_parties_controller.rb
app/config/routes/api/v1/pool_parties.rb

This will modify:
app/controller/application_controller.rb
config/routes.rb
2 changes: 1 addition & 1 deletion lib/generators/jsonapi/controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Jsonapi
class ControllerGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def create_resource
def create_controller
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] Is the intention for these generators to refer to "resource" more generally, i.e. any kind of MVC element, or was it referring to a "resource" in the sense of the resource classes created and relied on by this library?

template_file = File.join(
'app/controllers',
class_path,
Expand Down
120 changes: 120 additions & 0 deletions lib/generators/jsonapi/scaffold_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module Jsonapi
Copy link
Author

@jamesconant jamesconant Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] In an effort to keep this simple, I've not broken the generators out in to sub-generators in called them from one meta-generator method. I'm not opposed to breaking them out if folks feel strongly about that.

class ScaffoldGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)

def copy_application_record_file
if namespace_path.present?
@application_record_class = application_record_class
path = namespaced_file_path(models_path, 'application_record.rb')
template 'application_record.erb', path
Copy link
Author

@jamesconant jamesconant Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] I noticed in the generators that already exist here that file paths for templates are ran through File, and I was wondering if that was a best practice that I was not aware of, or what the utility in doing so is? Would there not be an error with or without its use that points out that the file does not exist?

end
end

def copy_model_file
@application_record_class = application_record_class
@class_name = full_namespace_class(file_name.camelize)
path = namespaced_file_path(models_path, file_name + '.rb')
template 'model.erb', path
end

def copy_application_resource_file
@application_resource_class = application_resource_class
path = namespaced_file_path(resources_path, 'application_resource.rb')
template 'application_resource.erb', path
end

def copy_resource_file
@application_resource_class = application_resource_class
@class_name = full_namespace_class(file_name.camelize + 'Resource')
path = namespaced_file_path(resources_path, file_name + '_resource.rb')
template 'resource.erb', path
end

def copy_application_controller_file
path = namespaced_file_path(controllers_path, 'application_controller.rb')
@application_controller_class = application_controller_class

if namespace_path.present?
template 'application_controller.erb', path
end

inject_into_class(path, 'ApplicationController', 'include JSONAPI::ActsAsResourceController')
end

def copy_controller_file
@application_controller_class = application_controller_class
@class_name = full_namespace_class(file_name.camelize.pluralize + 'Controller')
path = namespaced_file_path(controllers_path, file_name.pluralize + '_controller.rb')
template 'controller.erb', path
end

def copy_route_file
dynamic_routes_method = <<~ROUTE
def draw_resource_route(route_name)
instance_eval(File.read(Rails.root.join("config/routes/\#{route_name}.rb")))
end\n
ROUTE

prepend_to_file 'config/routes.rb', dynamic_routes_method

@route_content = recursive_routes(class_path.dup)

route "draw_resource_route '#{file_path.pluralize}'"

template "route.erb", namespaced_file_path('config/routes/', file_name.pluralize + '.rb')
end

private

def models_path
'app/models/'
end

def resources_path
'app/resources/'
end

def controllers_path
'app/controllers/'
end

def namespace_path
class_path.map { |cp| cp + '/' }.join
end

def namespaced_file_path(prefix, file)
prefix + namespace_path + file
end

def namespace_class
class_path.map(&:camelize).map { |cp| cp + '::' }.join
end

def full_namespace_class(class_suffix)
namespace_class + class_suffix
end

def application_record_class
full_namespace_class('ApplicationRecord')
end

def application_resource_class
full_namespace_class('ApplicationResource')
end

def application_controller_class
full_namespace_class('ApplicationController')
end

def recursive_routes(path_elements, tab_count = 1)
if path_elements.count == 0
"jsonapi_resources :#{plural_name}"
else
namespace = path_elements.shift
start_tabs = "\t" * tab_count
end_tabs = "\t" * (tab_count - 1)
"namespace :#{namespace} do\n#{start_tabs}#{recursive_routes(path_elements, tab_count + 1)}\n#{end_tabs}end"
end
end
end
end
2 changes: 2 additions & 0 deletions lib/generators/jsonapi/templates/application_controller.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class <%= @application_controller_class %> < ApplicationController
end
3 changes: 3 additions & 0 deletions lib/generators/jsonapi/templates/application_record.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class <%= @application_record_class %> < ApplicationRecord
self.abstract_class = true
end
2 changes: 2 additions & 0 deletions lib/generators/jsonapi/templates/application_resource.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class <%= @application_resource_class %> < JSONAPI::Resource
end
4 changes: 4 additions & 0 deletions lib/generators/jsonapi/templates/controller.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class <%= class_name.pluralize %>Controller < <%= @application_controller_class %>
end
<% end -%>
4 changes: 4 additions & 0 deletions lib/generators/jsonapi/templates/model.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class <%= class_name %> < <%= @application_record_class %>
end
<% end -%>
5 changes: 5 additions & 0 deletions lib/generators/jsonapi/templates/resource.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% module_namespacing do -%>
class <%= class_name %>Resource < <%= @application_resource_class %>
model_name self.to_s.gsub('Resource', '')
end
<% end -%>
1 change: 1 addition & 0 deletions lib/generators/jsonapi/templates/route.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @route_content %>