diff options
| -rw-r--r-- | app/controllers/my/avatars_controller.rb | 33 | ||||
| -rw-r--r-- | app/services/application/handlers/add_to_favorites.rb | 2 | ||||
| -rw-r--r-- | app/services/application/upload_avatar.rb | 32 | ||||
| -rw-r--r-- | spec/controllers/my/avatars_controller_spec.rb | 2 |
4 files changed, 34 insertions, 35 deletions
diff --git a/app/controllers/my/avatars_controller.rb b/app/controllers/my/avatars_controller.rb index 76e387b2..01bccd36 100644 --- a/app/controllers/my/avatars_controller.rb +++ b/app/controllers/my/avatars_controller.rb @@ -1,12 +1,43 @@ module My class AvatarsController < BaseController + def initialize(bus = Spank::IOC.resolve(:command_bus)) + @bus = bus + end + def new @avatar = current_user.avatar || Photo.new end def create - UploadAvatar.new.run(current_user, params[:photo][:image]) + publish(params[:photo][:image]) redirect_to new_my_avatar_path, notice: t(:avatar_uploaded) end + + private + + def publish(image) + @bus.publish(:upload_avatar, create_message_from(image)) + end + + def create_message_from(image) + { + user_id: current_user.id, + file_path: move_to_temporary_storage(image), + original_filename: image.original_filename, + content_type: image.content_type, + } + end + + def move_to_temporary_storage(image) + "#{create_tmp_dir}/#{image.original_filename}".tap do |new_path| + FileUtils.mv(image.path, new_path) + end + end + + def create_tmp_dir + Rails.root.join("tmp/uploads/#{SecureRandom.uuid}").tap do |directory| + system "mkdir -p #{directory}" + end + end end end diff --git a/app/services/application/handlers/add_to_favorites.rb b/app/services/application/handlers/add_to_favorites.rb index 3e671903..b43bba15 100644 --- a/app/services/application/handlers/add_to_favorites.rb +++ b/app/services/application/handlers/add_to_favorites.rb @@ -1,6 +1,6 @@ class AddToFavorites def handles?(event) - :add_cake_to_favorites + :add_cake_to_favorites == event end def handle(message) diff --git a/app/services/application/upload_avatar.rb b/app/services/application/upload_avatar.rb deleted file mode 100644 index 52a3531a..00000000 --- a/app/services/application/upload_avatar.rb +++ /dev/null @@ -1,32 +0,0 @@ -class UploadAvatar - def initialize(command_bus = Spank::IOC.resolve(:command_bus)) - @command_bus = command_bus - end - - def run(user, image) - @command_bus.publish(:upload_avatar, create_message_from(user, image)) - end - - private - - def create_message_from(user, image) - { - user_id: user.id, - file_path: move_to_temporary_storage(image), - original_filename: image.original_filename, - content_type: image.content_type, - } - end - - def move_to_temporary_storage(image) - "#{create_tmp_dir}/#{image.original_filename}".tap do |new_path| - FileUtils.mv(image.path, new_path) - end - end - - def create_tmp_dir - Rails.root.join("tmp/uploads/#{SecureRandom.uuid}").tap do |directory| - system "mkdir -p #{directory}" - end - end -end diff --git a/spec/controllers/my/avatars_controller_spec.rb b/spec/controllers/my/avatars_controller_spec.rb index 050c5e84..f8418009 100644 --- a/spec/controllers/my/avatars_controller_spec.rb +++ b/spec/controllers/my/avatars_controller_spec.rb @@ -23,7 +23,7 @@ describe My::AvatarsController do end it "should display a flash notice" do - flash[:notice].should_not be_nil + expect(flash[:notice]).to_not be_nil end end end |
