diff options
| author | mo khan <mo@mokhan.ca> | 2014-10-16 20:35:28 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-10-16 20:35:28 -0600 |
| commit | f2eef36abce2148fe044b6dbda85fadf77754019 (patch) | |
| tree | 33f25a21fd7487cafbad6578e057d71668bb1797 | |
| parent | f3e8563c45ee3a11211428acda1b8305e077545b (diff) | |
publish a command to add a favorites to a users collection as a background job.
| -rw-r--r-- | app/controllers/favorites_controller.rb | 17 | ||||
| -rw-r--r-- | app/services/application/add_to_favorites.rb | 21 | ||||
| -rw-r--r-- | app/services/application/handlers/add_to_favorites.rb | 14 | ||||
| -rw-r--r-- | config/initializers/container.rb | 1 | ||||
| -rw-r--r-- | spec/controllers/favorites_controller_spec.rb | 29 |
5 files changed, 31 insertions, 51 deletions
diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index d284a8b2..b3986845 100644 --- a/app/controllers/favorites_controller.rb +++ b/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 diff --git a/app/services/application/add_to_favorites.rb b/app/services/application/add_to_favorites.rb deleted file mode 100644 index 3cd50409..00000000 --- a/app/services/application/add_to_favorites.rb +++ /dev/null @@ -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 diff --git a/app/services/application/handlers/add_to_favorites.rb b/app/services/application/handlers/add_to_favorites.rb new file mode 100644 index 00000000..3e671903 --- /dev/null +++ b/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 diff --git a/config/initializers/container.rb b/config/initializers/container.rb index f4433439..dc5a01d2 100644 --- a/config/initializers/container.rb +++ b/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 } diff --git a/spec/controllers/favorites_controller_spec.rb b/spec/controllers/favorites_controller_spec.rb index 7bac296f..063318fa 100644 --- a/spec/controllers/favorites_controller_spec.rb +++ b/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 |
