Commit 27801754 by liyijie

Refactor simple controller with resource_plural & resource_singular

parent 9487c0bf
...@@ -21,7 +21,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -21,7 +21,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
def copy_view_files def copy_view_files
%w(index show _single _simple _detail).each do |view| %w(index show _single _simple _detail).each do |view|
filename = filename_with_extensions(view) filename = filename_with_extensions(view)
template "views/#{filename}", File.join('app/views', view_class_path, filename) template "views/#{filename}", File.join('app/views', view_path, filename)
end end
end end
...@@ -33,7 +33,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -33,7 +33,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
protected protected
def view_class_path def view_path
return options.view if options.view.present? return options.view if options.view.present?
if controller_class_path.size > 1 if controller_class_path.size > 1
File.join controller_class_path[0], plural_name File.join controller_class_path[0], plural_name
...@@ -103,6 +103,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -103,6 +103,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
@resource_class @resource_class
end end
def resource_plural
resource_class&.model_name&.plural
end
def resource_singular
resource_class&.model_name&.singular
end
def attributes_names def attributes_names
begin begin
resource_class.columns.map(&:name) - %w(id created_at updated_at) resource_class.columns.map(&:name) - %w(id created_at updated_at)
......
class <%= controller_class_name %>Controller < SimpleController::BaseController class <%= controller_class_name %>Controller < SimpleController::BaseController
set_view_path '<%= view_class_path %>' defaults(
<% if auth.present? %>acts_as_auth_action :<%= auth.downcase %><% end %> resource_class: <%= resource_class %>,
collection_name: '<%= resource_plural %>',
instance_name: '<%= resource_singular %>',
view_path: '<%= view_path %>'
)
<% if auth.present? %>acts_as_auth_action :<%= auth.downcase %><% end -%>
private private
def <%= singular_name %>_params def <%= resource_singular %>_params
params.require(:<%= singular_name %>).permit( params.require(:<%= resource_singular %>).permit(
<%= attributes_list(attributes_names) %> <%= attributes_list(attributes_names) %>
) )
end end
......
require 'swagger_helper' require 'swagger_helper'
<%= singular_name.upcase %>_REF = { <%= resource_singular.upcase %>_REF = {
type: :object, properties: { type: :object, properties: {
<%= singular_name %>: { <%= resource_singular %>: {
type: :object, properties: { type: :object, properties: {
<% if resource_class&.columns_hash.present? -%> <% if resource_class&.columns_hash.present? -%>
<% resource_class.columns_hash.except('id', 'created_at', 'updated_at').values.each do |column| -%> <% resource_class.columns_hash.except('id', 'created_at', 'updated_at').values.each do |column| -%>
...@@ -14,7 +14,8 @@ require 'swagger_helper' ...@@ -14,7 +14,8 @@ require 'swagger_helper'
} }
} }
<%= singular_name.upcase %>_VALUE = FactoryBot.attributes_for(:<%= resource_class.to_s.underscore.sub('/', '_') %>) <%= resource_singular.upcase %>_VALUE = FactoryBot.attributes_for(:<%=
resource_singular %>)
RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, tags: ["<%= controller_class_path.join(' ') %>"] do RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, tags: ["<%= controller_class_path.join(' ') %>"] do
before :each do before :each do
...@@ -22,7 +23,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, ...@@ -22,7 +23,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
@auth = <%= auth %>.register "auth", "password" @auth = <%= auth %>.register "auth", "password"
@auth_token = <%= auth %>.login "auth", "password" @auth_token = <%= auth %>.login "auth", "password"
<% end -%> <% end -%>
@<%= plural_name %> = FactoryBot.create_list(:<%= resource_class.to_s.underscore.sub('/', '_') %>, 5) @<%= resource_plural %> = FactoryBot.create_list(:<%= resource_singular %>, 5)
end end
<% @routes.each do | template, path_item | -%> <% @routes.each do | template, path_item | -%>
...@@ -36,7 +37,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, ...@@ -36,7 +37,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
parameter '<%= param %>', in: :path, type: :string parameter '<%= param %>', in: :path, type: :string
<% end -%> <% end -%>
<% path_item[:params].each do |param| -%> <% path_item[:params].each do |param| -%>
let(:<%= param %>) { @<%= plural_name %>.first.id } let(:<%= param %>) { @<%= resource_plural %>.first.id }
<% end -%> <% end -%>
<% end -%> <% end -%>
...@@ -47,10 +48,10 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, ...@@ -47,10 +48,10 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
consumes 'application/json' consumes 'application/json'
<% if ['post', 'patch'].include? action -%> <% if ['post', 'patch'].include? action -%>
parameter :<%= singular_name %>, in: :body, schema: <%= parameter :<%= resource_singular %>, in: :body, schema: <%=
singular_name.upcase %>_REF resource_singular.upcase %>_REF
let(:<%= singular_name %>) do let(:<%= resource_singular %>) do
{ <%= singular_name %>: <%= singular_name.upcase %>_VALUE } { <%= resource_singular %>: <%= resource_singular.upcase %>_VALUE }
end end
<% end -%> <% end -%>
response(<%= response_status action %>, description: 'successful') do response(<%= response_status action %>, description: 'successful') do
......
json.partial! "<%= view_class_path %>/single", <%= singular_name %>: <%= singular_name %> json.partial! "<%= view_path %>/single", <%= resource_singular %>: <%= resource_singular %>
json.partial! "<%= view_class_path %>/single", <%= singular_name %>: <%= singular_name %> json.partial! "<%= view_path %>/single", <%= resource_singular %>: <%= resource_singular %>
json.extract! <%= singular_name %>, <%= attributes_list_with_timestamps %> json.extract! <%= resource_singular %>, <%= attributes_list_with_timestamps %>
json.current_page @<%= plural_name %>.current_page json.current_page @<%= resource_plural %>.current_page
json.total_page @<%= plural_name %>.total_pages json.total_page @<%= resource_plural %>.total_pages
json.<%= plural_name %> @<%= plural_name %>, partial: '<%= view_class_path %>/simple', as: :<%= singular_name %> json.<%= resource_plural %> @<%= resource_plural %>, partial: '<%= view_path %>/simple', as: :<%= resource_singular %>
json.partial! "<%= view_class_path %>/detail", <%= singular_name %>: @<%= singular_name %> json.partial! "<%= view_path %>/detail", <%= resource_singular %>: @<%= resource_singular %>
...@@ -15,12 +15,19 @@ class SimpleController::BaseController < ::InheritedResources::Base ...@@ -15,12 +15,19 @@ class SimpleController::BaseController < ::InheritedResources::Base
protected protected
def self.set_view_path path class << self
@view_path = path def view_path
@view_path
end end
def self.view_path def defaults(options)
@view_path set_view_path options.delete(:view_path)
super(options)
end
def set_view_path path
@view_path = path
end
end end
def view_path def view_path
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment