Skip to content

Commit

Permalink
state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
neutralino1 committed Dec 19, 2012
1 parent a8e68fd commit 62ad686
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 103 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ gem "jquery-rails"
gem "devise"
gem 'paperclip'

gem 'state_machine'

gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'
193 changes: 112 additions & 81 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,161 +1,192 @@
GIT
remote: git://github.com/sferik/rails_admin.git
revision: 8048e7d844c2bcf438037ac793dd9677ed6d4042
revision: cee9a893e9fd84d0591e61937c6e662d104ea96a
specs:
rails_admin (0.0.1)
bbenezech-nested_form (~> 0.0)
bootstrap-sass (~> 2.0)
rails_admin (0.3.0)
bootstrap-sass (~> 2.1)
builder (~> 3.0)
coffee-rails (~> 3.1)
font-awesome-sass-rails (~> 2.0)
haml (~> 3.1)
jquery-rails (>= 1.0)
kaminari (~> 0.12)
rack-pjax (~> 0.5)
jquery-rails (~> 2.1)
jquery-ui-rails (~> 2.0)
kaminari (~> 0.14)
nested_form (~> 0.2)
rack-pjax (~> 0.6)
rails (~> 3.1)
remotipart (~> 1.0)
sass-rails (~> 3.1)

PATH
remote: .
specs:
glysellin (0.0.1)
glysellin (0.3.alpha)
activemerchant
country_select
money
paperclip
rails (~> 3.2.0)
simple_form

GEM
remote: http://rubygems.org/
specs:
actionmailer (3.2.1)
actionpack (= 3.2.1)
mail (~> 2.4.0)
actionpack (3.2.1)
activemodel (= 3.2.1)
activesupport (= 3.2.1)
actionmailer (3.2.9)
actionpack (= 3.2.9)
mail (~> 2.4.4)
actionpack (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.1)
journey (~> 1.0.4)
rack (~> 1.4.0)
rack-cache (~> 1.1)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.1.2)
activemodel (3.2.1)
activesupport (= 3.2.1)
sprockets (~> 2.2.1)
active_utils (1.0.5)
activesupport (>= 2.3.11)
i18n
activemerchant (1.29.3)
active_utils (>= 1.0.2)
activesupport (>= 2.3.14)
builder (>= 2.0.0)
i18n
json (>= 1.5.1)
money
nokogiri
activemodel (3.2.9)
activesupport (= 3.2.9)
builder (~> 3.0.0)
activerecord (3.2.1)
activemodel (= 3.2.1)
activesupport (= 3.2.1)
arel (~> 3.0.0)
activerecord (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.1)
activemodel (= 3.2.1)
activesupport (= 3.2.1)
activesupport (3.2.1)
activeresource (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
activesupport (3.2.9)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.0)
bbenezech-nested_form (0.0.2)
arel (3.0.2)
bcrypt-ruby (3.0.1)
bootstrap-sass (2.0.0)
builder (3.0.0)
cocaine (0.2.1)
bootstrap-sass (2.2.1.1)
sass (~> 3.2)
builder (3.0.4)
cocaine (0.4.2)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.2.0)
devise (2.0.0)
coffee-script-source (1.4.0)
country_select (1.0.1)
devise (2.1.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
orm_adapter (~> 0.1)
railties (~> 3.1)
warden (~> 1.1)
warden (~> 1.2.1)
erubis (2.7.0)
execjs (1.3.0)
execjs (1.4.0)
multi_json (~> 1.0)
haml (3.1.4)
font-awesome-sass-rails (2.0.0.0)
railties (>= 3.1.1)
sass-rails (>= 3.1.1)
haml (3.1.7)
hike (1.2.1)
hpricot (0.8.6)
i18n (0.6.0)
journey (1.0.1)
jquery-rails (2.0.0)
railties (< 5.0, >= 3.2.0.beta)
thor (~> 0.14)
json (1.6.5)
kaminari (0.13.0)
i18n (0.6.1)
journey (1.0.4)
jquery-rails (2.1.4)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (2.0.2)
jquery-rails
railties (>= 3.1.0)
json (1.7.5)
kaminari (0.14.1)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
mail (2.4.1)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.4)
mime-types (1.19)
money (5.1.0)
i18n (~> 0.6.0)
multi_json (1.5.0)
mysql2 (0.3.11)
orm_adapter (0.0.6)
paperclip (2.5.2)
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
cocaine (>= 0.0.2)
nested_form (0.3.1)
nokogiri (1.5.5)
orm_adapter (0.4.0)
paperclip (3.3.1)
activemodel (>= 3.0.0)
activerecord (>= 3.0.0)
activesupport (>= 3.0.0)
cocaine (~> 0.4.0)
mime-types
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.1)
rack-cache (1.2)
rack (>= 0.4)
rack-pjax (0.5.7)
hpricot (~> 0.8.6)
rack-pjax (0.6.0)
nokogiri (~> 1.5)
rack (~> 1.3)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack-test (0.6.2)
rack (>= 1.0)
rails (3.2.1)
actionmailer (= 3.2.1)
actionpack (= 3.2.1)
activerecord (= 3.2.1)
activeresource (= 3.2.1)
activesupport (= 3.2.1)
rails (3.2.9)
actionmailer (= 3.2.9)
actionpack (= 3.2.9)
activerecord (= 3.2.9)
activeresource (= 3.2.9)
activesupport (= 3.2.9)
bundler (~> 1.0)
railties (= 3.2.1)
railties (3.2.1)
actionpack (= 3.2.1)
activesupport (= 3.2.1)
railties (= 3.2.9)
railties (3.2.9)
actionpack (= 3.2.9)
activesupport (= 3.2.9)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2.2)
thor (>= 0.14.6, < 2.0)
rake (10.0.3)
rdoc (3.12)
json (~> 1.4)
remotipart (1.0.2)
sass (3.1.12)
sass-rails (3.2.4)
sass (3.2.3)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
sprockets (2.1.2)
simple_form (2.0.4)
actionpack (~> 3.0)
activemodel (~> 3.0)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
thor (0.14.6)
tilt (~> 1.1, != 1.3.0)
state_machine (1.1.2)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.31)
warden (1.1.0)
tzinfo (0.3.35)
warden (1.2.1)
rack (>= 1.0)

PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass (~> 2.0.0)
devise
glysellin!
jquery-rails
mysql2
paperclip
rails_admin!
sass-rails (~> 3.1)
state_machine
21 changes: 13 additions & 8 deletions app/models/glysellin/order.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
module Glysellin
class Order < ActiveRecord::Base

self.table_name = 'glysellin_orders'

state_machine initial: :created do
event :payment do
transition created: :payment
end

event :paid do
transition payment: :paid
end
end

# Relations
#
# Order items are used to map order to cloned and simplified products
Expand Down Expand Up @@ -184,6 +195,7 @@ def payment_method
# @return [Boolean] if the doc was saved
def pay!
self.payment.new_status Payment::PAYMENT_STATUS_PAID
self.paid
self.status = ORDER_STATUS_PAID
self.paid_on = payment.last_payment_action_on
self.save
Expand All @@ -196,14 +208,6 @@ def paid?
payment.status == Payment::PAYMENT_STATUS_PAID
end

def shipping?
payment.status == Payment::PAYMENT_STATUS_SHIPPING
end

def shipped?
payment.status == Payment::ORDER_STATUS_SHIPPED
end

# Permits to create or update an order from nested forms (hashes)
# and can create a whole order object ready to be paid but
# only modifies the order from the params passed in the data param
Expand Down Expand Up @@ -278,6 +282,7 @@ def fill_payment_method_from_hash data
payment_hash = data[:payments_attributes].first.last
payment.type = PaymentMethod.find(payment_hash[:type_id])
self.status = ORDER_STATUS_PAYMENT_PENDING
self.payment
end

def fill_products_from_hash data
Expand Down
21 changes: 7 additions & 14 deletions lib/generators/glysellin/install/templates/order_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ module Glysellin
class OrderObserver < ActiveRecord::Observer

def after_payment order
# This will be run once the order has been paid
end

def after_shipping order
# This will be run once the order gets in shipping status
puts "### AFTER PAYMENT"
puts order.inspect
# This will be run once the payment method has been chosen
end

def after_shipped order
# This will be run once the order has been shipped
def after_paid order
puts "### AFTER PAID"
puts order.inspect
# This will be run once the order has been paid
end

def after_update order
if order.status_changed?
after_payment(order) if order.paid?
after_shipping(order) if order.shipping?
after_shipped(order) if order.shipped?
end
end
end
end

0 comments on commit 62ad686

Please sign in to comment.