diff --git a/app/assets/images/guest.png b/app/assets/images/guest.png new file mode 100644 index 0000000..c700f02 Binary files /dev/null and b/app/assets/images/guest.png differ diff --git a/app/assets/javascripts/orders.js b/app/assets/javascripts/orders.js index f6e0f01..00041f6 100644 --- a/app/assets/javascripts/orders.js +++ b/app/assets/javascripts/orders.js @@ -52,7 +52,7 @@ ready = function() { barcode = $(this).find("input[type=number]").val(); $("#from_barcode_form")[0].reset(); $.ajax({ - url: $("#from_barcode_form").data("url") + barcode, + url: $("#from_barcode_form").data("url") + "/" + barcode, success: function(data) { if (data != null) { increment_product(data["id"]); diff --git a/app/controllers/guest/orders_controller.rb b/app/controllers/guest/orders_controller.rb new file mode 100644 index 0000000..718ebc9 --- /dev/null +++ b/app/controllers/guest/orders_controller.rb @@ -0,0 +1,9 @@ +class Guest::OrdersController < OrdersController + before_action :load_guest + + private + + def load_guest + @user = User.guest + end +end diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index b757ef8..2516cdf 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -9,7 +9,7 @@ class OrdersController < ApplicationController def create if @order.save - flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!" + flash[:success] = @order.flash_success redirect_to root_path else @products = Product.all.for_sale.order(:name) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 78743ec..f02d73d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,6 @@ module ApplicationHelper + include ActionView::Helpers::NumberHelper + def bootstrap_class_for flash_type { success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type] || flash_type.to_s end diff --git a/app/models/order.rb b/app/models/order.rb index 7132e8f..52935f9 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -11,7 +11,7 @@ # class Order < ActiveRecord::Base - include ActionView::Helpers::TextHelper + include ActionView::Helpers::TextHelper, ApplicationHelper belongs_to :user, counter_cache: true has_many :order_items, dependent: :destroy @@ -19,7 +19,7 @@ class Order < ActiveRecord::Base before_validation :calculate_price before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } } - after_create :create_api_job + after_create :create_api_job, unless: -> { user.name == "Guest" } validates :user, presence: true validates_associated :order_items @@ -33,6 +33,12 @@ class Order < ActiveRecord::Base }.to_sentence end + def flash_success + f = "#{to_sentence} ordered." + f << " Please put #{euro_from_cents(price_cents)} in our pot!" if user.name == "Guest" + f << " Enjoy it!" + end + def deletable self.created_at > Rails.application.config.call_api_after.ago end diff --git a/app/models/user.rb b/app/models/user.rb index ddab635..e340ecc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,4 +55,10 @@ class User < ActiveRecord::Base rescue end end + + def self.guest + @guest || find_or_create_by(name: "Guest") do |user| + user.avatar = File.new(File.join("app", "assets", "images", "guest.png")) + end + end end diff --git a/app/views/orders/overview.html.haml b/app/views/orders/overview.html.haml index 053fb78..a6d3d3d 100644 --- a/app/views/orders/overview.html.haml +++ b/app/views/orders/overview.html.haml @@ -7,5 +7,7 @@ - @last.each do |user| = image_tag user.avatar(:small), class: "img-responsive img-circle img-thumbnail" .col-md-11 + .row.text-center + =link_to "Not a Zeus Account? Order as a guest.", new_guest_order_path, class: "btn btn-primary btn-lg" .row = render @users diff --git a/config/initializers/errbit.rb b/config/initializers/errbit.rb index f7037bf..bd4049c 100644 --- a/config/initializers/errbit.rb +++ b/config/initializers/errbit.rb @@ -1,6 +1,6 @@ Airbrake.configure do |config| config.api_key = '6a6aacac3413e9c7b49b18c913477e4a' - config.host = 'zeus-errbit.ilion.me' + config.host = 'errbit.awesomepeople.tv' config.port = 80 config.secure = config.port == 443 end diff --git a/config/routes.rb b/config/routes.rb index 1495ef8..1663591 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,4 +36,8 @@ Rails.application.routes.draw do resources :barcodes, only: [:show, :index, :destroy] get 'overview' => 'orders#overview', as: "orders" + + namespace :guest do + resources :orders, only: [:new, :create] + end end diff --git a/spec/controllers/barcodes_controller_spec.rb b/spec/controllers/barcodes_controller_spec.rb new file mode 100644 index 0000000..90c10c9 --- /dev/null +++ b/spec/controllers/barcodes_controller_spec.rb @@ -0,0 +1,72 @@ +# product_barcodes POST /products/:product_id/barcodes(.:format) barcodes#create +# barcodes GET /barcodes(.:format) barcodes#index +# barcode GET /barcodes/:id(.:format) barcodes#show +# DELETE /barcodes/:id(.:format) barcodes#destroy +# + +describe BarcodesController, type: :controller do + before :each do + @product = create :product + @admin = create :admin + sign_in @admin + end + + ########## + # POST # + ########## + + describe 'POST create' do + context 'successful' do + it 'should create a barcode' do + expect{ + post :create, product_id: @product, barcode: attributes_for(:barcode) + }.to change{ Barcode.count }.by(1) + end + end + + context 'failed' do + it 'should not create a barcode' do + expect{ + post :create, product_id: @product, barcode: attributes_for(:invalid_barcode) + }.to_not change{ Barcode.count } + end + end + end + + ########### + # INDEX # + ########### + + describe 'GET index' do + it 'should load all the barcodes' do + barcode = create :barcode + get :index + expect(assigns(:barcodes)).to eq([barcode]) + end + end + + ########## + # SHOW # + ########## + + describe 'GET show' do + before :each do + @barcode = create :barcode + end + + it 'should load the correct barcode' do + get :show, id: @barcode + expect(assigns(:barcode)).to eq(@barcode) + end + + it 'should allow friendly id' do + get :show, id: @barcode.code + expect(assigns(:barcode)).to eq(@barcode) + end + + it 'should respond with this barcode' do + get :show, id: @barcode + expect(response.body).to eq @barcode.product.to_json + end + end +end diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb index 35bb79d..ff9e416 100644 --- a/spec/controllers/products_controller_spec.rb +++ b/spec/controllers/products_controller_spec.rb @@ -13,12 +13,12 @@ describe ProductsController, type: :controller do sign_in @admin end - ########## - # POST # - ########## + ############ + # CREATE # + ############ describe 'POST create' do - context 'successfull' do + context 'successful' do it 'should create a product' do expect{ post :create, product: attributes_for(:product) @@ -35,7 +35,7 @@ describe ProductsController, type: :controller do it 'should not create a product' do expect{ post :create, product: attributes_for(:invalid_product) - }.to_not change{Product.count} + }.to_not change{ Product.count} end it 'should render form' do diff --git a/spec/factories/barcodes.rb b/spec/factories/barcodes.rb index 2bada88..eb06c96 100644 --- a/spec/factories/barcodes.rb +++ b/spec/factories/barcodes.rb @@ -13,5 +13,9 @@ FactoryGirl.define do factory :barcode do product sequence :code + + factory :invalid_barcode do + code nil + end end end