-
Notifications
You must be signed in to change notification settings - Fork 535
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
base: release-0-9
Are you sure you want to change the base?
Add scaffold generator #1266
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
module Jsonapi | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class <%= @application_controller_class %> < ApplicationController | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class <%= @application_record_class %> < ApplicationRecord | ||
self.abstract_class = true | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class <%= @application_resource_class %> < JSONAPI::Resource | ||
end |
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 -%> |
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 -%> |
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 -%> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= @route_content %> |
There was a problem hiding this comment.
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?