Commit c04042f8
Changed files (4)
app
controllers
services
application
handlers
spec
controllers
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
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)
app/services/application/upload_avatar.rb
@@ -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
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