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
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'
]
else
'specs/auth_spec.rb'
template_file
=
"specs/spec.rb"
else
end
'specs/spec.rb'
template
template_file
,
File
.
join
(
"spec/requests"
,
controller_class_path
,
"
#{
controller_file_name
}
_spec.rb"
)
end
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,28 +162,34 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
...
@@ -156,28 +162,34 @@ 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
StandardError
rescue
[]
[]
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
active_record?
?
active_record?
?
resource_class
.
reflections
.
values
.
select
{
|
ref
|
ref
.
belongs_to?
&&
!
ref
.
polymorphic?
}
:
resource_class
.
reflections
.
values
.
select
{
|
ref
|
ref
.
belongs_to?
&&
!
ref
.
polymorphic?
}
:
[]
[]
end
end
def
active_record?
def
active_record?
...
@@ -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
lib/generators/simple_controller/templates/views/_detail.json.jbuilder
View file @
60bb4ac5
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 -%>
lib/generators/simple_controller/templates/views/_single.json.jbuilder
View file @
60bb4ac5
...
@@ -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 -%>
)
)
...
...
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