Skip to content

Commit 8b82ba4

Browse files
author
Vladimir Bazhanov
authored
Shrine added; Avatar feature added (#576)
* Shrine added; Avatar feature added
1 parent b6f380b commit 8b82ba4

File tree

17 files changed

+194
-47
lines changed

17 files changed

+194
-47
lines changed

.gitignore

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
._*
12
.bundle
3+
.env
4+
.pryrc
5+
.sass-cache
6+
coverage/*
27
db/*.sqlite3
8+
doc/**/*
39
log/*.log
4-
tmp/*
5-
coverage/*
10+
public/uploads
611
rerun.txt
7-
doc/**/*
12+
tmp/*
813
vendor/bundle
914
vendor/cache
1015
vendor/ruby
11-
.sass-cache
12-
._*
13-
.env
14-
.pryrc

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Style/StringMethods:
6464
Metrics/LineLength:
6565
Max: 120
6666

67+
Metrics/MethodLength:
68+
Max: 16
69+
6770
Metrics/BlockLength:
6871
Enabled: false
6972

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ gem "rails", "5.2.4.3"
77

88
# assets
99
gem "autoprefixer-rails"
10+
gem "aws-sdk-s3"
1011
gem "coffee-rails"
1112
gem "foundation-icons-sass-rails"
1213
gem "foundation-rails"
14+
gem "image_processing"
1315
gem "premailer-rails"
1416
gem "sass-rails", "~> 5.0.0", ">= 5.0.6"
1517
gem "uglifier", ">= 2.7.2"
@@ -45,6 +47,7 @@ gem "rack-mini-profiler", require: false, git: "https://github.com/MiniProfiler/
4547
gem "responders"
4648
gem "rollbar"
4749
gem "seedbank"
50+
gem "shrine"
4851
gem "stackprof"
4952

5053
group :staging, :production do

Gemfile.lock

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ GEM
6464
autoprefixer-rails (6.7.4)
6565
execjs
6666
awesome_print (1.7.0)
67+
aws-eventstream (1.1.0)
68+
aws-partitions (1.327.0)
69+
aws-sdk-core (3.98.0)
70+
aws-eventstream (~> 1, >= 1.0.2)
71+
aws-partitions (~> 1, >= 1.239.0)
72+
aws-sigv4 (~> 1.1)
73+
jmespath (~> 1.0)
74+
aws-sdk-kms (1.33.0)
75+
aws-sdk-core (~> 3, >= 3.71.0)
76+
aws-sigv4 (~> 1.1)
77+
aws-sdk-s3 (1.67.1)
78+
aws-sdk-core (~> 3, >= 3.96.1)
79+
aws-sdk-kms (~> 1)
80+
aws-sigv4 (~> 1.1)
81+
aws-sigv4 (1.1.4)
82+
aws-eventstream (~> 1.0, >= 1.0.2)
6783
babel-source (5.8.35)
6884
babel-transpiler (0.7.0)
6985
babel-source (>= 4.0, < 6)
@@ -81,13 +97,14 @@ GEM
8197
bundler (>= 1.2.0, < 3)
8298
thor (~> 0.18)
8399
byebug (9.0.6)
84-
capybara (2.12.1)
100+
capybara (3.32.2)
85101
addressable
86-
mime-types (>= 1.16)
87-
nokogiri (>= 1.3.3)
88-
rack (>= 1.0.0)
89-
rack-test (>= 0.5.4)
90-
xpath (~> 2.0)
102+
mini_mime (>= 0.1.3)
103+
nokogiri (~> 1.8)
104+
rack (>= 1.6.0)
105+
rack-test (>= 0.6.3)
106+
regexp_parser (~> 1.5)
107+
xpath (~> 3.2)
91108
choice (0.2.0)
92109
cliver (0.3.2)
93110
codeclimate-test-reporter (0.6.0)
@@ -105,6 +122,7 @@ GEM
105122
execjs
106123
json
107124
concurrent-ruby (1.1.6)
125+
content_disposition (1.0.0)
108126
crack (0.4.3)
109127
safe_yaml (~> 1.0.0)
110128
crass (1.0.6)
@@ -127,6 +145,8 @@ GEM
127145
dotenv-rails (2.7.5)
128146
dotenv (= 2.7.5)
129147
railties (>= 3.2, < 6.1)
148+
down (5.1.1)
149+
addressable (~> 2.5)
130150
draper (4.0.1)
131151
actionpack (>= 5.0)
132152
activemodel (>= 5.0)
@@ -149,7 +169,7 @@ GEM
149169
ffi (1.9.25)
150170
flamegraph (0.9.5)
151171
formatador (0.2.5)
152-
formulaic (0.4.0)
172+
formulaic (0.4.1)
153173
activesupport
154174
capybara
155175
i18n
@@ -184,6 +204,9 @@ GEM
184204
htmlentities (4.3.4)
185205
i18n (0.9.5)
186206
concurrent-ruby (~> 1.0)
207+
image_processing (1.11.0)
208+
mini_magick (>= 4.9.5, < 5)
209+
ruby-vips (>= 2.0.17, < 3)
187210
inky-rb (1.3.7.5)
188211
foundation_emails (~> 2)
189212
nokogiri
@@ -195,6 +218,7 @@ GEM
195218
rake
196219
jasmine-core (3.5.0)
197220
jasmine-jquery-rails (2.0.3)
221+
jmespath (1.4.0)
198222
json (2.0.3)
199223
kaminari (1.2.1)
200224
activesupport (>= 4.1.0)
@@ -232,6 +256,8 @@ GEM
232256
mime-types-data (~> 3.2015)
233257
mime-types-data (3.2020.0512)
234258
mimemagic (0.3.5)
259+
mini_magick (4.10.1)
260+
mini_mime (1.0.2)
235261
mini_portile2 (2.4.0)
236262
minitest (5.14.1)
237263
msgpack (1.3.3)
@@ -265,7 +291,7 @@ GEM
265291
coderay (~> 1.1.0)
266292
method_source (~> 0.8.1)
267293
slop (~> 3.4)
268-
public_suffix (4.0.1)
294+
public_suffix (4.0.5)
269295
puma (4.3.5)
270296
nio4r (~> 2.0)
271297
pundit (1.1.0)
@@ -315,6 +341,7 @@ GEM
315341
rb-fsevent (0.9.8)
316342
rb-inotify (0.9.8)
317343
ffi (>= 0.5.0)
344+
regexp_parser (1.7.1)
318345
request_store (1.5.0)
319346
rack (>= 1.4)
320347
responders (3.0.0)
@@ -357,6 +384,8 @@ GEM
357384
rubocop (>= 0.42.0)
358385
ruby-graphviz (1.2.2)
359386
ruby-progressbar (1.8.1)
387+
ruby-vips (2.0.17)
388+
ffi (~> 1.9)
360389
ruby_dep (1.5.0)
361390
safe_yaml (1.0.4)
362391
sass (3.4.25)
@@ -374,6 +403,9 @@ GEM
374403
shellany (0.0.1)
375404
shoulda-matchers (4.1.2)
376405
activesupport (>= 4.2.0)
406+
shrine (3.2.1)
407+
content_disposition (~> 1.0)
408+
down (~> 5.1)
377409
simple_form (5.0.0)
378410
actionpack (>= 5.0)
379411
activemodel (>= 5.0)
@@ -440,8 +472,8 @@ GEM
440472
websocket-driver (0.7.2)
441473
websocket-extensions (>= 0.1.0)
442474
websocket-extensions (0.1.5)
443-
xpath (2.1.0)
444-
nokogiri (~> 1.3)
475+
xpath (3.2.0)
476+
nokogiri (~> 1.8)
445477

446478
PLATFORMS
447479
ruby
@@ -450,6 +482,7 @@ DEPENDENCIES
450482
active_link_to
451483
autoprefixer-rails
452484
awesome_print
485+
aws-sdk-s3
453486
bootsnap
454487
brakeman
455488
bullet
@@ -475,6 +508,7 @@ DEPENDENCIES
475508
google-analytics-rails
476509
guard-rspec
477510
health_check
511+
image_processing
478512
inky-rb
479513
interactor
480514
jasmine (> 2.0)
@@ -506,6 +540,7 @@ DEPENDENCIES
506540
scss_lint
507541
seedbank
508542
shoulda-matchers
543+
shrine
509544
simple_form
510545
slim
511546
slim-rails

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class User < ApplicationRecord
2+
include AvatarUploader::Attachment(:avatar)
3+
24
devise :database_authenticatable, :registerable, :confirmable,
35
:recoverable, :rememberable, :trackable, :validatable, :lockable
46

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
class User
22
class ParameterSanitizer < Devise::ParameterSanitizer
3-
USER_PARAMS = %i[
3+
SIGN_UP_PARAMS = %i[
44
full_name
55
email
66
password
77
password_confirmation
88
].freeze
99

10+
ACCOUNT_UPDATE_PARAMS = %i[
11+
full_name
12+
email
13+
avatar
14+
remove_avatar
15+
password
16+
password_confirmation
17+
].freeze
18+
1019
def initialize(*)
1120
super
12-
permit(:sign_up, keys: USER_PARAMS)
13-
permit(:account_update, keys: USER_PARAMS)
21+
permit(:sign_up, keys: SIGN_UP_PARAMS)
22+
permit(:account_update, keys: ACCOUNT_UPDATE_PARAMS)
1423
end
1524
end
1625
end

app/uploaders/avatar_uploader.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AvatarUploader < Shrine
2+
MAX_AVATAR_FILE_SIZE = 5 * 1024 * 1024
3+
4+
Attacher.validate do
5+
validate_size 1..MAX_AVATAR_FILE_SIZE
6+
validate_mime_type %w[image/jpeg image/png]
7+
validate_extension %w[jpg jpeg png]
8+
end
9+
end

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
.columns
33
h2 = title("Edit #{current_user.full_name}")
44

5-
.row
6-
.medium-5.columns
7-
= simple_form_for resource,
8-
as: resource_name,
9-
url: registration_path(resource_name),
10-
html: { method: :put } do |f|
11-
5+
= simple_form_for resource,
6+
as: resource_name,
7+
url: registration_path(resource_name),
8+
html: { method: :put } do |f|
9+
.row
10+
.medium-5.columns
1211
.form-inputs
1312
= f.input :full_name, required: true, autofocus: true
1413
= f.input :email, required: true
@@ -17,17 +16,25 @@
1716
required: false
1817
= f.input :password_confirmation,
1918
required: false
20-
2119
.form-actions
22-
= f.button :submit, "Update"
20+
= f.button :submit, "Update"
21+
22+
.medium-5.columns
23+
.form-inputs
24+
- if resource.avatar
25+
= image_tag resource.avatar_url
26+
27+
= f.input :remove_avatar, as: :boolean
28+
29+
= f.hidden_field :avatar, value: resource.cached_avatar_data
30+
= f.input :avatar
2331

24-
.medium-5.medium-offset-1.columns.end
25-
h6
26-
b Cancel my account
27-
p
28-
| Unhappy?
29-
'
30-
= link_to "Cancel my account.",
31-
registration_path(resource_name),
32-
data: { confirm: "Are you sure?" },
33-
method: :delete
32+
h6
33+
b Cancel my account
34+
p
35+
| Unhappy?
36+
'
37+
= link_to "Cancel my account.",
38+
registration_path(resource_name),
39+
data: { confirm: "Are you sure?" },
40+
method: :delete

config/application.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
require_relative "boot"
22

3-
require "rails/all"
3+
require "active_record/railtie"
4+
require "action_controller/railtie"
5+
require "action_view/railtie"
6+
require "action_mailer/railtie"
7+
require "active_job/railtie"
8+
require "sprockets/railtie"
49

510
# Require the gems listed in Gemfile, including any gems
611
# you've limited to :test, :development, or :production.

config/environments/development.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
config.cache_store = :null_store
2828
end
2929

30-
# Store uploaded files on the local file system (see config/storage.yml for options)
31-
config.active_storage.service = :local
32-
3330
# Don't care if the mailer can't send.
3431
config.action_mailer.raise_delivery_errors = false
3532

config/environments/test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
# Disable request forgery protection in test environment.
2929
config.action_controller.allow_forgery_protection = false
3030

31-
# Store uploaded files on the local file system in a temporary directory
32-
config.active_storage.service = :test
33-
3431
config.action_mailer.perform_caching = false
3532

3633
# Tell Action Mailer not to deliver emails to the real world.

0 commit comments

Comments
 (0)