Skip to content

Commit 7920220

Browse files
committed
Added omniauth Facebook and actiontext to activeadmin
1 parent 6ffe728 commit 7920220

29 files changed

+184
-63
lines changed

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Style/EnvHome: # new in 1.29
139139
Style/ExactRegexpMatch: # new in 1.51
140140
Enabled: true
141141
Style/FetchEnvVar: # new in 1.28
142-
Enabled: true
142+
Enabled: false
143143
Style/FileEmpty: # new in 1.48
144144
Enabled: true
145145
Style/FileRead: # new in 1.24

Gemfile

+8
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ gem 'faker'
8080
gem 'bootstrap'
8181

8282
gem "importmap-rails", "~> 1.2"
83+
84+
gem "omniauth", "~> 2.1"
85+
86+
gem "omniauth-google-oauth2", "~> 1.1"
87+
88+
gem "omniauth-rails_csrf_protection", "~> 1.0"
89+
90+
gem "omniauth-facebook", "~> 9.0"

Gemfile.lock

+41
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ GEM
119119
execjs (2.8.1)
120120
faker (3.2.1)
121121
i18n (>= 1.8.11, < 2)
122+
faraday (2.7.10)
123+
faraday-net_http (>= 2.0, < 3.1)
124+
ruby2_keywords (>= 0.0.4)
125+
faraday-net_http (3.0.2)
122126
ffi (1.15.5)
123127
formtastic (4.0.0)
124128
actionpack (>= 5.2.0)
@@ -128,6 +132,7 @@ GEM
128132
has_scope (0.8.1)
129133
actionpack (>= 5.2)
130134
activesupport (>= 5.2)
135+
hashie (5.0.0)
131136
i18n (1.14.1)
132137
concurrent-ruby (~> 1.0)
133138
image_processing (1.12.2)
@@ -152,6 +157,7 @@ GEM
152157
railties (>= 4.2.0)
153158
thor (>= 0.14, < 2.0)
154159
json (2.6.3)
160+
jwt (2.7.1)
155161
kaminari (1.2.2)
156162
activesupport (>= 4.1.0)
157163
kaminari-actionview (= 1.2.2)
@@ -182,6 +188,7 @@ GEM
182188
mini_mime (1.1.5)
183189
minitest (5.19.0)
184190
msgpack (1.7.2)
191+
multi_xml (0.6.0)
185192
net-imap (0.3.7)
186193
date
187194
net-protocol
@@ -196,6 +203,30 @@ GEM
196203
racc (~> 1.4)
197204
nokogiri (1.15.4-x86_64-linux)
198205
racc (~> 1.4)
206+
oauth2 (2.0.9)
207+
faraday (>= 0.17.3, < 3.0)
208+
jwt (>= 1.0, < 3.0)
209+
multi_xml (~> 0.5)
210+
rack (>= 1.2, < 4)
211+
snaky_hash (~> 2.0)
212+
version_gem (~> 1.1)
213+
omniauth (2.1.1)
214+
hashie (>= 3.4.6)
215+
rack (>= 2.2.3)
216+
rack-protection
217+
omniauth-facebook (9.0.0)
218+
omniauth-oauth2 (~> 1.2)
219+
omniauth-google-oauth2 (1.1.1)
220+
jwt (>= 2.0)
221+
oauth2 (~> 2.0.6)
222+
omniauth (~> 2.0)
223+
omniauth-oauth2 (~> 1.8.0)
224+
omniauth-oauth2 (1.8.0)
225+
oauth2 (>= 1.4, < 3)
226+
omniauth (~> 2.0)
227+
omniauth-rails_csrf_protection (1.0.1)
228+
actionpack (>= 4.2)
229+
omniauth (~> 2.0)
199230
orm_adapter (0.5.0)
200231
parallel (1.23.0)
201232
parser (3.2.2.3)
@@ -208,6 +239,8 @@ GEM
208239
nio4r (~> 2.0)
209240
racc (1.7.1)
210241
rack (2.2.8)
242+
rack-protection (3.1.0)
243+
rack (~> 2.2, >= 2.2.4)
211244
rack-test (2.1.0)
212245
rack (>= 1.3)
213246
rails (7.0.7)
@@ -284,6 +317,9 @@ GEM
284317
rexml (~> 3.2, >= 3.2.5)
285318
rubyzip (>= 1.2.2, < 3.0)
286319
websocket (~> 1.0)
320+
snaky_hash (2.0.1)
321+
hashie
322+
version_gem (~> 1.1, >= 1.1.1)
287323
sprockets (4.2.0)
288324
concurrent-ruby (~> 1.0)
289325
rack (>= 2.2.4, < 4)
@@ -297,6 +333,7 @@ GEM
297333
tzinfo (2.0.6)
298334
concurrent-ruby (~> 1.0)
299335
unicode-display_width (2.4.2)
336+
version_gem (1.1.3)
300337
warden (1.2.9)
301338
rack (>= 2.0.9)
302339
web-console (4.2.0)
@@ -335,6 +372,10 @@ DEPENDENCIES
335372
json
336373
kaminari
337374
meta-tags
375+
omniauth (~> 2.1)
376+
omniauth-facebook (~> 9.0)
377+
omniauth-google-oauth2 (~> 1.1)
378+
omniauth-rails_csrf_protection (~> 1.0)
338379
pg
339380
puma
340381
rails (~> 7.0.6)

app/admin/comments.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
filter :published
2828
filter :body
2929
filter :post
30-
filter :user, collection: User.all.map { |u| [u.email, u.id] }
30+
filter :user#, collection: User.all.map { |u| [u.email, u.id] }
3131
filter :updated_at
3232
filter :created_at
3333

3434
form do |f|
3535
f.inputs 'Attached' do
3636
f.semantic_errors
3737
f.input :post
38-
f.input :user, collection: User.all.map { |u| [u.email, u.id] }
38+
f.input :user#, collection: User.all.map { |u| [u.email, u.id] }
3939
end
4040

4141
f.inputs 'Content' do

app/admin/likes.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
actions
1010
end
1111

12-
filter :user, collection: User.all.map { |u| [u.email, u.id] }
12+
filter :user#, collection: User.all.map { |u| [u.email, u.id] }
1313
filter :record_type
1414
filter :created_at
1515
filter :updated_at
1616

1717
form do |f|
1818
f.inputs do
1919
f.semantic_errors
20-
f.input :user, collection: User.all.map { |u| [u.email, u.id] }
20+
f.input :user#, collection: User.all.map { |u| [u.email, u.id] }
2121
f.input :record_type, as: :hidden, input_html: { value: 'Post' }
2222
f.input :record, collection: Post.all.map { |u| [u.title, u.id] }
2323
end

app/assets/stylesheets/application.scss

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
*= require_self
1414
*/
1515

16-
@import "bootstrap";
16+
17+
@import "bootstrap";
18+
19+
.bg-body-tertiary {
20+
background-color: #fff !important;
21+
}
1722

1823
p img {
1924
width: 100%;
20-
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Blog
2+
class AuthorsController < BaseController
3+
def show
4+
@author = AdminUser.find_by_name(params[:id])
5+
end
6+
end
7+
end

app/controllers/blog/user_profiles_controller.rb

-7
This file was deleted.

app/controllers/users/omniauth_callbacks_controller.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
55
# devise :omniauthable, omniauth_providers: [:twitter]
66

77
# You should also create an action method in this controller like this:
8-
# def twitter
9-
# end
8+
def google_oauth2
9+
@user = User.form_google_oauth2(request.env['omniauth.auth'])
10+
sign_in_and_redirect @user
11+
end
12+
13+
def facebook
14+
@user = User.form_facebook(request.env['omniauth.auth'])
15+
sign_in_and_redirect @user
16+
end
1017

1118
# More info at:
1219
# https://github.com/heartcombo/devise#omniauth

app/helpers/blog/authors_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Blog::AuthorsHelper
2+
end

app/helpers/blog/user_profiles_helper.rb

-2
This file was deleted.

app/models/user.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class User < ApplicationRecord
88
:rememberable,
99
:validatable,
1010
:recoverable,
11-
:registerable
11+
:registerable,
12+
:omniauthable,
13+
omniauth_providers: %i[google_oauth2 facebook]
1214

1315
validates :name, presence: true
1416

@@ -20,5 +22,21 @@ def ransackable_attributes(_auth_object = nil)
2022
def ransackable_associations(_auth_object = nil)
2123
%w[comments likes]
2224
end
25+
26+
def form_facebook(auth)
27+
find_or_create_by(email: auth.info.email) do |user|
28+
user.name = auth.info.name
29+
user.email = auth.info.email
30+
user.password = Devise.friendly_token
31+
end
32+
end
33+
34+
def form_google_oauth2(auth)
35+
find_or_create_by(email: auth.info.email) do |user|
36+
user.name = auth.info.name
37+
user.email = auth.info.email
38+
user.password = Devise.friendly_token
39+
end
40+
end
2341
end
2442
end

app/views/blog/user_profiles/show.html.erb renamed to app/views/blog/authors/show.html.erb

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<% set_meta_tags(
2-
title: t('.meta.title', name: @profile.name),
3-
description: t('.meta.description', name: @profile.name, site_name: t('site.name')),
4-
keywords: t('.meta.keywords', name: @profile.name),
5-
canonical: user_profile_url(@profile.name),
2+
title: t('.meta.title', name: @author.name),
3+
description: t('.meta.description', name: @author.name, site_name: t('site.name')),
4+
keywords: t('.meta.keywords', name: @author.name),
5+
canonical: author_url(@author.name),
66
) %>
77
<div class="card" style="margin-bottom: 10px">
8-
<h1 class="card-header"><%= @profile.name %></h1>
8+
<h1 class="card-header"><%= @author.name %></h1>
99
<div class="card-body">
1010
<div class="container text-center">
1111
<div class="row">
1212
<div class="col-2">
13-
<% if @profile.avatar.attached? %>
14-
<%= image_tag @profile.avatar, size: "250x250" %>
13+
<% if @author.avatar.attached? %>
14+
<%= image_tag @author.avatar, size: "250x250" %>
1515
<% else %>
1616
<svg class="bd-placeholder-img" height="250" width="250" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Image cap" preserveAspectRatio="xMidYMid slice" focusable="false">
1717
<rect width="100%" height="100%" fill="#868e96"></rect>
@@ -20,7 +20,7 @@
2020
<% end %>
2121
</div>
2222
<div class="col-10">
23-
<%= @profile.body %>
23+
<%= @author.body %>
2424
</div>
2525
</div>
2626
</div>

app/views/blog/posts/_post.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
</div>
88
<ul class="list-group list-group-flush">
99
<li class="list-group-item"><%= t('blog.posts.show.category') %> <%= link_to post.category.title, category_path(post.category), class: 'card-link' %></li>
10-
<li class="list-group-item"><%= t('blog.posts.show.author') %> <%= link_to post.admin_user.name, user_profile_path(post.admin_user.name), class: 'card-link' %></li>
10+
<li class="list-group-item"><%= t('blog.posts.show.author') %> <%= link_to post.admin_user.name, author_path(post.admin_user.name), class: 'card-link' %></li>
1111
</ul>
1212
</div>

app/views/blog/posts/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ul class="list-group list-group-flush">
1313
<li class="list-group-item"><%= t('.category') %> <%= link_to @post.category.title, category_path(@post.category), class: "card-link" %></li>
1414
<li class="list-group-item"><%= t('.added') %> <%= @post.date_created %></li>
15-
<li class="list-group-item"><%= t('.author') %> <%= link_to @post.admin_user.name, user_profile_path(@post.admin_user.name), class: 'card-link' %></li>
15+
<li class="list-group-item"><%= t('.author') %> <%= link_to @post.admin_user.name, author_path(@post.admin_user.name), class: 'card-link' %></li>
1616
<li class="list-group-item">
1717
<div id="likes"><%= render 'blog/likes/update' %></div>
1818
</li>

app/views/users/confirmations/new.html.erb

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
<% set_meta_tags(
2+
title: 'Resend confirmation instructions',
3+
canonical: '',
4+
) %>
15
<h2>Resend confirmation instructions</h2>
26

37
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
4-
<%= render "devise/shared/error_messages", resource: resource %>
8+
<%= render "users/shared/error_messages", resource: resource %>
59

610
<div class="field">
711
<%= f.label :email %><br />
@@ -13,4 +17,4 @@
1317
</div>
1418
<% end %>
1519

16-
<%= render "devise/shared/links" %>
20+
<%= render "users/shared/links" %>

app/views/users/passwords/edit.html.erb

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
<% set_meta_tags(
2+
title: 'Change your password',
3+
canonical: edit_user_password_url,
4+
) %>
5+
16
<h2>Change your password</h2>
27
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
3-
<%= render "devise/shared/error_messages", resource: resource %>
8+
<%= render "users/shared/error_messages", resource: resource %>
49
<%= f.hidden_field :reset_password_token %>
510
<div class="mb-3">
611
<%= f.label :password, "New password", class: "form-label" %>
@@ -15,4 +20,4 @@
1520
</div>
1621
<%= f.submit "Change my password", class: "btn btn-primary" %>
1722
<% end %>
18-
<%= render "devise/shared/links" %>
23+
<%= render "users/shared/links" %>
+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
<% set_meta_tags(
2+
title: 'Forgot your password?',
3+
canonical: new_user_password_url,
4+
) %>
5+
16
<h2>Forgot your password?</h2>
27
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
3-
<%= render "devise/shared/error_messages", resource: resource %>
8+
<%= render "users/shared/error_messages", resource: resource %>
49
<div class="mb-3">
510
<%= f.label :email, class: "form-label" %>
611
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
712
</div>
813
<%= f.submit "Send me reset password instructions", class: "btn btn-primary" %>
914
<% end %>
10-
<%= render "devise/shared/links" %>
15+
<%= render "users/shared/links" %>

app/views/users/registrations/edit.html.erb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
<% set_meta_tags(
2+
title: "Edit #{resource_name.to_s.humanize}",
3+
canonical: edit_user_registration_url,
4+
) %>
5+
16
<h3>Edit <%= resource_name.to_s.humanize %></h3>
27
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
3-
<%= render "devise/shared/error_messages", resource: resource %>
8+
<%= render "users/shared/error_messages", resource: resource %>
49
<div class="mb-3">
510
<%= f.label :email, class: "form-label" %>
611
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>

app/views/users/registrations/new.html.erb

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
<% set_meta_tags(
2+
title: "Sign up",
3+
canonical: new_user_registration_url,
4+
) %>
5+
16
<h2>Sign up</h2>
27
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
3-
<%= render "devise/shared/error_messages", resource: resource %>
8+
<%= render "users/shared/error_messages", resource: resource %>
49
<div class="mb-3">
510
<%= f.label :email, class: "form-label" %>
611
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
@@ -24,4 +29,4 @@
2429
<%= f.submit "Sign up", class: "btn btn-primary" %>
2530
</div>
2631
<% end %>
27-
<%= render "devise/shared/links" %>
32+
<%= render "users/shared/links" %>

0 commit comments

Comments
 (0)