Commit 27801754 by liyijie

Refactor simple controller with resource_plural & resource_singular

parent 9487c0bf
......@@ -21,7 +21,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
def copy_view_files
%w(index show _single _simple _detail).each do |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
......@@ -33,7 +33,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
protected
def view_class_path
def view_path
return options.view if options.view.present?
if controller_class_path.size > 1
File.join controller_class_path[0], plural_name
......@@ -103,6 +103,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
@resource_class
end
def resource_plural
resource_class&.model_name&.plural
end
def resource_singular
resource_class&.model_name&.singular
end
def attributes_names
begin
resource_class.columns.map(&:name) - %w(id created_at updated_at)
......
class <%= controller_class_name %>Controller < SimpleController::BaseController
set_view_path '<%= view_class_path %>'
<% if auth.present? %>acts_as_auth_action :<%= auth.downcase %><% end %>
defaults(
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
def <%= singular_name %>_params
params.require(:<%= singular_name %>).permit(
def <%= resource_singular %>_params
params.require(:<%= resource_singular %>).permit(
<%= attributes_list(attributes_names) %>
)
end
......
require 'swagger_helper'
<%= singular_name.upcase %>_REF = {
<%= resource_singular.upcase %>_REF = {
type: :object, properties: {
<%= singular_name %>: {
<%= resource_singular %>: {
type: :object, properties: {
<% if resource_class&.columns_hash.present? -%>
<% resource_class.columns_hash.except('id', 'created_at', 'updated_at').values.each do |column| -%>
......@@ -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
before :each do
......@@ -22,7 +23,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
@auth = <%= auth %>.register "auth", "password"
@auth_token = <%= auth %>.login "auth", "password"
<% end -%>
@<%= plural_name %> = FactoryBot.create_list(:<%= resource_class.to_s.underscore.sub('/', '_') %>, 5)
@<%= resource_plural %> = FactoryBot.create_list(:<%= resource_singular %>, 5)
end
<% @routes.each do | template, path_item | -%>
......@@ -36,7 +37,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
parameter '<%= param %>', in: :path, type: :string
<% end -%>
<% path_item[:params].each do |param| -%>
let(:<%= param %>) { @<%= plural_name %>.first.id }
let(:<%= param %>) { @<%= resource_plural %>.first.id }
<% end -%>
<% end -%>
......@@ -47,10 +48,10 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
consumes 'application/json'
<% if ['post', 'patch'].include? action -%>
parameter :<%= singular_name %>, in: :body, schema: <%=
singular_name.upcase %>_REF
let(:<%= singular_name %>) do
{ <%= singular_name %>: <%= singular_name.upcase %>_VALUE }
parameter :<%= resource_singular %>, in: :body, schema: <%=
resource_singular.upcase %>_REF
let(:<%= resource_singular %>) do
{ <%= resource_singular %>: <%= resource_singular.upcase %>_VALUE }
end
<% end -%>
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.total_page @<%= plural_name %>.total_pages
json.current_page @<%= resource_plural %>.current_page
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
protected
def self.set_view_path path
@view_path = path
end
class << self
def view_path
@view_path
end
def self.view_path
@view_path
def defaults(options)
set_view_path options.delete(:view_path)
super(options)
end
def set_view_path path
@view_path = path
end
end
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