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
b8de68a4
Commit
b8de68a4
authored
Dec 06, 2018
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SimpleController supports rails api authentication
parent
70b1be3a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
0 deletions
+69
-0
simple_controller_generator.rb
...nerators/simple_controller/simple_controller_generator.rb
+11
-0
auth_controller.rb.tt
...le_controller/templates/controllers/auth_controller.rb.tt
+5
-0
auth_spec.rb.tt
...erators/simple_controller/templates/specs/auth_spec.rb.tt
+53
-0
No files found.
lib/generators/simple_controller/simple_controller_generator.rb
View file @
b8de68a4
...
...
@@ -6,19 +6,26 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
class_option
:view
,
type: :string
,
desc:
"View files generate folder"
class_option
:model
,
type: :string
,
desc:
"Model name for extract attributes"
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"
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
&
.
columns_hash
.
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"
)
end
def
copy_view_files
return
if
options
[
"auth-only"
]
%w(index show _single _simple _detail)
.
each
do
|
view
|
filename
=
filename_with_extensions
(
view
)
template
"views/
#{
filename
}
"
,
File
.
join
(
'app/views'
,
view_path
,
filename
)
...
...
@@ -27,7 +34,11 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
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"
)
end
...
...
lib/generators/simple_controller/templates/controllers/auth_controller.rb.tt
0 → 100644
View file @
b8de68a4
class <%= controller_class_name %>Controller < ApplicationController
<% if auth.present? %>
acts_as_auth_session :<%= auth.downcase %>
<% end -%>
end
lib/generators/simple_controller/templates/specs/auth_spec.rb.tt
0 → 100644
View file @
b8de68a4
require 'swagger_helper'
RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, tags: ["<%= controller_class_path.join(' ') %>"] do
before :each do
<% if auth.present? -%>
@<%= auth_singular %> = <%= auth %>.register "auth", "password"
@auth_token = <%= auth %>.login "auth", "password"
<% end -%>
end
path '/auth/<%= auth_singular %>/session' do
delete(summary: 'logout') do
produces 'application/json'
consumes 'application/json'
parameter '<%= auth %>-Token', in: :header, type: :string
let('<%= auth %>-Token') { @auth_token.token }
response(200, description: 'successful') do
it {
body = JSON.parse(response.body)
expect(body['message']).to eq('logout successful')
}
end
end
post(summary: 'login') do
produces 'application/json'
consumes 'application/json'
parameter :<%= auth_singular %>, in: :body, schema: {
type: :object, properties: {
<%= auth_singular %>: {
type: :object, properties: {
name: { type: :string },
password: { type: :string },
}
}
}
}
response(200, description: 'successful') do
let(:<%= auth_singular %>) {
{
<%= auth_singular %>: {
name: "auth",
password: "password",
}
}
}
end
end
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