Skip to content

Instantly share code, notes, and snippets.

@anon987654321
Created May 3, 2020 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anon987654321/940a55acaae969310067210d5981bd4e to your computer and use it in GitHub Desktop.
Save anon987654321/940a55acaae969310067210d5981bd4e to your computer and use it in GitHub Desktop.
commit e7f6af15c65397c1e185aadfd10ce46c99fb1fdd
Author: dev <dev@dev.my.domain>
Date: Wed Jan 1 00:00:00 2020 -0100
Add avatars with EXIF removal
diff --git a/Gemfile b/Gemfile
index 172128e..d6b9b2c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -31,6 +31,9 @@ gem 'devise', '~> 4.7', '>= 4.7.1'
# Spam protection with text-based CAPTCHAs
gem 'acts_as_textcaptcha', '~> 4.5'
+# A simple GitHub-like identicon generator
+gem 'identicon', '~> 0.0.5'
+
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index adb79ab..8a8cfcf 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -74,6 +74,7 @@ GEM
regexp_parser (~> 1.5)
xpath (~> 3.2)
childprocess (3.0.0)
+ chunky_png (1.3.11)
concurrent-ruby (1.1.5)
crass (1.0.6)
devise (4.7.1)
@@ -88,6 +89,8 @@ GEM
activesupport (>= 4.2.0)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
+ identicon (0.0.5)
+ chunky_png
image_processing (1.10.3)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
@@ -205,6 +208,7 @@ DEPENDENCIES
byebug
capybara (>= 2.15)
devise (~> 4.7, >= 4.7.1)
+ identicon (~> 0.0.5)
image_processing (~> 1.2)
jbuilder (~> 2.7)
puma (~> 4.1)
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 00d720e..4ee6933 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
protected
def configure_permitted_parameters
- devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:display_name, :email, :password)}
- devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:display_name, :email, :password, :current_password)}
+ devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :display_name, :password, :avatar)}
+ devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:email, :display_name, :password, :password_confirmation, :current_password, :avatar)}
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 3221c60..8d731a4 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -8,6 +8,15 @@ class User < ApplicationRecord
acts_as_textcaptcha
+ has_one_attached :avatar
+ before_save :send_to_active_storage
+
+ after_create :set_admin
+
+ def send_to_active_storage
+ self.avatar.attach(io: StringIO.new(Identicon.blob_for(email)), filename: "#{email}-avatar.png")
+ end
+
private
def set_admin
if User.count == 1
diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb
index 9ad36b3..a2ec75e 100644
--- a/app/views/devise/registrations/edit.html.erb
+++ b/app/views/devise/registrations/edit.html.erb
@@ -3,6 +3,13 @@
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
+ <%= image_tag(current_user.avatar.variant(resize_to_fit: [70, 70], strip: true).processed) %>
+
+ <div class="field">
+ <%= f.label :avatar %>
+ <%= f.file_field :avatar %>
+ </div>
+
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index f4894f5..2a62fdc 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -13,6 +13,9 @@
<%= link_to "New post", new_post_path %>
<%= link_to "Mew user", edit_user_registration_path %>
<%= link_to "Log out", destroy_user_session_path, method: :delete %>
+ <%= current_user.display_name %>
+ <%= image_tag(current_user.avatar.variant(resize_to_fit: [30, 30], strip: true).processed) %>
<% else %>
<%= link_to "Log in", new_user_session_path %>
<%= link_to "Sign up", new_user_registration_path %>
diff --git a/app/views/posts/_comment.html.erb b/app/views/posts/_comment.html.erb
index ef8ada1..6808807 100755
--- a/app/views/posts/_comment.html.erb
+++ b/app/views/posts/_comment.html.erb
@@ -1,5 +1,8 @@
<div>
- <h4><%= comment.display_name %></h4>
+ <h4>
+ <%= image_tag(comment.user.avatar.variant(resize_to_fit: [50, 50], strip: true).processed) %>
+ <%= comment.display_name %>
+ </h4>
<p><%= comment.body %></p>
<% if user_signed_in? && current_user == comment.user %>
<%= link_to 'Edit', edit_post_comment_path(@post, comment) %>
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb
index b2a0e9a..0993ff8 100644
--- a/app/views/posts/index.html.erb
+++ b/app/views/posts/index.html.erb
@@ -35,6 +35,9 @@
<% end %>
</tr>
+ <%= image_tag(post.user.avatar.variant(resize_to_fit: [100, 100])) %>
+ <%= post.user.display_name %>
<% post.images.each do |image| %>
<%= link_to(image_tag(image.variant(resize_to_fill: [100, 100], strip: true).processed), image.variant(strip: true).processed, target: :_blank) %>
<% end %>
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb
index 2227350..6abbd47 100644
--- a/app/views/posts/show.html.erb
+++ b/app/views/posts/show.html.erb
@@ -1,5 +1,10 @@
<p id="notice"><%= notice %></p>
+<%= image_tag(@post.user.avatar.variant(resize_to_fit: [70, 70], strip: true).processed) %>
+<%= @post.user.display_name %>
<% @post.images.each do |image| %>
<%= link_to(image_tag(image.variant(resize_to_fill: [500, 500], strip: true).processed), image.variant(strip: true).processed, target: :_blank) %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment