Commit 098c823c
Changed files (6)
app
controllers
services
application
handlers
views
my
avatars
shared
config
app/controllers/my/avatars_controller.rb
@@ -1,24 +1,12 @@
module My
class AvatarsController < BaseController
- before_action :find_or_build_avatar
-
- def edit
+ def new
+ @avatar = current_user.avatar || Photo.new
end
- def update
+ def create
UploadAvatar.new.run(current_user, params)
- redirect_to edit_my_avatar_path(current_user), :notice => t(:avatar_uploaded)
- end
-
- protected
-
- def find_or_build_avatar
- if current_user.avatar == nil
- @avatar = current_user.avatar = Avatar.new
- current_user.save
- else
- @avatar = current_user.avatar
- end
+ redirect_to new_my_avatar_path, notice: t(:avatar_uploaded)
end
end
end
app/services/application/handlers/process_avatar.rb
@@ -1,5 +1,6 @@
class ProcessAvatar
- def initialize()
+ def initialize(blob_storage)
+ @blob_storage = blob_storage
end
def handles?(event)
@@ -8,18 +9,8 @@ class ProcessAvatar
def handle(message)
user = User.find(message[:user_id])
- #avatar = avatar_for(user)
- end
-
- private
-
- def avatar_for(user)
- if user.avatar.nil?
- avatar = user.avatar = Avatar.new
- user.save
- return avatar
- else
- user.avatar
- end
+ user.avatar = Photo.create!(image_processing: true)
+ user.avatar.upload(message[:file_path], @blob_storage)
+ user.avatar.save!
end
end
app/services/application/upload_avatar.rb
@@ -10,7 +10,7 @@ class UploadAvatar
private
def create_message_from(user, payload)
- image = payload[:avatar][:avatar]
+ image = payload[:photo][:image]
{
user_id: user.id,
file_path: move_to_temporary_storage(image),
app/views/my/avatars/edit.html.erb → app/views/my/avatars/new.html.erb
@@ -4,10 +4,10 @@
</div>
<div class="span10">
<%= avatar_for(current_user) %>
- <%= form_for(@avatar, :url => my_avatar_path(@avatar), :method => :put, :multipart => true, remote: true, authenticity_token: true) do |f| %>
+ <%= form_for(@avatar, url: my_avatars_path, method: :post, :multipart => true, remote: true, authenticity_token: true) do |f| %>
<div class="control-group">
<div class="controls">
- <%= f.file_field :avatar %>
+ <%= f.file_field :image %>
</div>
</div>
<div class="control-group">
config/routes.rb
@@ -86,7 +86,7 @@ Cake::Application.routes.draw do
resources :favorites, only: [:index]
resources :settings, only: [:index, :update]
resources :passwords, only: [:index, :update]
- resources :avatars, only: [:edit, :update]
+ resources :avatars, only: [:new, :create]
root to: "dashboard#index"
end
end