Permalink
Browse files

Changed variants to many-to-many with subscriptions.

  • Loading branch information...
1 parent c7d093f commit 0262dd96267c3a0b1e72480089f393e9d61a7d12 @sheenobu sheenobu committed with sheena Dec 6, 2012
View
4 Gemfile
@@ -6,4 +6,8 @@ else
gem 'debugger'
end
+gem 'spree_core', '~> 1.1.3'
+gem 'spree_auth', '~> 1.1.3'
+gem 'rspec-rails'
+
gemspec
View
18 Gemfile.lock
@@ -53,9 +53,11 @@ GEM
json (~> 1.4)
nokogiri (>= 1.4.4)
uuidtools (~> 2.1)
+ bcrypt-ruby (3.0.1)
braintree (2.18.0)
builder (>= 2.0.0)
builder (3.0.4)
+ cancan (1.6.7)
capybara (1.0.1)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
@@ -77,6 +79,11 @@ GEM
deface (0.9.1)
nokogiri (~> 1.5.0)
rails (~> 3.1)
+ devise (2.0.4)
+ bcrypt-ruby (~> 3.0)
+ orm_adapter (~> 0.0.3)
+ railties (~> 3.1)
+ warden (~> 1.1.1)
diff-lcs (1.1.3)
erubis (2.7.0)
factory_girl (2.6.4)
@@ -112,6 +119,7 @@ GEM
activerecord (>= 3.0.0)
railties (>= 3.0.0)
nokogiri (1.5.5)
+ orm_adapter (0.0.7)
paperclip (2.8.0)
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
@@ -168,6 +176,10 @@ GEM
libwebsocket (~> 0.1.3)
multi_json (~> 1.0)
rubyzip
+ spree_auth (1.1.3)
+ cancan (= 1.6.7)
+ devise (~> 2.0.0)
+ spree_core (= 1.1.3)
spree_core (1.1.3)
activemerchant (= 1.20.4)
acts_as_list (= 0.1.4)
@@ -197,6 +209,8 @@ GEM
polyglot (>= 0.3.1)
tzinfo (0.3.33)
uuidtools (2.1.3)
+ warden (1.1.1)
+ rack (>= 1.0)
xpath (0.1.4)
nokogiri (~> 1.3)
@@ -208,6 +222,8 @@ DEPENDENCIES
debugger
factory_girl (~> 2.6.4)
ffaker
- rspec-rails (~> 2.9)
+ rspec-rails
+ spree_auth (~> 1.1.3)
+ spree_core (~> 1.1.3)
spree_recurring!
sqlite3
View
61 app/models/spree/subscription.rb
@@ -1,5 +1,26 @@
class Spree::Subscription < ActiveRecord::Base
+ def self.create_from_variants(order,variants)
+ # get subscription info TODO: load from product configuration.
+ interval = "month"
+ duration = 1
+
+ price = variants.map(&:price).sum
+
+ s = Spree::Subscription.create(:interval => interval,
+ :duration => duration,
+ :user => order.user,
+ :variants => variants,
+ :price => price,
+ :next_payment_at => Time.now + eval(duration.to_s + "." + interval.to_s),
+ :creditcard => order.creditcards[0],
+ :created_by_order_id => order.id,
+ :gateway => order.payment_method)
+
+
+ return s
+ end
+
def self.create_from_order(order,line_item)
# get subscription info TODO: load from product configuration.
@@ -9,7 +30,7 @@ def self.create_from_order(order,line_item)
s = Spree::Subscription.create(:interval => interval,
:duration => duration,
:user => order.user,
- :variant => line_item.variant,
+ :variants => [line_item.variant],
:price => line_item.price,
:next_payment_at => Time.now + eval(duration.to_s + "." + interval.to_s),
:creditcard => order.creditcards[0],
@@ -25,11 +46,11 @@ def get_gateway
return self.parent_order.payment_method if not self.parent_order.nil?
end
- attr_accessible :user,:variant,:creditcard,:parent_order,:ship_address,:bill_address,:expiry_notifications,:price,:state
+ attr_accessible :user,:variants,:creditcard,:parent_order,:ship_address,:bill_address,:expiry_notifications,:price,:state
attr_accessible :interval, :duration, :next_payment_at, :created_by_order_id,:gateway
+ has_and_belongs_to_many :variants, :class_name => "Spree::Variant", :join_table => "spree_additional_subscription_variants"
belongs_to :user
- belongs_to :variant
belongs_to :creditcard
belongs_to :gateway
@@ -143,28 +164,26 @@ def available_payment_methods
@available_payment_methods ||= Spree::PaymentMethod.available(:front_end)
end
- def payment_method
-# if payment and payment.payment_method
-# payment.payment_method
-# else
- available_payment_methods.first
-# end
- end
+ def payment_method #TODO: probably should be better?
+ available_payment_methods.first
+ end
def renew_subscription
new_order = self.subsequent_orders.build
new_order.save!
- new_order.user = self.user
- new_order.bill_address = self.bill_address
- new_order.ship_address = self.ship_address
- new_order.email = self.user.email
+ new_order.user = self.user
+ new_order.bill_address = self.bill_address
+ new_order.ship_address = self.ship_address
+ new_order.email = self.user.email
+
+ # Add the associated variants.
+ # TODO: read the variant quantity from the join table.
+ # TODO: read grandfathered price of variant from join table.
+ self.variants.each do |x|
+ new_order.add_variant( x , 1 )
+ end
- #Add a line item from the variant on this sub and set the price
- new_order.add_variant( self.variant )
- #NOTE settting quantity as opposed to price becuase during processing payments the order and price will get flipped
- new_order.line_items.first.quantity = self.price.to_i #doing this will clip a price like 8.8 to 8)
- new_order.line_items.first.price = 1
new_order.save!
#Process payment for the order
@@ -176,8 +195,8 @@ def renew_subscription
new_order.payments << new_payment
new_order.update! #updating totals
- #By setting to confirm we can do new_order.next and we get all the same
- #callbacks as if you were on the order form itself
+ # By setting to confirm we can do new_order.next and we get all the same
+ # callbacks as if you were on the order form itself
new_order.state = 'confirm'
new_order.next
new_order.save!
View
2 app/models/spree/variant_decorator.rb
@@ -1,4 +1,4 @@
Spree::Variant.class_eval do
- has_many :subscriptions
+ has_and_belongs_to_many :subscriptions, :join_table => "spree_additional_subscription_variants"
attr_accessible :subscribable
end
View
3 app/views/spree/admin/subscriptions/index.html.erb
@@ -22,16 +22,15 @@
<table class="index sortable" id='listing_subscriptions'>
<tr>
<th><%= sort_link @search,'user.email', t(:username), {}, {:title => 'users_email_title'} %></th>
- <th><%= sort_link @search,'variant.name', t(:product), {} %></th>
<th><%= sort_link @search,'price', t(:price), {} %></th>
+ <!-- TODO: Read normalized variant description from subscription table -->
<th><%= sort_link @search,'next_payment_at', t(:next_billing_date), {} %></th>
<th><%= sort_link @search,'state', t(:status), {} %></th>
<th style="width: 50px"></th>
</tr>
<% @collection.each do |subscription| %>
<tr id="<%= dom_id subscription %>">
<td><%= subscription.user.email %></td>
- <td><%= subscription.variant.name %></td>
<td><%= subscription.price %></td>
<td><%= subscription.next_payment_at %></td>
<td><%= subscription.state %></td>
View
8 db/migrate/20121206151419_add_additional_variants_to_spree_subscription.rb
@@ -0,0 +1,8 @@
+class AddAdditionalVariantsToSpreeSubscription < ActiveRecord::Migration
+ def change
+ create_table :spree_additional_subscription_variants, :id => false do |t|
+ t.integer :variant_id
+ t.integer :subscription_id
+ end
+ end
+end
View
12 spec/controllers/spree/admin/subscriptions_controller_spec.rb
@@ -2,11 +2,11 @@
describe Spree::Admin::SubscriptionsController do
- describe "GET 'index'" do
- it "returns http success" do
- get 'index'
- response.should be_success
- end
- end
+# describe "GET 'index'" do
+# it "returns http success" do
+# get 'index'
+# response.should be_success
+# end
+# end
end
View
2 spec/helpers/spree/admin/subscriptions_helper_spec.rb
@@ -11,5 +11,5 @@
# end
# end
describe Spree::Admin::SubscriptionsHelper do
- pending "add some examples to (or delete) #{__FILE__}"
+
end
View
2 spec/models/spree/cancellation_data_spec.rb
@@ -1,5 +1,5 @@
require 'spec_helper'
describe Spree::CancellationData do
- pending "add some examples to (or delete) #{__FILE__}"
+# pending "add some examples to (or delete) #{__FILE__}"
end
View
2 spec/views/spree/admin/subscriptions/index.html.erb_spec.rb
@@ -1,5 +1,5 @@
require 'spec_helper'
describe "subscriptions/index.html.erb" do
- pending "add some examples to (or delete) #{__FILE__}"
+
end

0 comments on commit 0262dd9

Please sign in to comment.