Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
misteral committed Jun 9, 2012
0 parents commit 2759def
Show file tree
Hide file tree
Showing 29 changed files with 399 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\#*
*~
.#*
.DS_Store
.idea
.project
tmp
nbproject
*.swp
spec/dummy
*.class
*.pyc
*.pyo
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'http://rubygems.org'

group :test do
gem 'ffaker'
end

if RUBY_VERSION < "1.9"
gem "ruby-debug"
else
gem "ruby-debug19"
end

gem 'active_reload'
gemspec
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2012 [name of plugin creator]
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name Spree nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Spree::CashOnDelivery
=====================

Introduction goes here.


Example
=======

Example goes here.

Testing
-------

Be sure to bundle your dependencies and then create a dummy test app for the specs to run against.

$ bundle
$ bundle exec rake test app
$ bundle exec rspec spec

Copyright (c) 2012 [name of extension creator], released under the New BSD License
29 changes: 29 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rake'
require 'rake/testtask'
require 'rake/packagetask'
require 'rubygems/package_task'
require 'rspec/core/rake_task'
require 'spree/core/testing_support/common_rake'

RSpec::Core::RakeTask.new

task :default => [:spec]

spec = eval(File.read('spree_cash_on_delivery.gemspec'))

Gem::PackageTask.new(spec) do |p|
p.gem_spec = spec
end

desc "Release to gemcutter"
task :release => :package do
require 'rake/gemcutter'
Rake::Gemcutter::Tasks.new(spec).define
Rake::Task['gem:push'].invoke
end

desc "Generates a dummy app for testing"
task :test_app do
ENV['LIB_NAME'] = 'spree_cash_on_delivery'
Rake::Task['common:test_app'].invoke
end
9 changes: 9 additions & 0 deletions Versionfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file is used to designate compatibilty with different versions of Spree
# Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details

# Examples
#
# "0.70.x" => { :branch => "master"}
# "0.60.x" => { :branch => "0-60-stable" }
# "0.40.x" => { :tag => "v1.0.0", :version => "1.0.0" }

1 change: 1 addition & 0 deletions app/assets/javascripts/admin/spree_cash_on_delivery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require admin/spree_core
1 change: 1 addition & 0 deletions app/assets/javascripts/store/spree_cash_on_delivery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require store/spree_core
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/spree_cash_on_delivery.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require admin/spree_core
*/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/store/spree_cash_on_delivery.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require store/spree_core
*/
4 changes: 4 additions & 0 deletions app/models/spree/app_configuration_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Spree::AppConfiguration.class_eval do
preference :cash_on_delivery_charge, :decimal, :default => 25.0
end

7 changes: 7 additions & 0 deletions app/models/spree/cash_on_delivery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Spree
module CashOnDelivery
def self.table_name_prefix
'spree_cash_on_delivery_'
end
end
end
19 changes: 19 additions & 0 deletions app/models/spree/cash_on_delivery/payment_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Spree
class CashOnDelivery::PaymentMethod < Spree::PaymentMethod
def payment_profiles_supported?
true # we want to show the confirm step.
end

def provider_class
self.class
end

def payment_source_class
Spree::CashOnDelivery::Transaction
end

def method_type
'cash_on_delivery'
end
end
end
73 changes: 73 additions & 0 deletions app/models/spree/cash_on_delivery/transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'cash_on_delivery.rb'))

module Spree
module CashOnDelivery
class Transaction < ActiveRecord::Base
has_many :payments, :as => :source

state_machine :initial => 'initial' do
event :send_goods do
transition 'initial' => 'goods_sent'
end

event :receive_cash do
transition 'goods_sent' => 'cash_received'
end

event :void_txn do
transition 'goods_sent' => 'canceled'
end
end

def post_create(payment)
payment.order.adjustments.create(:amount => Spree::Config[:cash_on_delivery_charge],
:source => payment,
:originator => payment,
:label => I18n.t(:shipping_and_handling))
end

def update_adjustment(adjustment, src)
adjustment.update_attribute_without_callbacks(:amount, Spree::Config[:cash_on_delivery_charge])
end

def actions
%w{capture void cash_received}
end

def can_capture?(payment)
['checkout', 'pending'].include?(payment.state)
end

def can_void?(payment)
payment.state != 'void' and ['goods_sent', 'initial'].include?(payment.source.state)
end

def can_cash_received?(payment)
payment.state == 'completed' and payment.source.state == 'goods_sent'
end

def capture(payment)
payment.update_attribute(:state, 'pending') if payment.state == 'checkout'
payment.complete
payment.source.send_goods
true
end

def void(payment)
payment.update_attribute(:state, 'pending') if payment.state == 'checkout'
payment.void
payment.source.void_txn
true
end

def cash_received(payment)
payment.source.receive_cash
true
end

def process!(payment)
capture(payment)
end
end
end
end
20 changes: 20 additions & 0 deletions app/models/spree/payment_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Spree::Payment.class_eval do
has_one :adjustment, :as => :source, :dependent => :destroy
def build_source
return if source_attributes.nil?

if payment_method and payment_method.payment_source_class
self.source = payment_method.payment_source_class.new(source_attributes)
if self.source.respond_to?(:post_create)
self.source.post_create(self)
end
end
end

def update_adjustment(adjustment, src)
if self.source.respond_to?(:update_adjustment)
self.source.update_adjustment(adjustment, src)
end
end
end

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<label>Status</label> <%= @payment.source.state.titleize %>
2 changes: 2 additions & 0 deletions app/views/spree/checkout/payment/_cash_on_delivery.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% param_prefix = "payment_source[#{payment_method.id}][amount]", @order.total %>
<% @order.bill_address ||= Spree::Address.default %>
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
cash_received: "Cash Received"
shipping_and_handling: 'Handling charges'
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Spree::CashOnDelivery::Engine.routes.draw do
# Add your extension routes here
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateSpreeCashOnDeliveryTransactions < ActiveRecord::Migration
def change
create_table :spree_cash_on_delivery_transactions do |t|
t.string :state
t.decimal :amount, :scale => 2, :precision => 8
t.timestamps
end
end
end
29 changes: 29 additions & 0 deletions lib/generators/spree/cash_on_delivery/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Spree::CashOnDelivery
module Generators
class InstallGenerator < Rails::Generators::Base

def add_javascripts
append_file "app/assets/javascripts/store/all.js", "//= require store/cash_on_delivery\n"
append_file "app/assets/javascripts/admin/all.js", "//= require admin/cash_on_delivery\n"
end

def add_stylesheets
inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/cash_on_delivery\n", :before => /\*\//, :verbose => true
inject_into_file "app/assets/stylesheets/admin/all.css", " *= require admin/cash_on_delivery\n", :before => /\*\//, :verbose => true
end

def add_migrations
run 'bundle exec rake railties:install:migrations FROM=spree_cash_on_delivery'
end

def run_migrations
res = ask "Would you like to run the migrations now? [Y/n]"
if res == "" || res.downcase == "y"
run 'bundle exec rake db:migrate'
else
puts "Skiping rake db:migrate, don't forget to run it!"
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/spree/cash_on_delivery/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Spree::CashOnDelivery
class Engine < Rails::Engine
engine_name 'spree_cash_on_delivery'

config.autoload_paths += %W(#{config.root}/lib)

# use rspec for tests
config.generators do |g|
g.test_framework :rspec
end

def self.activate
Dir.glob(File.join(File.dirname(__FILE__), "../../../app/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end

config.to_prepare &method(:activate).to_proc

initializer "spree_cash_on_delivery.register.payment_method", :after => "spree.register.payment_methods" do |app|
app.config.spree.payment_methods << Spree::CashOnDelivery::PaymentMethod
end
end
end
2 changes: 2 additions & 0 deletions lib/spree_cash_on_delivery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'spree_core'
require 'spree/cash_on_delivery/engine'
5 changes: 5 additions & 0 deletions script/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

ENGINE_PATH = File.expand_path('../..', __FILE__)
load File.expand_path('../../spec/dummy/script/rails', __FILE__)
11 changes: 11 additions & 0 deletions spec/models/spree/cash_on_delivery/payment_method_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe Spree::CashOnDelivery::PaymentMethod do
context "capture" do
before(:each) do
@order = Factory(:order_with_totals)
@order.payments = []
@order.payments.build(:amount => @order.total)
end
end
end
5 changes: 5 additions & 0 deletions spec/models/spree/cash_on_delivery/transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe CashOnDelivery::Transaction do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading

0 comments on commit 2759def

Please sign in to comment.