Commit 60bb4ac5 by liyijie

feat: support detail attribute for detail view

parent 9a13c9f4
class SimpleControllerGenerator < Rails::Generators::NamedBase
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 :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 :auth, type: :string, desc: "Authentication model name"
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-view', type: :boolean, desc: "Do not generate views file"
class_option :view, type: :string, desc: 'View files generate folder'
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 :auth, type: :string, desc: 'Authentication model name'
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-view', type: :boolean, desc: 'Do not generate views file'
def setup
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?
return if options['auth-only']
@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
def create_controller_files
if options["auth-only"]
template_file = "controllers/auth_controller.rb"
else
template_file = "controllers/controller.rb"
end
template template_file, File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb")
template_file = if options['auth-only']
'controllers/auth_controller.rb'
else
'controllers/controller.rb'
end
template template_file, File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
end
def copy_view_files
return if options["auth-only"] || options["no-view"]
%w(index show _single _simple _detail).each do |view|
return if options['auth-only'] || options['no-view']
%w[index show _single _simple _detail].each do |view|
filename = filename_with_extensions(view)
template "views/#{filename}", File.join('app/views', view_path, filename)
end
end
def create_swagger_files
return if options["no-swagger"]
if options["auth-only"]
template_file = "specs/auth_spec.rb"
else
template_file = "specs/spec.rb"
end
template template_file, File.join("spec/requests", controller_class_path, "#{controller_file_name}_spec.rb")
return if options['no-swagger']
template_file = if options['auth-only']
'specs/auth_spec.rb'
else
'specs/spec.rb'
end
template template_file, File.join('spec/requests', controller_class_path, "#{controller_file_name}_spec.rb")
end
protected
def view_path
return options.view if options.view.present?
if resource_collection.present?
resource_collection
elsif controller_class_path.size > 1
......@@ -69,7 +73,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
options.auth
end
def response_status action
def response_status(action)
case action
when 'get'
200
......@@ -101,6 +105,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
namespaced_class = namespaced_classes.join('::').singularize
resource_class = namespaced_class.constantize
raise NameError if resource_class.instance_of? Module
resource_class
rescue NameError
nil
......@@ -129,6 +134,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
class_name.constantize
rescue NameError => e
raise unless e.message.include?(class_name)
nil
end
@resource_class
......@@ -156,28 +162,34 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
# mod: 'all', 'only_json', 'without_json'
def attributes_names(mod: 'all')
begin
_attributes =
case mod.to_s
when 'only_json'
resource_class.columns.select { |column| column.type.in?([:json, :jsonb])}
when 'without_json'
resource_class.columns.select { |column| !column.type.in?([:json, :jsonb])}
else
resource_class.columns
end
_attributes.map(&:name) - %w(id created_at updated_at)
rescue NameError
[]
rescue
[]
end
_attributes =
case mod.to_s
when 'only_json'
resource_class.columns.select { |column| column.type.in?([:json, :jsonb]) }
when 'without_json'
resource_class.columns.select { |column| !column.type.in?([:json, :jsonb]) }
else
resource_class.columns
end
_attributes.map(&:name) - %w[id created_at updated_at]
rescue NameError
[]
rescue StandardError
[]
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
def belongs_to_refs
active_record? ?
resource_class.reflections.values.select { |ref| ref.belongs_to? && !ref.polymorphic? } :
[]
active_record? ?
resource_class.reflections.values.select { |ref| ref.belongs_to? && !ref.polymorphic? } :
[]
end
def active_record?
......@@ -185,18 +197,18 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
end
def filename_with_extensions(name)
[name, :json, :jbuilder] * '.'
[name, :json, :jbuilder].join('.')
end
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
def attributes_list(attributes = attributes_names)
attributes.map { |a| ":#{a}"} * ", "
attributes.map { |a| ":#{a}" } * ', '
end
def json_attributes_list(attributes = attributes_names(mod: :only_json))
attributes.map { |a| "#{a}: {}" } * ", "
attributes.map { |a| "#{a}: {}" } * ', '
end
end
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!(
:id,
:created_at,
:updated_at,
<%- attributes_names.each do |attribute_name| -%>
<%- single_attribute_names.each do |attribute_name| -%>
:<%= attribute_name %>,
<%- 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