Skip to content

Commit

Permalink
268 part 1: staff can upload profile pic (#276)
Browse files Browse the repository at this point in the history
* 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
yuricarvalhop authored Oct 26, 2023
1 parent c7e68e7 commit 3068017
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def account_update_params
:password,
:password_confirmation,
:signup_role,
:current_password)
:current_password,
:append_avatar)
end

def after_sign_up_path_for(resource)
Expand Down
16 changes: 16 additions & 0 deletions app/models/concerns/avatarable.rb
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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
# index_users_on_reset_password_token (reset_password_token) UNIQUE
#
class User < ApplicationRecord
include Avatarable

acts_as_tenant(:organization)
default_scope do
#
Expand Down
2 changes: 2 additions & 0 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
<i>(we need your current password to confirm your changes)</i><br />
</div>

<%= render partial: 'partials/avatarable_form', locals: { resource: resource, form: f } %>

<div class="actions">
<%= f.submit "Update", class: 'btn btn-outline-dark mt-3 mb-3',
data: { turbo: false } %>
Expand Down
21 changes: 21 additions & 0 deletions app/views/partials/_avatarable_form.html.erb
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 %>
14 changes: 14 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require "test_helper"
require "shared/avatarable_shared_tests"

class UserTest < ActiveSupport::TestCase
include AvatarableSharedTests

context "associations" do
should have_one(:staff_account).dependent(:destroy)
should have_one(:adopter_account).dependent(:destroy)
Expand Down Expand Up @@ -32,4 +35,15 @@ class UserTest < ActiveSupport::TestCase
assert_not_includes User.organization_staff(organization.id), user
end
end

private

def fixture_file
@fixture_file ||= load_file
end

def load_file
fixture_path = File.join(Rails.root, "test", "fixtures", "files", "logo.png")
File.open(fixture_path)
end
end
35 changes: 35 additions & 0 deletions test/shared/avatarable_shared_tests.rb
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

0 comments on commit 3068017

Please sign in to comment.