Skip to content

Instantly share code, notes, and snippets.

@skwp
Last active December 15, 2015 18:09

Revisions

  1. skwp revised this gist Apr 3, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions watch_listing.rb
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ def self.watch(user, product, listener)
    if product.owner?(user)
    listener.failure(I18n.t('flash.watchlist.error_own'))
    else
    Reverb::Analytics.track(user, :watch_product) # FIXME, this doesn't belong here
    user.user_watch_products.create(:product_id => product.id)
    listener.success
    end
  2. skwp renamed this gist Apr 3, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. skwp revised this gist Apr 3, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions watch_listing.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    class Reverb::Actions::WatchListing
    def self.watch(user, product, listener)
    if product.owner?(user)
    listener.failure(I18n.t('flash.watchlist.error_own'))
    else
    user.user_watch_products.create(:product_id => product.id)
    listener.success
    end
    end
    end
  4. skwp revised this gist Apr 3, 2013. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions wants_api.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    module Reverb
    class Wants < Grape::API
    post '/wants' do
    class WatchListingResponder < SimpleDelegator
    def success
    {"success" => true}
    end

    def failure(message)
    error!({ "error" => message}, 412)
    end
    end

    Reverb::Actions::WatchListing.watch(current_user, Product.find(params[:id]), WatchListingResponder.new(self))
    end
    end
    end
  5. skwp revised this gist Apr 3, 2013. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions watched_products_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    class Dashboard::Buying::WatchedProductsController < Dashboard::BaseController

    def create
    product = Product.find(params[:id])
    Reverb::Actions::WatchListing.watch(current_user, product, WatchListingResponder.new(self))
    redirect_to product_url(product)
    end

    private

    class WatchListingResponder < SimpleDelegator
    def success
    flash[:success] = "Listing has been added to #{link_to_watchlist}."
    end

    def failure(message)
    flash[:error] = message
    end

    def link_to_watchlist
    view_context.link_to 'your watchlist', dashboard_buying_watched_products_url
    end
    end
    end
  6. skwp created this gist Apr 3, 2013.
    27 changes: 27 additions & 0 deletions watched_products_controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    describe Dashboard::Buying::WatchedProductsController do
    stub_user

    let(:product) { mock('product', :id => 1) }

    before { Product.stub(:find).with("1") { product } }

    describe "#create" do
    let(:responder) { Dashboard::Buying::WatchedProductsController::WatchListingResponder.new(subject) }

    it "watches the listing" do
    Reverb::Actions::WatchListing.should_receive(:watch).with(user, product, anything)
    post :create, :id => product.id
    response.should redirect_to product_url(product)
    end

    it "handles error display" do
    responder.failure("foo")
    flash[:error].should == "foo"
    end

    it "handles success display" do
    responder.success
    flash[:success].should =~ /Listing has been added to.*your watchlist.*/
    end
    end
    end