Commit 980df161
Changed files (4)
app
controllers
models
services
application
handlers
config
initializers
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
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)
app/services/application/handlers/add_to_favorites.rb
@@ -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
config/initializers/container.rb
@@ -4,7 +4,6 @@ class ConfigureContainerCommand
container.register(:message_handler) { |builder| builder.build(PublishCakeToTwitter) }
container.register(:message_handler) { |builder| builder.build(ProcessPhoto) }
container.register(:message_handler) { |builder| builder.build(ProcessAvatar) }
- container.register(:message_handler) { |builder| builder.build(AddToFavorites) }
container.register(:queue) { |c| Delayed::Job }
container.register(:command_bus) { |c| c.build(CommandBus) }.as_singleton
container.register(:exif_parser) { |builder| ExifParser.new }