Commit f2eef36a
Changed files (5)
app
controllers
services
application
handlers
config
initializers
spec
controllers
app/controllers/favorites_controller.rb
@@ -1,20 +1,23 @@
class FavoritesController < ApplicationController
before_action :authenticate!
+ def initialize(command_bus = Spank::IOC.resolve(:command_bus))
+ @bus = command_bus
+ super()
+ end
+
def index
@creation = FindCreationQuery.new.fetch(params[:creation_id])
@favorites = @creation.favorites
end
def create
- AddToFavorites.new(self).run(params[:creation_id])
+ cake = Creation.find(params[:creation_id])
+ bus.publish(:add_cake_to_favorites, { user_id: current_user.id, cake_id: cake.id })
+ redirect_to cake, notice: "This has been added to your favorites"
end
- def favorite_created(cake, message)
- redirect_to cake, notice: message
- end
+ private
- def create_favorite_failed(cake)
- redirect_to cake
- end
+ attr_reader :bus
end
app/services/application/handlers/add_to_favorites.rb
@@ -0,0 +1,14 @@
+class AddToFavorites
+ def handles?(event)
+ :add_cake_to_favorites
+ 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
app/services/application/add_to_favorites.rb
@@ -1,21 +0,0 @@
-class AddToFavorites
- def initialize(context, current_user = context.current_user)
- @context = context
- @current_user = current_user
- end
-
- def run(creation_id)
- cake = Creation.find(creation_id)
- if @current_user.owns(cake)
- @context.favorite_created(cake, "You can't favorite your own stuff")
- return
- end
-
- favorite = @current_user.add_favorite(cake)
- if favorite.save
- @context.favorite_created(cake, 'Welcome to the fanclub!')
- else
- @context.create_favorite_failed
- end
- end
-end
config/initializers/container.rb
@@ -4,6 +4,7 @@ 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 }
spec/controllers/favorites_controller_spec.rb
@@ -13,47 +13,30 @@ describe FavoritesController do
before :each do
creation.favorites << favorite
creation.save!
- get :index, :creation_id => creation.id
+ get :index, creation_id: creation.id
end
it "should return them all" do
- assigns(:favorites).should include favorite
+ expect(assigns(:favorites)).to include(favorite)
end
end
context "when adding a cake to your favorites" do
before :each do
- post :create, :creation_id => creation.id
+ post :create, creation_id: creation.id
end
it "should add the cake to the logged in users favorites" do
- user.reload.favorites.count == 1
+ expect(user.reload.favorites.count).to eql(1)
end
it "should redirect to the cake detail page" do
- response.should redirect_to(creation)
+ expect(response).to redirect_to(creation)
end
it "should include a friendly flash message" do
- flash[:notice].should_not be_nil
- end
- end
-
- context "when trying to add your own cake to your favorites" do
- before :each do
- creation.user = user
- creation.save!
- post :create, :creation_id => creation.id
- end
-
- it "should not let you" do
- flash[:notice].should == "You can't favorite your own stuff"
- end
-
- it "should redirect you to the creation" do
- response.should redirect_to(creation)
+ expect(flash[:notice]).to_not be_nil
end
end
end
-
end