Commit 098c823c

mo khan <mo@mokhan.ca>
2014-09-20 04:18:50
switch up avatar controller to upload a new photo.
1 parent e7db161
Changed files (6)
app
controllers
services
views
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">
app/views/my/shared/_my_nav.html.erb
@@ -1,5 +1,5 @@
 <div class="well sidebar-nav">
-  <%= link_to edit_my_avatar_path(current_user), title: 'Change your avatar', data: { toggle: 'tooltip', placement: 'bottom', animation: 'true' }, class: 'tooltip-item' do %>
+  <%= link_to new_my_avatar_path(current_user), title: 'Change your avatar', data: { toggle: 'tooltip', placement: 'bottom', animation: 'true' }, class: 'tooltip-item' do %>
     <%= avatar_for(current_user) %>
   <% end %>
   <ul class="nav nav-list" data-no-turbolink>
@@ -12,7 +12,7 @@
     <li class="<%= "active" if controller?("favorites") %>"><%= link_to "<i class='fa fa-star'></i> Favorites".html_safe, my_favorites_path %></li>
     <li class="<%= 'active' if controller?('settings') %>"><%= link_to "<i class='fa fa-cog'></i> Settings".html_safe, my_settings_path %></li>
     <li class="<%= 'active' if controller?('passwords') %>"><%= link_to "<i class='fa fa-eye'></i> Password".html_safe, my_passwords_path %></li>
-    <li class="<%= 'active' if controller?('avatars') %>"><%= link_to "<i class='fa fa-picture-o'></i> Picture".html_safe, edit_my_avatar_path(current_user) %></li>
+    <li class="<%= 'active' if controller?('avatars') %>"><%= link_to "<i class='fa fa-picture-o'></i> Picture".html_safe, new_my_avatar_path(current_user) %></li>
     <li class="nav-header">Actions</li>
     <li>
       <%= link_to my_root_path(anchor: 'cakes/new') do %>
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