Skip to content

Commit

Permalink
Override resource generator to work with our structure better (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
NuckChorris committed Feb 1, 2017
1 parent f5840ec commit 9b450e7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/application.rb
Expand Up @@ -72,5 +72,12 @@ class Application < Rails::Application

# Set ActiveJob adapter
config.active_job.queue_adapter = :sidekiq

# Configure Scaffold Generators
config.generators do |g|
g.authorization :policy
g.serialization :jsonapi_resource
g.resource_controller :resource_controller
end
end
end
@@ -0,0 +1,15 @@
require 'rails/generators/rails/resource_route/resource_route_generator'

module Rails
module Generators
class ApiResourceRouteGenerator < ResourceRouteGenerator
def add_resource_route
return if options[:actions].present?
route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ")
route_config << "jsonapi_resources :#{file_name.pluralize}"
route_config << " end" * regular_class_path.size
route route_config
end
end
end
end
@@ -0,0 +1,16 @@
module Rails
module Generators
class JsonapiResourceGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)

def create_resource
template_file = File.join(
'app/resources',
class_path,
"#{file_name.singularize}_resource.rb"
)
template 'resource.rb', template_file
end
end
end
end
4 changes: 4 additions & 0 deletions lib/generators/rails/jsonapi_resource/templates/resource.rb
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class <%= class_name.singularize %>Resource < JSONAPI::Resource
end
<% end -%>
18 changes: 18 additions & 0 deletions lib/generators/rails/policy/policy_generator.rb
@@ -0,0 +1,18 @@
module Rails
module Generators
class PolicyGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)

def create_policy
template_file = File.join(
'app/policies',
class_path,
"#{file_name.singularize}_policy.rb"
)
template 'policy.rb', template_file
end

hook_for :test_framework
end
end
end
4 changes: 4 additions & 0 deletions lib/generators/rails/policy/templates/policy.rb
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class <%= class_name %>Policy < ApplicationPolicy
end
<% end -%>
@@ -0,0 +1,22 @@
require 'generators/jsonapi/controller_generator'

module Rails
module Generators
class ResourceControllerGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
check_class_collision suffix: 'Controller'

def create_controller
template_file = File.join(
'app/controllers',
class_path,
"#{file_name.pluralize}_controller.rb"
)
template 'jsonapi_controller.rb', template_file
end

hook_for :authorization
hook_for :serialization
end
end
end
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class <%= class_name.pluralize %>Controller < ApplicationController
end
<% end -%>

0 comments on commit 9b450e7

Please sign in to comment.