Skip to content

Commit

Permalink
update orderer for new cart model, use friendly_id for slugs, clean u…
Browse files Browse the repository at this point in the history
…p some unused code
  • Loading branch information
vala committed Jun 6, 2013
1 parent 9d77df6 commit c116c83
Show file tree
Hide file tree
Showing 38 changed files with 198 additions and 189 deletions.
24 changes: 8 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ In order to stay simple, we choosed for now to keep with some strong dependencie

Also, no admin interface is provided so you can integrate it, but we always use [RailsAdmin](https://github.com/sferik/rails_admin).


## Dependencies

* [Devise](https://github.com/plataformatec/devise)
* [Paperclip](https://github.com/thoughtbot/paperclip)
* [Simple Form](https://github.com/plataformatec/simple_form)


## Disclaimer

Glysellin is under development and can now be used, but documentation is poor, there are no tests and API can change quickly while we don't have tested it within enough projects.
Expand All @@ -33,33 +35,21 @@ The install generator command :
rails generate glysellin:install
```


## Using the Cart

The shopping cart contents are stored in user's cookies, with the key `glysellin.cart`

### Displaying the cart

Here is a simple code example that loads the cart for every controller action :

```ruby
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery

before_filter :init

def init
@cart = Glysellin::Cart.new(cookies['glysellin.cart'])
end
end
```
### Displaying the cart

To display the cart you must render it's partial in your layout :

```erb
<%= render_cart(@cart) %>
<%= render_cart(current_cart) %>
```


### Filling the cart

To fill the cart, you can use the pre-built helper to create a simple "Add to cart" form that asynchronously updates user's cart contents.
Expand All @@ -69,6 +59,7 @@ You must pass the helper a `Glysellin::Product` instance in order to make it wor
<%= add_to_cart_form(@product) %>
```


## Managing orders

By default, being bound to Devise, Glysellin automatically generates anonymous users to bind orders to. You can choose to keep this behavior alone, or add real subscription in the ordering process or remove the default behavior to force users to create an account by switching off the default functionality in the initializer (default parameter is commented) :
Expand All @@ -84,6 +75,7 @@ Glysellin.config do |config|
end
```


## Customizing Order behavior

Since the Order object is implemented with the [state_machine gem](), it emits state transition events.
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/glysellin/cart/addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module Glysellin
module Cart
class AddressesController < CartController
def update
@cart.update(params[:glysellin_cart_basket])
current_cart.update(params[:glysellin_cart_basket])

if @cart.valid?
@cart.addresses_filled!
if current_cart.valid?
current_cart.addresses_filled!
redirect_to cart_path
else
@cart.state = "addresses"
current_cart.state = "addresses"
render "glysellin/cart/show"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/glysellin/cart/discount_code_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Glysellin
module Cart
class DiscountCodeController < CartController
def update
@cart.discount_code = params[:code]
current_cart.discount_code = params[:code]
render json: totals_hash
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/glysellin/cart/payment_method_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module Glysellin
module Cart
class PaymentMethodController < CartController
def update
@cart.update(params[:glysellin_cart_basket])
current_cart.update(params[:glysellin_cart_basket])

if @cart.valid?
@cart.payment_method_chosen!
if current_cart.valid?
current_cart.payment_method_chosen!
redirect_to cart_path
else
@cart.state = "choose_payment_method"
current_cart.state = "choose_payment_method"
render "glysellin/cart/show"
end
end
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/glysellin/cart/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module Glysellin
module Cart
class ProductsController < CartController
def create
@cart.add(params[:cart])
@cart.products_added!
current_cart.add(params[:cart])
current_cart.products_added!
@product_added_to_cart = true
render_cart_partial
end

def update
@cart.set_quantity(params[:id], params[:quantity], override: true)
current_cart.set_quantity(params[:id], params[:quantity], override: true)

product = @cart.product(params[:id])
product = current_cart.product(params[:id])
variant, quantity = product.variant, product.quantity

render json: {
Expand All @@ -22,14 +22,14 @@ def update
end

def destroy
@cart.remove(params[:id])
current_cart.remove(params[:id])
redirect_to cart_path
end

def validate
@cart.update(params[:glysellin_cart_basket])
@cart.validated! if @cart.valid?
@cart.customer = current_user
current_cart.update(params[:glysellin_cart_basket])
current_cart.validated! if current_cart.valid?
current_cart.customer = current_user
redirect_to cart_path
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/glysellin/cart/shipping_method_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module Glysellin
module Cart
class ShippingMethodController < CartController
def update
@cart.update(params[:glysellin_cart_basket])
current_cart.update(params[:glysellin_cart_basket])

if @cart.valid?
@cart.shipping_method_chosen!
if current_cart.valid?
current_cart.shipping_method_chosen!
redirect_to cart_path
else
@cart.state = "choose_shipping_method"
@cart.valid?
current_cart.state = "choose_shipping_method"
current_cart.valid?
render "glysellin/cart/show"
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/glysellin/cart/state_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module Cart
class StateController < CartController
def show
state = params[:state]
@cart.state = state.to_sym if @cart.available_states.include?(state)

if current_cart.available_states.include?(state)
current_cart.state = state.to_sym
end

redirect_to cart_path
end
end
Expand Down
28 changes: 12 additions & 16 deletions app/controllers/glysellin/cart_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,47 @@ class CartController < ApplicationController
after_filter :update_cart_in_session

def show
@cart.update_quantities!
current_cart.update_quantities!
end

def destroy
@cart = Cart.new
session.delete("glysellin.cart")
reset_cart!
redirect_to cart_path
end

protected

def render_cart_partial
render partial: 'cart', locals: {
cart: @cart
}
render partial: 'cart', locals: { cart: current_cart }
end

def set_cart
@cart ||= Cart.new(session["glysellin.cart"])
@states = @cart.available_states
@states = current_cart.available_states
end

# Helper method to set cookie value
def update_cart_in_session options = {}
if @cart.errors.any?
if current_cart.errors.any?
flash[:error] =
t("glysellin.errors.cart.state_transitions.#{ @cart.state }")
t("glysellin.errors.cart.state_transitions.#{ current_cart.state }")
end

session["glysellin.cart"] = @cart.serialize
session["glysellin.cart"] = current_cart.serialize
end

def totals_hash
adjustment = @cart.discount
adjustment = current_cart.discount

discount_name = adjustment.name rescue nil
discount_value = number_to_currency(adjustment.value) rescue nil

{
discount_name: discount_name,
discount_value: discount_value,
total_eot_price: number_to_currency(@cart.total_eot_price),
total_price: number_to_currency(@cart.total_price),
eot_subtotal: number_to_currency(@cart.eot_subtotal),
subtotal: number_to_currency(@cart.subtotal)
total_eot_price: number_to_currency(current_cart.total_eot_price),
total_price: number_to_currency(current_cart.total_price),
eot_subtotal: number_to_currency(current_cart.eot_subtotal),
subtotal: number_to_currency(current_cart.subtotal)
}
end
end
Expand Down
20 changes: 0 additions & 20 deletions app/controllers/glysellin/main_controller.rb

This file was deleted.

10 changes: 7 additions & 3 deletions app/controllers/glysellin/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
module Glysellin
class OrdersController < MainController
class OrdersController < ApplicationController
protect_from_forgery :except => :gateway_response

def index
@orders = Order.from_customer(current_user)
end

def show
user_order = Order.where('id = ? AND customer_id = ?', params[:id], @active_customer.id)
user_order = if user_signed_in?
Order.where('id = ? AND customer_id = ?', params[:id], current_user.id)
else
[]
end

if user_order.length > 0
@order = user_order.first
else
Expand All @@ -16,7 +21,6 @@ def show
end
end


def gateway_response
# Get gateway object
gateway = if params[:id]
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/glysellin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
end

def show
@product = Glysellin::Product.find_by_slug(params[:id])
@product = Glysellin::Product.find(params[:id])
end
end
end
22 changes: 16 additions & 6 deletions app/helpers/glysellin/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Glysellin
module OrdersHelper
module OrdersHelper

# Generates Orderer form fields
#
Expand All @@ -9,16 +9,26 @@ module OrdersHelper
def addresses_fields_for form, record = nil
# Copy addresses from record to form object
if record
%w(billing_address shipping_address).each do |attribute|
attributes = record.send("#{attribute}").attributes.select { |key, value| Glysellin::Address.accessible_attributes.include?(key) }
form.object.send("build_#{attribute}", attributes) unless form.object.send(attribute).present? && form.object.send(attribute).id.present?
%w(billing_address shipping_address).each do |addr|
if (address = record.send("#{ addr }"))
attrs = address.attributes.select do |key, value|
Glysellin::Address.accessible_attributes.include?(key)
end
end

unless (address = form.object.send(addr).presence) && address.id.present?
form.object.send("#{ addr }=", Glysellin::Address.new(attrs))
end
end

unless form.object.use_another_address_for_shipping.present?
form.object.use_another_address_for_shipping = record.use_another_address_for_shipping
end
form.object.use_another_address_for_shipping = record.use_another_address_for_shipping unless form.object.use_another_address_for_shipping.present?
end

form.object.build_billing_address unless form.object.billing_address
form.object.build_shipping_address unless form.object.shipping_address

render partial: 'glysellin/orders/addresses_fields', locals: { form: form }
end
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/glysellin/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,16 @@ class Address < ActiveRecord::Base
#
# Validates presence of the fields defined in the config file or the glysellin initializer
validates_presence_of *Glysellin.address_presence_validation_keys

def same_as?(other)
clone_attributes == other.clone_attributes
end

def clone_attributes
clone.attributes.select do |key, value|
self.class.accessible_attributes.include?(key) &&
!key.to_s.match(/_addressable_(type|id)$/)
end
end
end
end
Loading

0 comments on commit c116c83

Please sign in to comment.