Skip to content

Commit ada99ca

Browse files
committed
added devise
1 parent 4256385 commit ada99ca

File tree

57 files changed

+4066
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4066
-13
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.byebug_history
22
.DS_Store
3-
public/uploads
3+
public/uploads
4+
tmp/

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ gem 'jbuilder', '~> 2.5'
3838
gem 'haml'
3939
gem "haml-rails", "~> 0.9"
4040
gem 'bootstrap-sass', '~> 3.3.6'
41+
gem 'devise'
4142

4243
group :development, :test do
4344
# Call 'byebug' anywhere in the code to stop execution and get a debugger console

Gemfile.lock

+13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ GEM
4141
arel (7.1.4)
4242
autoprefixer-rails (6.7.2)
4343
execjs
44+
bcrypt (3.1.11)
4445
bootstrap-sass (3.3.7)
4546
autoprefixer-rails (>= 5.2.1)
4647
sass (>= 3.3.4)
@@ -55,6 +56,12 @@ GEM
5556
coffee-script-source (1.12.2)
5657
concurrent-ruby (1.0.4)
5758
debug_inspector (0.0.2)
59+
devise (4.2.0)
60+
bcrypt (~> 3.0)
61+
orm_adapter (~> 0.1)
62+
railties (>= 4.1.0, < 5.1)
63+
responders
64+
warden (~> 1.2.3)
5865
erubis (2.7.0)
5966
execjs (2.7.0)
6067
ffi (1.9.17)
@@ -98,6 +105,7 @@ GEM
98105
nio4r (1.2.1)
99106
nokogiri (1.7.0.1)
100107
mini_portile2 (~> 2.1.0)
108+
orm_adapter (0.5.0)
101109
puma (3.7.0)
102110
rack (2.0.1)
103111
rack-test (0.6.3)
@@ -129,6 +137,8 @@ GEM
129137
rb-fsevent (0.9.8)
130138
rb-inotify (0.9.8)
131139
ffi (>= 0.5.0)
140+
responders (2.3.0)
141+
railties (>= 4.2.0, < 5.1)
132142
ruby_parser (3.8.4)
133143
sexp_processor (~> 4.1)
134144
sass (3.4.23)
@@ -162,6 +172,8 @@ GEM
162172
thread_safe (~> 0.1)
163173
uglifier (3.0.4)
164174
execjs (>= 0.3.0, < 3)
175+
warden (1.2.7)
176+
rack (>= 1.0)
165177
web-console (3.4.0)
166178
actionview (>= 5.0)
167179
activemodel (>= 5.0)
@@ -178,6 +190,7 @@ DEPENDENCIES
178190
bootstrap-sass (~> 3.3.6)
179191
byebug
180192
coffee-rails (~> 4.2)
193+
devise
181194
haml
182195
haml-rails (~> 0.9)
183196
jbuilder (~> 2.5)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/

app/assets/stylesheets/comments.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Comments controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class [scope]::ConfirmationsController < Devise::ConfirmationsController
2+
# GET /resource/confirmation/new
3+
# def new
4+
# super
5+
# end
6+
7+
# POST /resource/confirmation
8+
# def create
9+
# super
10+
# end
11+
12+
# GET /resource/confirmation?confirmation_token=abcdef
13+
# def show
14+
# super
15+
# end
16+
17+
# protected
18+
19+
# The path used after resending confirmation instructions.
20+
# def after_resending_confirmation_instructions_path_for(resource_name)
21+
# super(resource_name)
22+
# end
23+
24+
# The path used after confirmation.
25+
# def after_confirmation_path_for(resource_name, resource)
26+
# super(resource_name, resource)
27+
# end
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class [scope]::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2+
# You should configure your model like this:
3+
# devise :omniauthable, omniauth_providers: [:twitter]
4+
5+
# You should also create an action method in this controller like this:
6+
# def twitter
7+
# end
8+
9+
# More info at:
10+
# https://github.com/plataformatec/devise#omniauth
11+
12+
# GET|POST /resource/auth/twitter
13+
# def passthru
14+
# super
15+
# end
16+
17+
# GET|POST /users/auth/twitter/callback
18+
# def failure
19+
# super
20+
# end
21+
22+
# protected
23+
24+
# The path used when OmniAuth fails
25+
# def after_omniauth_failure_path_for(scope)
26+
# super(scope)
27+
# end
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class [scope]::PasswordsController < Devise::PasswordsController
2+
# GET /resource/password/new
3+
# def new
4+
# super
5+
# end
6+
7+
# POST /resource/password
8+
# def create
9+
# super
10+
# end
11+
12+
# GET /resource/password/edit?reset_password_token=abcdef
13+
# def edit
14+
# super
15+
# end
16+
17+
# PUT /resource/password
18+
# def update
19+
# super
20+
# end
21+
22+
# protected
23+
24+
# def after_resetting_password_path_for(resource)
25+
# super(resource)
26+
# end
27+
28+
# The path used after sending reset password instructions
29+
# def after_sending_reset_password_instructions_path_for(resource_name)
30+
# super(resource_name)
31+
# end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class [scope]::RegistrationsController < Devise::RegistrationsController
2+
# before_action :configure_sign_up_params, only: [:create]
3+
# before_action :configure_account_update_params, only: [:update]
4+
5+
# GET /resource/sign_up
6+
# def new
7+
# super
8+
# end
9+
10+
# POST /resource
11+
# def create
12+
# super
13+
# end
14+
15+
# GET /resource/edit
16+
# def edit
17+
# super
18+
# end
19+
20+
# PUT /resource
21+
# def update
22+
# super
23+
# end
24+
25+
# DELETE /resource
26+
# def destroy
27+
# super
28+
# end
29+
30+
# GET /resource/cancel
31+
# Forces the session data which is usually expired after sign
32+
# in to be expired now. This is useful if the user wants to
33+
# cancel oauth signing in/up in the middle of the process,
34+
# removing all OAuth session data.
35+
# def cancel
36+
# super
37+
# end
38+
39+
# protected
40+
41+
# If you have extra params to permit, append them to the sanitizer.
42+
# def configure_sign_up_params
43+
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
44+
# end
45+
46+
# If you have extra params to permit, append them to the sanitizer.
47+
# def configure_account_update_params
48+
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
49+
# end
50+
51+
# The path used after sign up.
52+
# def after_sign_up_path_for(resource)
53+
# super(resource)
54+
# end
55+
56+
# The path used after sign up for inactive accounts.
57+
# def after_inactive_sign_up_path_for(resource)
58+
# super(resource)
59+
# end
60+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class [scope]::SessionsController < Devise::SessionsController
2+
# before_action :configure_sign_in_params, only: [:create]
3+
4+
# GET /resource/sign_in
5+
# def new
6+
# super
7+
# end
8+
9+
# POST /resource/sign_in
10+
# def create
11+
# super
12+
# end
13+
14+
# DELETE /resource/sign_out
15+
# def destroy
16+
# super
17+
# end
18+
19+
# protected
20+
21+
# If you have extra params to permit, append them to the sanitizer.
22+
# def configure_sign_in_params
23+
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
24+
# end
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class [scope]::UnlocksController < Devise::UnlocksController
2+
# GET /resource/unlock/new
3+
# def new
4+
# super
5+
# end
6+
7+
# POST /resource/unlock
8+
# def create
9+
# super
10+
# end
11+
12+
# GET /resource/unlock?unlock_token=abcdef
13+
# def show
14+
# super
15+
# end
16+
17+
# protected
18+
19+
# The path used after sending unlock password instructions
20+
# def after_sending_unlock_instructions_path_for(resource)
21+
# super(resource)
22+
# end
23+
24+
# The path used after unlocking the resource
25+
# def after_unlock_path_for(resource)
26+
# super(resource)
27+
# end
28+
end
+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
class ApplicationController < ActionController::Base
22
protect_from_forgery with: :exception
3+
before_action :authenticate_user!
4+
before_action :configure_permitted_parameters, if: :devise_controller?
5+
6+
protected
7+
8+
def configure_permitted_parameters
9+
devise_parameter_sanitizer.permit(:sign_in) do |user_params|
10+
user_params.permit(:username, :email, :password, :given_name, :last_name)
11+
end
12+
13+
devise_parameter_sanitizer.permit(:account_update) do |user_params|
14+
user_params.permit(:username, :email, :password, :current_password, :password_confirmation, :given_name, :last_name)
15+
end
16+
end
317
end

app/controllers/articles_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class ArticlesController < ApplicationController
2+
23
def show
34
@article = Article.find(params[:id])
45
end
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CommentsController < ApplicationController
2+
3+
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
4+
5+
def create
6+
@article = Article.find(params[:article_id])
7+
@comment = @article.comments.create(comment_params)
8+
redirect_to article_path(@article)
9+
end
10+
11+
def destroy
12+
@article = Article.find(params[:article_id])
13+
@comment = @article.comments.find(params[:id])
14+
@comment.destroy
15+
redirect_to article_path(@article)
16+
end
17+
18+
private
19+
def comment_params
20+
params.require(:comment).permit(:commenter, :body)
21+
end
22+
end

app/helpers/application_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
module ApplicationHelper
2+
def app_name
3+
'First Blog App'
4+
end
25
end

app/helpers/comments_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module CommentsHelper
2+
end

app/models/article.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class Article < ApplicationRecord
2+
belongs_to :user
3+
has_many :comments, dependent: :destroy
24
validates :title, presence: true, length: { minimum: 5 }
35
end

app/models/comment.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Comment < ApplicationRecord
2+
belongs_to :article
3+
end

app/models/user.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class User < ApplicationRecord
2+
# Include default devise modules. Others available are:
3+
# :confirmable, :lockable, :timeoutable and :omniauthable
4+
has_many :articles
5+
devise :database_authenticatable, :registerable,
6+
:recoverable, :rememberable, :trackable, :validatable
7+
end

app/views/articles/show.html.haml

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@
44
%p
55
%strong Text:
66
= @article.text
7-
= link_to 'Back', articles_path
7+
8+
= render @article.comments
9+
10+
%h2 Add a comment:
11+
= render 'comments/form'
12+
13+
= link_to 'Edit', edit_article_path(@article)
14+
|
15+
\#{link_to 'Back', articles_path}

app/views/comments/_comment.html.haml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%h2 Comments
2+
%p
3+
%strong
4+
Commenter:
5+
= comment.commenter
6+
7+
%p
8+
%strong Comment:
9+
= comment.body
10+
11+
%p
12+
= link_to 'Destroy Comment', [comment.article, comment],
13+
method: :delete,
14+
data: { confirm: 'Are you sure?' }

0 commit comments

Comments
 (0)