Module: EffectiveOrdersHelper

Defined in:
app/helpers/effective_orders_helper.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_purchasable_path(purchasable, action = :show) ⇒ Object

This is called on the My Sales Page and is intended to be overridden in the app if needed



31
32
33
# File 'app/helpers/effective_orders_helper.rb', line 31

def acts_as_purchasable_path(purchasable, action = :show)
  polymorphic_path(purchasable)
end


77
78
79
80
# File 'app/helpers/effective_orders_helper.rb', line 77

def link_to_my_purchases(opts = {})
  options = {:rel => :nofollow}.merge(opts)
  link_to (options.delete(:label) || 'My Purchases'), effective_orders.my_purchases_path, options
end

#order_item_summary(order_item) ⇒ Object



22
23
24
25
26
27
28
# File 'app/helpers/effective_orders_helper.rb', line 22

def order_item_summary(order_item)
  if order_item.quantity > 1
    (:p, "#{price_to_currency(order_item.total)} total for #{pluralize(order_item.quantity, 'item')}")
  else
    (:p, "#{price_to_currency(order_item.total)} total")
  end
end

#order_payment_to_html(order) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/effective_orders_helper.rb', line 35

def order_payment_to_html(order)
  payment = order.payment

  if order.purchased?(:stripe_connect) && order.payment.kind_of?(Hash)
    payment = Hash[
      order.payment.map do |seller_id, v|
        if (user = Effective::Customer.find(seller_id).try(:user))
          [link_to(user, admin_user_path(user)), order.payment[seller_id]]
        else
          [seller_id, order.payment[seller_id]]
        end
      end
    ]
  end

  (:pre) do
    raw JSON.pretty_generate(payment).html_safe
      .gsub('\"', '')
      .gsub("[\n\n    ]", '[]')
      .gsub("{\n    }", '{}')
  end
end

#order_summary(order) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/effective_orders_helper.rb', line 7

def order_summary(order)
  (:p, "#{price_to_currency(order.total)} total for #{pluralize(order.num_items, 'item')}:") +

  (:ul) do
    order.order_items.map do |item|
      (:li) do
        title = item.title.split('<br>')
        "#{item.quantity}x #{title.first} for #{price_to_currency(item.price)}".tap do |output|
          title[1..-1].each { |line| output << "<br>#{line}" }
        end.html_safe
      end
    end.join().html_safe
  end
end

#price_to_currency(price) ⇒ Object



2
3
4
5
# File 'app/helpers/effective_orders_helper.rb', line 2

def price_to_currency(price)
  raise 'price_to_currency expects an Integer representing the number of cents in a price' unless price.kind_of?(Integer)
  number_to_currency(price / 100.0)
end

#render_checkout(order, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/effective_orders_helper.rb', line 62

def render_checkout(order, opts = {})
  raise ArgumentError.new('unable to checkout an order without a user') unless order.user.present?

  locals = {
    :purchased_redirect_url => nil,
    :declined_redirect_url => nil
  }.merge(opts)

  if order.new_record? || !order.valid?
    render(:partial => 'effective/orders/checkout_step_1', :locals => locals.merge({:order => order}))
  else
    render(:partial => 'effective/orders/checkout_step_2', :locals => locals.merge({:order => order}))
  end
end

#render_order(order) ⇒ Object



58
59
60
# File 'app/helpers/effective_orders_helper.rb', line 58

def render_order(order)
  render(:partial => 'effective/orders/order', :locals => {:order => order})
end

#render_order_history(user_or_orders, opts = {}) ⇒ Object Also known as: render_purchases, render_my_purchases



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/effective_orders_helper.rb', line 83

def render_order_history(user_or_orders, opts = {})
  if user_or_orders.kind_of?(User)
    orders = Effective::Order.purchased_by(user_or_orders)
  elsif user_or_orders.respond_to?(:to_a)
    begin
      orders = user_or_orders.to_a.select { |order| order.purchased? }
    rescue => e
      raise ArgumentError.new('expecting an instance of User or an array/collection of Effective::Order objects')
    end
  else
    raise ArgumentError.new('expecting an instance of User or an array/collection of Effective::Order objects')
  end

  locals = {
    :orders => orders,
    :order_path => effective_orders.order_path(':id') # The :id string will be replaced with the order id
  }.merge(opts)

  render(:partial => 'effective/orders/my_purchases', :locals => locals)
end