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
25037d0f
Commit
25037d0f
authored
Nov 07, 2021
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 升级simple_controller 脚手架生成的代码
parent
a701ca29
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
7 deletions
+43
-7
simple_controller_generator.rb
...nerators/simple_controller/simple_controller_generator.rb
+21
-3
controller.rb.tt
.../simple_controller/templates/controllers/controller.rb.tt
+7
-1
_detail.json.jbuilder
...s/simple_controller/templates/views/_detail.json.jbuilder
+1
-1
_simple.json.jbuilder
...s/simple_controller/templates/views/_simple.json.jbuilder
+5
-1
_single.json.jbuilder
...s/simple_controller/templates/views/_single.json.jbuilder
+9
-1
No files found.
lib/generators/simple_controller/simple_controller_generator.rb
View file @
25037d0f
...
...
@@ -154,9 +154,19 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
resource_class
&
.
model_name
&
.
collection
end
def
attributes_names
# mod: 'all', 'only_json', 'without_json'
def
attributes_names
(
mod:
'all'
)
begin
resource_class
.
columns
.
map
(
&
:name
)
-
%w(id created_at updated_at)
_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
...
...
@@ -164,6 +174,10 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
end
end
def
belongs_to_refs
resource_class
.
reflections
.
values
.
select
{
|
ref
|
ref
.
belongs_to?
&&
!
ref
.
polymorphic?
}
end
def
filename_with_extensions
(
name
)
[
name
,
:json
,
:jbuilder
]
*
'.'
end
...
...
@@ -173,6 +187,10 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
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
}
: {}"
}
*
", "
end
end
lib/generators/simple_controller/templates/controllers/controller.rb.tt
View file @
25037d0f
...
...
@@ -10,9 +10,15 @@ class <%= controller_class_name %>Controller < SimpleController::BaseController
<% end -%>
private
def <%= resource_singular %>_params
params.require(:<%= resource_singular %>).permit(
<%= attributes_list(attributes_names) %>
<%- attributes_names(mod: :without_json).each do |attribute_name| -%>
:<%= attribute_name %>,
<%- end -%>
<%- attributes_names(mod: :only_json).each do |attribute_name| -%>
<%= attribute_name %>: {},
<%- end -%>
)
end
end
lib/generators/simple_controller/templates/views/_detail.json.jbuilder
View file @
25037d0f
json.partial! "simple", <%= resource_singular %>: <%= resource_singular %>
json.partial! "
<%= view_path %>/
simple", <%= resource_singular %>: <%= resource_singular %>
lib/generators/simple_controller/templates/views/_simple.json.jbuilder
View file @
25037d0f
json.partial! "single", <%= resource_singular %>: <%= resource_singular %>
json.partial! "<%= view_path %>/single", <%= resource_singular %>: <%= resource_singular %>
<%- belongs_to_refs.each do |ref| -%>
json.<%= ref.name.to_s %> <%= resource_singular %>.<%= ref.name.to_s %>, partial: '<%= File.join(ref.klass.name.underscore.pluralize, 'single') %>', as: :<%= ref.name %>
<%- end -%>
lib/generators/simple_controller/templates/views/_single.json.jbuilder
View file @
25037d0f
json.extract! <%= resource_singular %>, <%= attributes_list_with_timestamps %>
json.extract!(
<%= resource_singular %>,
:id,
:created_at,
:updated_at,
<%- attributes_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