Permalink
Browse files

Initial commit.

  • Loading branch information...
stephskardal committed Dec 30, 2011
0 parents commit 75426a5a8d9e096ff547c943057e49cf0f733a0e
Showing with 1,156 additions and 0 deletions.
  1. +5 −0 .document
  2. +48 −0 .gitignore
  3. +13 −0 Gemfile
  4. +18 −0 Gemfile.lock
  5. +19 −0 README.rdoc
  6. +56 −0 Rakefile
  7. +1 −0 VERSION
  8. +29 −0 app/controllers/piggybak/cart_controller.rb
  9. +74 −0 app/controllers/piggybak/orders_controller.rb
  10. +14 −0 app/models/piggybak/address.rb
  11. +12 −0 app/models/piggybak/calculator.rb
  12. +28 −0 app/models/piggybak/calculator/flat_rate.rb
  13. +33 −0 app/models/piggybak/calculator/range.rb
  14. +53 −0 app/models/piggybak/cart.rb
  15. +14 −0 app/models/piggybak/line_item.rb
  16. +42 −0 app/models/piggybak/order.rb
  17. +10 −0 app/models/piggybak/payment.rb
  18. +20 −0 app/models/piggybak/payment_method.rb
  19. +9 −0 app/models/piggybak/payment_method_value.rb
  20. +14 −0 app/models/piggybak/product.rb
  21. +16 −0 app/models/piggybak/shipment.rb
  22. +31 −0 app/models/piggybak/shipping_method.rb
  23. +11 −0 app/models/piggybak/shipping_method_value.rb
  24. +10 −0 app/views/piggybak/cart/_form.html.erb
  25. +25 −0 app/views/piggybak/cart/_items.html.erb
  26. +3 −0 app/views/piggybak/cart/show.html.erb
  27. +30 −0 app/views/piggybak/orders/receipt.html.erb
  28. +94 −0 app/views/piggybak/orders/show.html.erb
  29. +5 −0 app/views/rails_admin/application/_order_detail.html.erb
  30. +7 −0 app/views/rails_admin/application/_shipping_method.html.erb
  31. +10 −0 config/routes.rb
  32. +16 −0 db/migrate/20111227150106_create_orders.rb
  33. +15 −0 db/migrate/20111227150322_create_addresses.rb
  34. +10 −0 db/migrate/20111227150432_create_line_items.rb
  35. +14 −0 db/migrate/20111227213558_create_products.rb
  36. +9 −0 db/migrate/20111228231756_create_shipping_methods.rb
  37. +11 −0 db/migrate/20111228231806_create_payment_methods.rb
  38. +12 −0 db/migrate/20111228231829_create_payments.rb
  39. +12 −0 db/migrate/20111228231838_create_shipments.rb
  40. +9 −0 db/migrate/20111228235852_create_shipping_method_values.rb
  41. +32 −0 lib/acts_as_product/base.rb
  42. +5 −0 lib/application_helper.rb
  43. +162 −0 lib/piggybak.rb
  44. +95 −0 piggybak.gemspec
@@ -0,0 +1,5 @@
lib/**/*.rb
bin/*
-
features/**/*.feature
LICENSE.txt
@@ -0,0 +1,48 @@
# rcov generated
coverage
# rdoc generated
rdoc
# yard generated
doc
.yardoc
# bundler
.bundle
# jeweler generated
pkg
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
# * Create a file at ~/.gitignore
# * Include files you want ignored
# * Run: git config --global core.excludesfile ~/.gitignore
#
# After doing this, these files will be ignored in all your git projects,
# saving you from having to 'pollute' every project you touch with them
#
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
#
# For MacOS:
#
#.DS_Store
# For TextMate
#*.tmproj
#tmtags
# For emacs:
#*~
#\#*
#.\#*
# For vim:
*.swp
# For redcar:
#.redcar
# For rubinius:
#*.rbc
13 Gemfile
@@ -0,0 +1,13 @@
source "http://rubygems.org"
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
# gem "shoulda", ">= 0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.6.4"
gem "rcov", ">= 0"
end
@@ -0,0 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
git (1.2.5)
jeweler (1.6.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
rake (0.9.2.2)
rcov (0.9.11)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.0.0)
jeweler (~> 1.6.4)
rcov
@@ -0,0 +1,19 @@
= piggybak
Description goes here.
== Contributing to piggybak
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
== Copyright
Copyright (c) 2011 Steph Skardal. See LICENSE.txt for
further details.
@@ -0,0 +1,56 @@
# encoding: utf-8
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'rake'
require 'jeweler'
Jeweler::Tasks.new do |gem|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
gem.name = "piggybak"
gem.homepage = "http://github.com/stephskardal/piggybak"
gem.license = "MIT"
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}
gem.email = "steph@endpoint.com"
gem.authors = ["Steph Skardal"]
gem.add_dependency "rails_admin"
gem.add_dependency "devise"
gem.add_dependency "activemerchant"
end
Jeweler::RubygemsDotOrgTasks.new
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
test.rcov_opts << '--exclude "gems/*"'
end
task :default => :test
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "piggybak #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
@@ -0,0 +1 @@
0.1.0
@@ -0,0 +1,29 @@
module Piggybak
class CartController < ApplicationController
def show
@cart = Cart.new(request.cookies["cart"])
end
def add
cookies["cart"] = { :value => Cart.add(request.cookies["cart"], params), :path => '/' }
redirect_to piggybak.cart_url
end
def remove
response.set_cookie("cart", { :value => Cart.remove(request.cookies["cart"], params[:item]), :path => '/' })
redirect_to piggybak.cart_url
end
def clear
cookies["cart"] = { :value => '', :path => '/' }
redirect_to piggybak.cart_url
end
=begin
def update
response.set_cookie("cart", { :value => Cart.update(request.cookies["cart"], params), :path => '/' })
redirect_to cart_url
end
=end
end
end
@@ -0,0 +1,74 @@
module Piggybak
class OrdersController < ApplicationController
def show
@cart = Piggybak::Cart.new(request.cookies["cart"])
@order = Piggybak::Order.new
@order.billing_address ||= Piggybak::Address.new
@order.shipping_address ||= Piggybak::Address.new
@shipping_methods = Piggybak::ShippingMethod.lookup_methods(@cart)
@order.shipments ||= [Piggybak::Shipment.new]
@payment_methods = Piggybak::PaymentMethod.find_all_by_active(true).inject([]) { |arr, b| arr << [b.label, b.id]; arr }
@order.payments ||= [Piggybak::Payment.new]
end
def submit
begin
ActiveRecord::Base.transaction do
@order = Piggybak::Order.new(params[:piggybak_order].merge({:total => 0.0 }))
if @order.save
cart = Piggybak::Cart.new(request.cookies["cart"])
cart.items.each do |item|
Piggybak::LineItem.create({ :order_id => @order.id,
:product_id => item[:product].id,
:total => item[:product].price*item[:quantity],
:quantity => item[:quantity] })
end
shipment = @order.shipments.first
calculator = shipment.shipping_method.klass.constantize
shipment.update_attribute(:total, calculator.rate(shipment.shipping_method, cart))
@order.update_total
payment = @order.payments.first
payment_gateway = payment.payment_method.klass.constantize
params[:credit_card][:first_name] = @order.billing_address.firstname
params[:credit_card][:last_name] = @order.billing_address.lastname
credit_card = ActiveMerchant::Billing::CreditCard.new(params[:credit_card])
if credit_card.valid?
gateway = payment_gateway.new(payment.payment_method.key_values)
gateway_response = gateway.authorize(@order.total*100, credit_card, :address => @order.avs_address)
if gateway_response.success?
gateway.capture(1000, gateway_response.authorization)
cookies["cart"] = { :value => '', :path => '/' }
session[:last_order] = @order.id
redirect_to piggybak.receipt_url
else
raise Exception, gateway_response.message
end
else
raise Exception, "Your credit card was not valid. #{credit_card.errors}"
end
else
raise Exception, @order.errors.full_messages
end
end
rescue Exception => e
@message = e.message
@cart = Piggybak::Cart.new(request.cookies["cart"])
@shipping_methods = Piggybak::ShippingMethod.lookup_methods(@cart)
@payment_methods = Piggybak::PaymentMethod.find_all_by_active(true).inject([]) { |arr, b| arr << [b.klass, b.id]; arr }
render "piggybak/orders/show"
end
end
def receipt
@order = Piggybak::Order.find(session[:last_order])
end
end
end
@@ -0,0 +1,14 @@
module Piggybak
class Address < ActiveRecord::Base
validates_presence_of :firstname
validates_presence_of :lastname
validates_presence_of :address1
validates_presence_of :city
validates_presence_of :state
validates_presence_of :zip
def admin_label
"Address: #{self.id}"
end
end
end
@@ -0,0 +1,12 @@
module Piggybak
class Calculator
def self.available?(*args)
false
end
def self.lookup(*args)
{ :available => false,
:rate => 0.00 }
end
end
end
@@ -0,0 +1,28 @@
module Piggybak
class Calculator::FlatRate < Calculator
def self.description
"Flat Rate"
end
def self.meta_keys
["rate"]
end
def self.available?(method, cart)
method.metadata.collect { |t| t.key }.sort == self.meta_keys
end
def self.rate(method, cart)
method.shipping_method_values.detect { |m| m.key == "rate" }.value
end
def self.lookup(method, cart)
if self.available?(method, cart)
{ :available => self.available?(method, cart),
:rate => self.rate(method, cart) }
else
{ :available => self.available?(method, cart) }
end
end
end
end
@@ -0,0 +1,33 @@
module Piggybak
class Calculator::Range < Calculator
def self.description
"Range"
end
def self.meta_keys
["cost", "lower", "upper"]
end
def self.available?(method, cart)
return false if method.metadata.collect { |t| t.key }.sort != self.meta_keys.sort
low_end = method.shipping_method_values.detect { |m| m.key == "lower" }.value
high_end = method.shipping_method_values.detect { |m| m.key == "upper" }.value
cart.total >= low_end.to_f && cart.total <= high_end.to_f
end
def self.rate(method, cart)
method.shipping_method_values.detect { |m| m.key == "cost" }.value
end
def self.lookup(method, cart)
if self.available?(method, cart)
{ :available => self.available?(method, cart),
:rate => self.rate(method, cart) }
else
{ :available => self.available?(method, cart) }
end
end
end
end
Oops, something went wrong.

0 comments on commit 75426a5

Please sign in to comment.