Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
simple_controller
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
simple_controller
Commits
60bb4ac5
Commit
60bb4ac5
authored
Jun 13, 2023
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support detail attribute for detail view
parent
9a13c9f4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
51 deletions
+72
-51
simple_controller_generator.rb
...nerators/simple_controller/simple_controller_generator.rb
+62
-50
_detail.json.jbuilder
...s/simple_controller/templates/views/_detail.json.jbuilder
+9
-0
_single.json.jbuilder
...s/simple_controller/templates/views/_single.json.jbuilder
+1
-1
No files found.
lib/generators/simple_controller/simple_controller_generator.rb
View file @
60bb4ac5
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
lib/generators/simple_controller/templates/views/_detail.json.jbuilder
View file @
60bb4ac5
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 -%>
lib/generators/simple_controller/templates/views/_single.json.jbuilder
View file @
60bb4ac5
...
...
@@ -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 -%>
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment