Skip to content

Add namespace option to resource generator for controllers #53

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

Open
wants to merge 3 commits into
base: main
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
46 changes: 42 additions & 4 deletions lib/generators/graphiti/resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Graphiti
class ResourceGenerator < ::Rails::Generators::NamedBase
NAMESPACE_ROUTE_REGEX = /ApplicationResource.*^\s*[^\n#]*\sas:\s'api'\sdo/m

include GeneratorMixin

source_root File.expand_path("../templates", __FILE__)
Expand Down Expand Up @@ -32,6 +34,12 @@ class ResourceGenerator < ::Rails::Generators::NamedBase
aliases: ["--model", "-m"],
desc: "Specify to use attributes from a particular model"

class_option :'namespace-controllers',
type: :boolean,
default: false,
aliases: ["--namespace"],
desc: "Generate controllers in a namespace specified in .graphiticfg.yml"

desc "This generator creates a resource file at app/resources, as well as corresponding controller/specs/route/etc"
def generate_all
generate_model
Expand Down Expand Up @@ -70,6 +78,10 @@ def omit_comments?
@options["omit-comments"]
end

def generate_namespace_controllers?
@options["namespace-controllers"]
end

def attributes_class
return @attributes_class if @attributes_class

Expand Down Expand Up @@ -126,7 +138,8 @@ def responders?
end

def generate_controller
to = File.join("app/controllers", class_path, "#{file_name.pluralize}_controller.rb")
namespace_path = generate_namespace_controllers? ? api_namespace : class_path
to = File.join("app/controllers", namespace_path, "#{file_name.pluralize}_controller.rb")
template("controller.rb.erb", to)
end

Expand All @@ -140,12 +153,31 @@ def application_resource_defined?
"ApplicationResource".safe_constantize.present?
end

def namespace_resource_routes_exist?
File.read('config/routes.rb').scan(NAMESPACE_ROUTE_REGEX).any?
end

def add_namespace_resource_routes
inject_into_file "config/routes.rb", after: /ApplicationResource.*$\n/ do
indent("scope module: '#{api_namespace[1..-1]}', as: 'api' do\nend\n", 4)
end
end

def generate_route
code = "resources :#{file_name.pluralize}"
if generate_namespace_controllers?
add_namespace_resource_routes unless namespace_resource_routes_exist?
resource_routes_regex = NAMESPACE_ROUTE_REGEX
indent = 6
else
resource_routes_regex = /ApplicationResource.*$\n/
indent = 4
end

code = "\nresources :#{file_name.pluralize}"
code << %(, only: [#{actions.map { |a| ":#{a}" }.join(", ")}]) if actions.length < 5
code << "\n"
inject_into_file "config/routes.rb", after: /ApplicationResource.*$\n/ do
indent(code, 4)
inject_into_file "config/routes.rb", after: resource_routes_regex do
indent(code, indent)
end
end

Expand Down Expand Up @@ -173,6 +205,12 @@ def create?
behavior == :invoke
end

def controller_klass
generate_namespace_controllers? ?
"#{api_namespace[1..-1].camelize}::#{model_klass.name.pluralize}Controller" :
"#{model_klass.name.pluralize}Controller"
end

def model_klass
class_name.safe_constantize
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/graphiti/templates/controller.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% module_namespacing do -%>
class <%= model_klass.name.pluralize %>Controller < ApplicationController
class <%= controller_klass %> < ApplicationController
<%- if actions?('index') -%>
def index
<%= file_name.pluralize %> = <%= resource_klass %>.all(params)
Expand Down