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
27801754
Commit
27801754
authored
Dec 31, 2017
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor simple controller with resource_plural & resource_singular
parent
9487c0bf
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
48 additions
and
27 deletions
+48
-27
simple_controller_generator.rb
...nerators/simple_controller/simple_controller_generator.rb
+10
-2
controller.rb.tt
.../simple_controller/templates/controllers/controller.rb.tt
+9
-4
spec.rb.tt
lib/generators/simple_controller/templates/specs/spec.rb.tt
+10
-9
_detail.json.jbuilder
...s/simple_controller/templates/views/_detail.json.jbuilder
+1
-1
_simple.json.jbuilder
...s/simple_controller/templates/views/_simple.json.jbuilder
+1
-1
_single.json.jbuilder
...s/simple_controller/templates/views/_single.json.jbuilder
+1
-1
index.json.jbuilder
...ors/simple_controller/templates/views/index.json.jbuilder
+3
-3
show.json.jbuilder
...tors/simple_controller/templates/views/show.json.jbuilder
+1
-1
base_controller.rb
lib/simple_controller/base_controller.rb
+12
-5
No files found.
lib/generators/simple_controller/simple_controller_generator.rb
View file @
27801754
...
@@ -21,7 +21,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
...
@@ -21,7 +21,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
def
copy_view_files
def
copy_view_files
%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_
class_
path
,
filename
)
template
"views/
#{
filename
}
"
,
File
.
join
(
'app/views'
,
view_path
,
filename
)
end
end
end
end
...
@@ -33,7 +33,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
...
@@ -33,7 +33,7 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
protected
protected
def
view_
class_
path
def
view_path
return
options
.
view
if
options
.
view
.
present?
return
options
.
view
if
options
.
view
.
present?
if
controller_class_path
.
size
>
1
if
controller_class_path
.
size
>
1
File
.
join
controller_class_path
[
0
],
plural_name
File
.
join
controller_class_path
[
0
],
plural_name
...
@@ -103,6 +103,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
...
@@ -103,6 +103,14 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
@resource_class
@resource_class
end
end
def
resource_plural
resource_class
&
.
model_name
&
.
plural
end
def
resource_singular
resource_class
&
.
model_name
&
.
singular
end
def
attributes_names
def
attributes_names
begin
begin
resource_class
.
columns
.
map
(
&
:name
)
-
%w(id created_at updated_at)
resource_class
.
columns
.
map
(
&
:name
)
-
%w(id created_at updated_at)
...
...
lib/generators/simple_controller/templates/controllers/controller.rb.tt
View file @
27801754
class <%= controller_class_name %>Controller < SimpleController::BaseController
class <%= controller_class_name %>Controller < SimpleController::BaseController
set_view_path '<%= view_class_path %>'
defaults(
<% if auth.present? %>acts_as_auth_action :<%= auth.downcase %><% end %>
resource_class: <%= resource_class %>,
collection_name: '<%= resource_plural %>',
instance_name: '<%= resource_singular %>',
view_path: '<%= view_path %>'
)
<% if auth.present? %>acts_as_auth_action :<%= auth.downcase %><% end -%>
private
private
def <%=
singular_name
%>_params
def <%=
resource_singular
%>_params
params.require(:<%=
singular_name
%>).permit(
params.require(:<%=
resource_singular
%>).permit(
<%= attributes_list(attributes_names) %>
<%= attributes_list(attributes_names) %>
)
)
end
end
...
...
lib/generators/simple_controller/templates/specs/spec.rb.tt
View file @
27801754
require 'swagger_helper'
require 'swagger_helper'
<%=
singular_name
.upcase %>_REF = {
<%=
resource_singular
.upcase %>_REF = {
type: :object, properties: {
type: :object, properties: {
<%=
singular_name
%>: {
<%=
resource_singular
%>: {
type: :object, properties: {
type: :object, properties: {
<% if resource_class&.columns_hash.present? -%>
<% if resource_class&.columns_hash.present? -%>
<% resource_class.columns_hash.except('id', 'created_at', 'updated_at').values.each do |column| -%>
<% resource_class.columns_hash.except('id', 'created_at', 'updated_at').values.each do |column| -%>
...
@@ -14,7 +14,8 @@ require 'swagger_helper'
...
@@ -14,7 +14,8 @@ require 'swagger_helper'
}
}
}
}
<%= singular_name.upcase %>_VALUE = FactoryBot.attributes_for(:<%= resource_class.to_s.underscore.sub('/', '_') %>)
<%= resource_singular.upcase %>_VALUE = FactoryBot.attributes_for(:<%=
resource_singular %>)
RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, tags: ["<%= controller_class_path.join(' ') %>"] do
RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, tags: ["<%= controller_class_path.join(' ') %>"] do
before :each do
before :each do
...
@@ -22,7 +23,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
...
@@ -22,7 +23,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
@auth = <%= auth %>.register "auth", "password"
@auth = <%= auth %>.register "auth", "password"
@auth_token = <%= auth %>.login "auth", "password"
@auth_token = <%= auth %>.login "auth", "password"
<% end -%>
<% end -%>
@<%=
plural_name %> = FactoryBot.create_list(:<%= resource_class.to_s.underscore.sub('/', '_')
%>, 5)
@<%=
resource_plural %> = FactoryBot.create_list(:<%= resource_singular
%>, 5)
end
end
<% @routes.each do | template, path_item | -%>
<% @routes.each do | template, path_item | -%>
...
@@ -36,7 +37,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
...
@@ -36,7 +37,7 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
parameter '<%= param %>', in: :path, type: :string
parameter '<%= param %>', in: :path, type: :string
<% end -%>
<% end -%>
<% path_item[:params].each do |param| -%>
<% path_item[:params].each do |param| -%>
let(:<%= param %>) { @<%=
plural_name
%>.first.id }
let(:<%= param %>) { @<%=
resource_plural
%>.first.id }
<% end -%>
<% end -%>
<% end -%>
<% end -%>
...
@@ -47,10 +48,10 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
...
@@ -47,10 +48,10 @@ RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true,
consumes 'application/json'
consumes 'application/json'
<% if ['post', 'patch'].include? action -%>
<% if ['post', 'patch'].include? action -%>
parameter :<%=
singular_name
%>, in: :body, schema: <%=
parameter :<%=
resource_singular
%>, in: :body, schema: <%=
singular_name
.upcase %>_REF
resource_singular
.upcase %>_REF
let(:<%=
singular_name
%>) do
let(:<%=
resource_singular
%>) do
{ <%=
singular_name %>: <%= singular_name
.upcase %>_VALUE }
{ <%=
resource_singular %>: <%= resource_singular
.upcase %>_VALUE }
end
end
<% end -%>
<% end -%>
response(<%= response_status action %>, description: 'successful') do
response(<%= response_status action %>, description: 'successful') do
...
...
lib/generators/simple_controller/templates/views/_detail.json.jbuilder
View file @
27801754
json.partial! "<%= view_
class_path %>/single", <%= singular_name %>: <%= singular_name
%>
json.partial! "<%= view_
path %>/single", <%= resource_singular %>: <%= resource_singular
%>
lib/generators/simple_controller/templates/views/_simple.json.jbuilder
View file @
27801754
json.partial! "<%= view_
class_path %>/single", <%= singular_name %>: <%= singular_name
%>
json.partial! "<%= view_
path %>/single", <%= resource_singular %>: <%= resource_singular
%>
lib/generators/simple_controller/templates/views/_single.json.jbuilder
View file @
27801754
json.extract! <%=
singular_name
%>, <%= attributes_list_with_timestamps %>
json.extract! <%=
resource_singular
%>, <%= attributes_list_with_timestamps %>
lib/generators/simple_controller/templates/views/index.json.jbuilder
View file @
27801754
json.current_page @<%=
plural_name
%>.current_page
json.current_page @<%=
resource_plural
%>.current_page
json.total_page @<%=
plural_name
%>.total_pages
json.total_page @<%=
resource_plural
%>.total_pages
json.<%=
plural_name %> @<%= plural_name %>, partial: '<%= view_class_path %>/simple', as: :<%= singular_name
%>
json.<%=
resource_plural %> @<%= resource_plural %>, partial: '<%= view_path %>/simple', as: :<%= resource_singular
%>
lib/generators/simple_controller/templates/views/show.json.jbuilder
View file @
27801754
json.partial! "<%= view_
class_path %>/detail", <%= singular_name %>: @<%= singular_name
%>
json.partial! "<%= view_
path %>/detail", <%= resource_singular %>: @<%= resource_singular
%>
lib/simple_controller/base_controller.rb
View file @
27801754
...
@@ -15,12 +15,19 @@ class SimpleController::BaseController < ::InheritedResources::Base
...
@@ -15,12 +15,19 @@ class SimpleController::BaseController < ::InheritedResources::Base
protected
protected
def
self
.
set_view_path
path
class
<<
self
@view_path
=
path
def
view_path
end
@view_path
end
def
self
.
view_path
def
defaults
(
options
)
@view_path
set_view_path
options
.
delete
(
:view_path
)
super
(
options
)
end
def
set_view_path
path
@view_path
=
path
end
end
end
def
view_path
def
view_path
...
...
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