Commit 60bb4ac5 by liyijie

feat: support detail attribute for detail view

parent 9a13c9f4
class SimpleControllerGenerator < Rails::Generators::NamedBase class SimpleControllerGenerator < Rails::Generators::NamedBase
include Rails::Generators::ResourceHelpers include Rails::Generators::ResourceHelpers
source_root File.expand_path('../templates', __FILE__) source_root File.expand_path('templates', __dir__)
class_option :view, type: :string, desc: "View files generate folder" class_option :view, type: :string, desc: 'View files generate folder'
class_option :model, type: :string, desc: "Model name for extract attributes" class_option :model, type: :string, desc: 'Model name for extract attributes'
class_option :resource, type: :string, desc: "Resource name for var name(plural or singular)" class_option :resource, type: :string, desc: 'Resource name for var name(plural or singular)'
class_option :auth, type: :string, desc: "Authentication model name" class_option :auth, type: :string, desc: 'Authentication model name'
class_option 'auth-only', type: :boolean, desc: "Only generate authentication" class_option 'auth-only', type: :boolean, desc: 'Only generate authentication'
class_option 'no-swagger', type: :boolean, desc: "Do not generate swagger spec file" class_option 'no-swagger', type: :boolean, desc: 'Do not generate swagger spec file'
class_option 'no-view', type: :boolean, desc: "Do not generate views file" class_option 'no-view', type: :boolean, desc: 'Do not generate views file'
def setup def setup
return if options["auth-only"] return if options['auth-only']
@routes = RSpec::Rails::Swagger::RouteParser.new(controller_path.sub(/^\//, '')).routes
p "Warning!! Resource is not exist, CHECK & regenerate after you have configurate the model and routes already" if resource_class.blank? @routes = RSpec::Rails::Swagger::RouteParser.new(controller_path.sub(%r{^/}, '')).routes
p 'Warning!! Resource is not exist, CHECK & regenerate after you have configurate the model and routes already' if resource_class.blank?
end end
def create_controller_files def create_controller_files
if options["auth-only"] template_file = if options['auth-only']
template_file = "controllers/auth_controller.rb" 'controllers/auth_controller.rb'
else else
template_file = "controllers/controller.rb" 'controllers/controller.rb'
end end
template template_file, File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb") template template_file, File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
end end
def copy_view_files def copy_view_files
return if options["auth-only"] || options["no-view"] return if options['auth-only'] || options['no-view']
%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_path, filename) template "views/#{filename}", File.join('app/views', view_path, filename)
end end
end end
def create_swagger_files def create_swagger_files
return if options["no-swagger"] return if options['no-swagger']
if options["auth-only"]
template_file = "specs/auth_spec.rb" template_file = if options['auth-only']
'specs/auth_spec.rb'
else else
template_file = "specs/spec.rb" 'specs/spec.rb'
end end
template template_file, File.join("spec/requests", controller_class_path, "#{controller_file_name}_spec.rb") template template_file, File.join('spec/requests', controller_class_path, "#{controller_file_name}_spec.rb")
end end
protected protected
def view_path def view_path
return options.view if options.view.present? return options.view if options.view.present?
if resource_collection.present? if resource_collection.present?
resource_collection resource_collection
elsif controller_class_path.size > 1 elsif controller_class_path.size > 1
...@@ -69,7 +73,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -69,7 +73,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
options.auth options.auth
end end
def response_status action def response_status(action)
case action case action
when 'get' when 'get'
200 200
...@@ -101,6 +105,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -101,6 +105,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
namespaced_class = namespaced_classes.join('::').singularize namespaced_class = namespaced_classes.join('::').singularize
resource_class = namespaced_class.constantize resource_class = namespaced_class.constantize
raise NameError if resource_class.instance_of? Module raise NameError if resource_class.instance_of? Module
resource_class resource_class
rescue NameError rescue NameError
nil nil
...@@ -129,6 +134,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -129,6 +134,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
class_name.constantize class_name.constantize
rescue NameError => e rescue NameError => e
raise unless e.message.include?(class_name) raise unless e.message.include?(class_name)
nil nil
end end
@resource_class @resource_class
...@@ -156,22 +162,28 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -156,22 +162,28 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
# mod: 'all', 'only_json', 'without_json' # mod: 'all', 'only_json', 'without_json'
def attributes_names(mod: 'all') def attributes_names(mod: 'all')
begin
_attributes = _attributes =
case mod.to_s case mod.to_s
when 'only_json' when 'only_json'
resource_class.columns.select { |column| column.type.in?([:json, :jsonb])} resource_class.columns.select { |column| column.type.in?([:json, :jsonb]) }
when 'without_json' when 'without_json'
resource_class.columns.select { |column| !column.type.in?([:json, :jsonb])} resource_class.columns.select { |column| !column.type.in?([:json, :jsonb]) }
else else
resource_class.columns resource_class.columns
end end
_attributes.map(&:name) - %w(id created_at updated_at) _attributes.map(&:name) - %w[id created_at updated_at]
rescue NameError rescue NameError
[] []
rescue rescue StandardError
[] []
end end
def single_attribute_names(mod: 'all')
attributes_names(mod: mod).reject { |attribute_name| attribute_name.to_s.include?('detail') }
end
def detail_attribute_names(mod: 'all')
attributes_names(mod: mod).select { |attribute_name| attribute_name.to_s.include?('detail') }
end end
def belongs_to_refs def belongs_to_refs
...@@ -185,18 +197,18 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -185,18 +197,18 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
end end
def filename_with_extensions(name) def filename_with_extensions(name)
[name, :json, :jbuilder] * '.' [name, :json, :jbuilder].join('.')
end end
def attributes_list_with_timestamps def attributes_list_with_timestamps
attributes_list(%w(id created_at updated_at) + attributes_names) attributes_list(%w[id created_at updated_at] + attributes_names)
end end
def attributes_list(attributes = attributes_names) def attributes_list(attributes = attributes_names)
attributes.map { |a| ":#{a}"} * ", " attributes.map { |a| ":#{a}" } * ', '
end end
def json_attributes_list(attributes = attributes_names(mod: :only_json)) def json_attributes_list(attributes = attributes_names(mod: :only_json))
attributes.map { |a| "#{a}: {}" } * ", " attributes.map { |a| "#{a}: {}" } * ', '
end end
end end
json.partial! '<%= view_path %>/simple', <%= resource_singular %>: <%= resource_singular %> json.partial! '<%= view_path %>/simple', <%= resource_singular %>: <%= resource_singular %>
<%- if detail_attribute_names.present? -%>
json.extract!(
<%= resource_singular %>,
<%- detail_attribute_names.each do |attribute_name| -%>
:<%= attribute_name %>,
<%- end -%>
)
<%- end -%>
...@@ -5,7 +5,7 @@ json.extract!( ...@@ -5,7 +5,7 @@ json.extract!(
:id, :id,
:created_at, :created_at,
:updated_at, :updated_at,
<%- attributes_names.each do |attribute_name| -%> <%- single_attribute_names.each do |attribute_name| -%>
:<%= attribute_name %>, :<%= attribute_name %>,
<%- end -%> <%- end -%>
) )
......
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