diff options
| author | mo khan <mo@mokhan.ca> | 2015-01-17 12:15:19 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-01-17 12:15:19 -0700 |
| commit | 980df161d60df14af98733057649cdd6ba98deca (patch) | |
| tree | d1e6f078400a283a486bcbaa8fa309f8d1f2a305 /app | |
| parent | 6dfba2b4603e2711a40c909361057677d65c72b1 (diff) | |
inline handler to add a cake to someones favorites.
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/favorites_controller.rb | 12 | ||||
| -rw-r--r-- | app/models/user.rb | 5 | ||||
| -rw-r--r-- | app/services/application/handlers/add_to_favorites.rb | 14 |
3 files changed, 5 insertions, 26 deletions
diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index a8b56bdc..095cc23a 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -1,11 +1,6 @@ class FavoritesController < ApplicationController before_action :authenticate! - def initialize(command_bus = Spank::IOC.resolve(:command_bus)) - @bus = command_bus - super() - end - def index @creation = Creation.find(params[:cake_id]) @favorites = @creation.favorites @@ -13,11 +8,8 @@ class FavoritesController < ApplicationController def create cake = Creation.find(params[:cake_id]) - bus.publish(:add_cake_to_favorites, { user_id: current_user.id, cake_id: cake.id }) + current_user.add_favorite(cake) + redirect_to cake_path(cake), notice: "This has been added to your favorites" end - - private - - attr_reader :bus end diff --git a/app/models/user.rb b/app/models/user.rb index e82edb1d..8594e9bb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,8 +18,9 @@ class User < ActiveRecord::Base has_one :avatar, class_name: 'Photo', as: :imageable acts_as_tagger - def add_favorite(creation) - creation.liked_by(self) + def add_favorite(cake) + return if self.owns(cake) + cake.liked_by(self) end def already_likes(creation) diff --git a/app/services/application/handlers/add_to_favorites.rb b/app/services/application/handlers/add_to_favorites.rb deleted file mode 100644 index b43bba15..00000000 --- a/app/services/application/handlers/add_to_favorites.rb +++ /dev/null @@ -1,14 +0,0 @@ -class AddToFavorites - def handles?(event) - :add_cake_to_favorites == event - end - - def handle(message) - cake = Creation.find(message[:cake_id]) - user = User.find(message[:user_id]) - return if user.owns(cake) - - favorite = user.add_favorite(cake) - favorite.save - end -end |
