-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
268 part 1: staff can upload profile pic (#276)
* Add model changes and tests * Add form field, and changes into the controller * change naming from Picture to Avatar * Run standard fix * Move avatarable tests to shared module
- Loading branch information
1 parent
c7e68e7
commit 3068017
Showing
7 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Avatarable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_one_attached :avatar | ||
|
||
validates :avatar, content_type: {in: ["image/png", "image/jpeg"], | ||
message: "must be PNG or JPEG"}, | ||
size: {between: 10.kilobyte..1.megabytes, | ||
message: "size must be between 10kb and 1Mb"} | ||
end | ||
|
||
def append_avatar=(attachable) | ||
avatar.attach(attachable) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<% if resource.avatar.attached? %> | ||
<div class='form-group mt-3'> | ||
<label class="form-label"> | ||
Current picture | ||
</label> | ||
<div class='d-flex flex-column align-items-center'> | ||
<%= image_tag resource.avatar, class: 'uploaded-image rounded mt-2 mb-2' %> | ||
<%= link_to t('general.delete'), | ||
purge_attachment_path(resource.avatar.id), | ||
data: { turbo_method: "delete" }, | ||
class: 'delete-button' %> | ||
</div> | ||
</div> | ||
<% else %> | ||
<div class='form-group mt-3'> | ||
<%= form.file_field :append_avatar, | ||
class: "custom-attachments", | ||
label: 'Attach images' %> | ||
|
||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module AvatarableSharedTests | ||
def self.included spec | ||
spec.class_eval do | ||
context "avatarable" do | ||
should "behave as avatarable" do | ||
assert_includes User.included_modules, Avatarable | ||
|
||
assert subject, respond_to?(:append_avatar=) | ||
end | ||
|
||
context "validations" do | ||
should "append error if avatar is too big" do | ||
fixture_file.stubs(:size).returns(2.megabytes) | ||
|
||
subject.avatar.attach(io: fixture_file, filename: "test.png") | ||
|
||
refute subject.valid? | ||
assert_includes subject.errors[:avatar], "size must be between 10kb and 1Mb" | ||
end | ||
|
||
should "append error if avatar is too small" do | ||
fixture_file.stubs(:size).returns(1.kilobyte) | ||
|
||
subject.avatar.attach(io: fixture_file, filename: "test.png") | ||
|
||
refute subject.valid? | ||
assert_includes subject.errors[:avatar], "size must be between 10kb and 1Mb" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |