-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails 4 spree 2 3 #12
base: master
Are you sure you want to change the base?
Changes from all commits
8fc18bb
42c15f0
55daa6e
342b0f5
7b64761
e4d2ab9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//= require backend/intervals_autocomplete |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,16 +8,13 @@ | |
def check_subscriptions | ||
return unless params[:subscriptions] && params[:subscriptions][:active].to_s == "1" | ||
|
||
params[:products].each do |product_id,variant_id| | ||
add_subscription variant_id, params[:subscriptions][:interval_id] | ||
end if params[:products] | ||
interval_id = params[:subscriptions][:interval_id] | ||
|
||
params[:variants].each do |variant_id, quantity| | ||
add_subscription variant_id, params[:subscriptions][:interval_id] | ||
end if params[:variants] | ||
end | ||
params[:products].each { |product_id, variant_id| add_subscription(variant_id, interval_id) } if params[:products] | ||
params[:variants].each { |variant_id, quantity| add_subscription(variant_id, interval_id) } if params[:variants] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused block argument - |
||
|
||
protected | ||
add_subscription(params[:variant_id], params[:subscriptions][:interval_id]) if params[:variant_id] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [102/80] |
||
end | ||
|
||
# DD: TODO write test for this method | ||
def add_subscription(variant_id, interval_id) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
require 'concerns/intervalable' | ||
|
||
class Spree::Subscription < ActiveRecord::Base | ||
attr_accessible :reorder_on, :user_id, :times, :time_unit, :line_item_id, :billing_address_id, :state, | ||
:shipping_address_id, :shipping_method_id, :payment_method_id, :source_id, :source_type | ||
|
||
include Intervalable | ||
|
||
attr_accessor :new_order | ||
|
@@ -19,7 +16,14 @@ class Spree::Subscription < ActiveRecord::Base | |
|
||
has_many :reorders, :class_name => "Spree::Order" | ||
|
||
scope :active, where(:state => 'active') | ||
scope :cart, -> { where(state: 'cart') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
scope :active, -> { where(state: 'active') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
scope :inactive, -> { where(state: 'inactive') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silly robot - prefer single-quotes unless I want to use inline variables |
||
scope :cancelled, -> { where(state: 'cancelled') } | ||
|
||
scope :current, -> { where(state: ['active', 'inactive']) } | ||
|
||
scope :due, -> { active.where("reorder_on <= ?", Date.today) } | ||
|
||
state_machine :state, :initial => 'cart' do | ||
event :suspend do | ||
|
@@ -35,35 +39,43 @@ class Spree::Subscription < ActiveRecord::Base | |
after_transition :on => :start, :do => :set_checkout_requirements | ||
after_transition :on => :resume, :do => :check_reorder_date | ||
end | ||
|
||
def self.reorder_due! | ||
due.each(&:reorder) | ||
end | ||
|
||
# DD: TODO pull out into a ReorderBuilding someday | ||
def reorder | ||
raise false unless self.state == 'active' | ||
|
||
create_reorder && | ||
add_subscribed_line_item && | ||
select_shipping && | ||
add_payment && | ||
confirm_reorder && | ||
complete_reorder && | ||
calculate_reorder_date! | ||
raise false unless active? | ||
|
||
result = create_reorder && | ||
select_shipping && | ||
add_payment && | ||
confirm_reorder && | ||
complete_reorder && | ||
calculate_reorder_date! | ||
|
||
puts result ? " -> Next reorder date: #{self.reorder_on}" : " -> FAILED" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
|
||
result | ||
end | ||
|
||
def create_reorder | ||
puts "[SPREE::SUBSCRIPTION] Reordering subscription: #{id}" | ||
puts " -> creating order..." | ||
|
||
self.new_order = Spree::Order.create( | ||
bill_address: self.billing_address.clone, | ||
ship_address: self.shipping_address.clone, | ||
subscription_id: self.id, | ||
email: self.user.email | ||
) | ||
self.new_order.user_id = self.user_id | ||
bill_address: self.billing_address.clone, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
ship_address: self.shipping_address.clone, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
subscription_id: self.id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
email: self.user.email, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
user_id: self.user_id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
) | ||
|
||
# DD: make it work with spree_multi_domain | ||
if self.new_order.respond_to?(:store_id) | ||
self.new_order.store_id = self.line_item.order.store_id | ||
end | ||
|
||
self.new_order.next # -> address | ||
self.new_order.store_id = self.line_item.order.store_id if self.new_order.respond_to?(:store_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [100/80] |
||
|
||
add_subscribed_line_item && progress # -> delivery | ||
end | ||
|
||
def add_subscribed_line_item | ||
|
@@ -73,39 +85,45 @@ def add_subscribed_line_item | |
line_item.price = self.line_item.price | ||
line_item.save! | ||
|
||
self.new_order.next # -> delivery | ||
result = progress # -> delivery | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless assignment to variable - |
||
end | ||
|
||
def select_shipping | ||
puts " -> selecting shipping rate..." | ||
|
||
# DD: shipments are created when order state goes to "delivery" | ||
shipment = self.new_order.shipments.first # DD: there should be only one shipment | ||
rate = shipment.shipping_rates.first{|r| r.shipping_method.id == self.shipping_method.id } | ||
raise "No rate was found. TODO: Implement logic to select the cheapest rate." unless rate | ||
|
||
shipment.selected_shipping_rate_id = rate.id | ||
shipment.save | ||
end | ||
|
||
def add_payment | ||
payment = self.new_order.payments.build( :amount => self.new_order.item_total ) | ||
puts " -> adding payment..." | ||
|
||
payment = self.new_order.payments.build(amount: self.new_order.outstanding_balance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [87/80] |
||
payment.source = self.source | ||
payment.payment_method = self.payment_method | ||
payment.save! | ||
|
||
self.new_order.next # -> payment | ||
progress # -> payment | ||
end | ||
|
||
def confirm_reorder | ||
self.new_order.next # -> confirm | ||
progress # -> confirm | ||
end | ||
|
||
def complete_reorder | ||
self.new_order.update! | ||
self.new_order.next && self.new_order.save # -> complete | ||
progress && self.new_order.save # -> complete | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
end | ||
|
||
def calculate_reorder_date! | ||
self.reorder_on ||= Date.today | ||
self.reorder_on += self.time | ||
|
||
save | ||
end | ||
|
||
|
@@ -134,9 +152,23 @@ def set_checkout_requirements | |
:user_id => order.user_id | ||
) | ||
end | ||
|
||
def new_order_state | ||
self.new_order.state | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your face is a redundant self |
||
end | ||
def progress | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use empty lines between defs. |
||
current_state = new_order_state | ||
result = self.new_order.next | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant |
||
|
||
success = !!result && current_state != new_order_state | ||
|
||
puts " !! Order progression failed. Status still '#{new_order_state}'" unless success | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [89/80] |
||
|
||
success | ||
end | ||
|
||
def self.reorder_states | ||
@reorder_states ||= state_machine.states.map(&:name) - ["cart"] | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Spree.user_class.class_eval do | ||
has_many :subscriptions, :order => "updated_at DESC", :class_name => 'Spree::Subscription' | ||
has_many :subscriptions, -> { order(updated_at: :desc) }, :class_name => 'Spree::Subscription' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
Spree::Variant.class_eval do | ||
delegate :subscribable?, :to => :product | ||
|
||
attr_accessible :subscribed_price | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused block argument -
product_id
. If it's necessary, use_
or_product_id
as an argument name to indicate that it won't be used.Line is too long. [118/80]