Merge branch 'master' into tests

This commit is contained in:
benji 2015-12-01 21:07:51 +01:00
commit 4f67230258
13 changed files with 115 additions and 10 deletions

BIN
app/assets/images/guest.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View file

@ -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"]);

View file

@ -0,0 +1,9 @@
class Guest::OrdersController < OrdersController
before_action :load_guest
private
def load_guest
@user = User.guest
end
end

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -13,5 +13,9 @@ FactoryGirl.define do
factory :barcode do
product
sequence :code
factory :invalid_barcode do
code nil
end
end
end