Skip to content

Commit

Permalink
Add cloudinary to upload user profile photo
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelp committed Jun 11, 2020
1 parent 82646f0 commit 81f7a8a
Show file tree
Hide file tree
Showing 30 changed files with 73 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/cloudinary.yml

/public/packs
/public/packs-test
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ end
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

gem 'bootstrap-sass', '~> 3.4', '>= 3.4.1'
gem 'font-awesome-sass', '~> 5.13'
gem 'font-awesome-sass', '~> 5.13'
gem 'carrierwave', '~> 2.1'
gem 'cloudinary', '~> 1.14'
36 changes: 36 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ GEM
ast (2.4.0)
autoprefixer-rails (9.7.6)
execjs
aws_cf_signer (0.1.3)
bindex (0.8.1)
bootsnap (1.4.6)
msgpack (~> 1.0)
Expand All @@ -77,11 +78,23 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
carrierwave (2.1.0)
activemodel (>= 5.0.0)
activesupport (>= 5.0.0)
addressable (~> 2.6)
image_processing (~> 1.1)
mimemagic (>= 0.3.0)
mini_mime (>= 0.1.3)
childprocess (3.0.0)
cloudinary (1.14.0)
aws_cf_signer
rest-client
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.5)
diff-lcs (1.3)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
erubi (1.9.0)
execjs (2.7.0)
factory_bot (5.2.0)
Expand All @@ -94,8 +107,14 @@ GEM
sassc (>= 1.11)
globalid (0.4.2)
activesupport (>= 4.2.0)
http-accept (1.7.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (1.8.3)
concurrent-ruby (~> 1.0)
image_processing (1.11.0)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
jbuilder (2.10.0)
activesupport (>= 5.0.0)
listen (3.2.1)
Expand All @@ -109,11 +128,16 @@ GEM
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0512)
mimemagic (0.3.5)
mini_magick (4.10.1)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.14.1)
msgpack (1.3.3)
netrc (0.11.0)
nio4r (2.5.2)
nokogiri (1.10.9)
mini_portile2 (~> 2.4.0)
Expand Down Expand Up @@ -161,6 +185,11 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (1.7.1)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.4)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
Expand Down Expand Up @@ -191,6 +220,8 @@ GEM
rubocop-ast (0.0.3)
parser (>= 2.7.0.1)
ruby-progressbar (1.10.1)
ruby-vips (2.0.17)
ffi (~> 1.9)
rubyzip (2.3.0)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
Expand Down Expand Up @@ -226,6 +257,9 @@ GEM
turbolinks-source (5.2.0)
tzinfo (1.2.7)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
unicode-display_width (1.7.0)
web-console (4.0.2)
actionview (>= 6.0.0)
Expand All @@ -251,6 +285,8 @@ DEPENDENCIES
bootstrap-sass (~> 3.4, >= 3.4.1)
byebug
capybara (~> 3.32, >= 3.32.2)
carrierwave (~> 2.1)
cloudinary (~> 1.14)
database_cleaner (~> 1.8, >= 1.8.5)
factory_bot_rails (~> 5.2)
font-awesome-sass (~> 5.13)
Expand Down
31 changes: 12 additions & 19 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
require 'digest/sha1'
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:index, :edit, :update, :destroy,
:following, :followers]
# GET /users
# GET /users.json
before_action :check_configuration, only: [:new]

def check_configuration
render 'configuration_missing' if Cloudinary.config.api_key.blank?
end

def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
def show
end

# GET /users/new
def new
session[:forwarding_url] = login_path
@user = User.new

render layout: "logged_out"
end

# GET /users/1/edit
def edit
end

# POST /users
# POST /users.json
def create
@user = User.new(user_params)
@user = User.new(name: user_params[:name])
uploaded_file = Cloudinary::Uploader.upload(user_params[:gravatar_url])
@user.gravatar_url = uploaded_file['secure_url']

respond_to do |format|
if @user.save
Expand All @@ -40,8 +42,6 @@ def create
end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
Expand All @@ -54,8 +54,6 @@ def update
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
Expand All @@ -69,13 +67,8 @@ def my_time_entries
end

private
# Use callbacks to share common setup or constraints between actions.

def set_user
@user = User.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:name, :gravatar_url)
end
end
1 change: 1 addition & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//= require cloudinary

require("@rails/ujs").start()
require("turbolinks").start()
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class User < ApplicationRecord
has_many :time_entries, inverse_of: 'author', foreign_key: :author_id
has_many :groups
validates :name, presence: true, uniqueness: true
# mount_uploader :gravatar_url, AvatarUploader
end
14 changes: 12 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/solid.js" integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous"></script>
<script defer src="https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js" integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous"></script> </head>
<%# <%= javascript_include_tag("jquery.ui.widget", "jquery.iframe-transport",
"jquery.fileupload", "jquery.cloudinary") %> %>
<%# <script defer src="https://use.fontawesome.com/releases/v5.0.13/js/solid.js" integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous"></script> %>
<%# <script defer src="https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js" integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous"></script> </head> %>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<body>
<% flash.each do |message_type, message| %>
Expand Down Expand Up @@ -55,7 +57,15 @@
$('#sidebarCollapse').on('click', function () {
$('#sidebar').toggleClass('active');
});

$(function() {
if($.fn.cloudinary_fileupload !== undefined) {
$("input.cloudinary-fileupload[type=file]").cloudinary_fileupload();
}
});
});
</script>
<%= cloudinary_js_config %>

</body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/logged_out.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
<% end %>
<%= yield %>
</div>
<%= cloudinary_js_config %>
</body>
</html>
9 changes: 6 additions & 3 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<div class="row text-center">
<p><%= @r %></p>
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: user, local: true, class: 'form') do |form| %>
<%= form.hidden_field(:gravatar_url_cache) %>

<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
Expand All @@ -16,7 +19,7 @@
<div class="form-group">
<div class="gravatar_edit">
<%= image_tag('default-profile.png', alt: "default", class: "gravatar") %>
<a href="https://gravatar.com/emails" target="_blank" rel="noopener">Upload picture</a>
<%= form.file_field :gravatar_url, placeholder: 'Upload picture' %>
</div>
</div>

Expand All @@ -25,11 +28,11 @@
</div>

<div class="form-group">
<%= form.email_field :name, class: 'form-control', placeholder: 'Email' %>
<%= form.email_field :email, class: 'form-control', placeholder: 'Email' %>
</div>

<div class="form-group">
<%= form.password_field :name, class: 'form-control', placeholder: 'Password' %>
<%= form.password_field :password, class: 'form-control', placeholder: 'Password' %>
</div>

<div class="actions">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/10/user6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/11/user7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/3/download.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/5/user1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/6/user2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/7/user3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/8/user4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/gravatar_url/9/user5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 81f7a8a

Please sign in to comment.