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



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

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


109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/effective_orders_helper.rb', line 109

def link_to_my_purchases(opts = {})
  options = {
    label: 'My Purchases',
    class: 'btn btn-default',
    rel: :nofollow
  }.merge(opts)

  label = options.delete(:label)
  options[:class] = ((options[:class] || '') + ' btn-my-purchases')

  link_to(label, effective_orders.my_purchases_path, options)
end

#order_checkout_label(processor = nil) ⇒ Object



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

def order_checkout_label(processor = nil)
  return 'Checkout' if (EffectiveOrders.single_payment_processor? && processor != :pretend)

  case processor
  when :free
    'Checkout'
  when :moneris, :stripe, :ccbill
    'Checkout with credit card'
  when :paypal
    'Checkout with PayPal'
  when :pretend
    EffectiveOrders.allow_pretend_purchase_in_production ? 'Purchase Order' : 'Purchase Order (development only)'
  when :cheque
    'Pay by Cheque'
  when :app_checkout
    EffectiveOrders.app_checkout[:checkout_label].presence || 'Checkout'
  else
    'Checkout'
  end
end

#order_item_summary(order_item) ⇒ Object



27
28
29
30
31
32
33
# File 'app/helpers/effective_orders_helper.rb', line 27

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/effective_orders_helper.rb', line 61

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



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/effective_orders_helper.rb', line 13

def order_summary(order)
  order_item_list = (: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
  (:p, "#{price_to_currency(order.total)} total for #{pluralize(order.num_items, 'item')}:") + order_item_list
end

#payment_card_label(card) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/helpers/effective_orders_helper.rb', line 169

def payment_card_label(card)
  card = card.to_s.downcase.gsub(' ', '').strip

  case card
  when ''
    'None'
  when 'v', 'visa'
    'Visa'
  when 'm', 'mc', 'master', 'mastercard'
    'MasterCard'
  when 'a', 'ax', 'american', 'americanexpress'
    'American Express'
  when 'd', 'discover'
    'Discover'
  else
    card.to_s
  end
end

#price_to_currency(price, options = {}) ⇒ Object



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

def price_to_currency(price, options = {})
  raise 'price_to_currency expects an Integer representing the number of cents in a price' unless price.kind_of?(Integer)
  options[:precision] ||= 2
  number_to_currency(price / 100.0, options)
end

#render_checkout_step1(order, opts = {}) ⇒ Object Also known as: render_checkout

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
# File 'app/helpers/effective_orders_helper.rb', line 88

def render_checkout_step1(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)

  render(partial: 'effective/orders/checkout_step1', locals: locals.merge({order: order}))
end

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

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/effective_orders_helper.rb', line 97

def render_checkout_step2(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_step1', locals: locals.merge({order: order}))
  else
    render(partial: 'effective/orders/checkout_step2', locals: locals.merge({order: order}))
  end
end

#render_order(order) ⇒ Object



84
85
86
# File 'app/helpers/effective_orders_helper.rb', line 84

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

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



123
124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/effective_orders_helper.rb', line 123

def render_orders(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)
    orders = user_or_orders.to_a
  else
    raise ArgumentError.new('expecting an instance of User or an array/collection of Effective::Order objects')
  end

  render(partial: 'effective/orders/orders_table', locals: {orders: orders}.merge(opts))
end

#tableize_order_payment(hash, options = {class: 'table table-bordered'}) ⇒ Object

Used by the _payment_details partial



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/effective_orders_helper.rb', line 140

def tableize_order_payment(hash, options = {class: 'table table-bordered'})
  if hash.present? && hash.kind_of?(Hash)
    (:table, class: options[:class]) do
      title = options.delete(:title)

      content = (:tbody) do
        hash.map do |k, v|
          (:tr) do
            ((options[:th] ? :th : :td), k) +
              (:td) do
                if v.kind_of?(Hash)
                  tableize_order_payment(v, options.merge(th: (options.key?(:sub_th) ? options[:sub_th] : options[:th])))
                elsif v.kind_of?(Array)
                  '[' + v.join(', ') + ']'
                else
                  v
                end
              end
          end
        end.join.html_safe
      end

      title.blank? ? content : ((:thead) { (:tr) { (:th, title, colspan: 2) } } + content)
    end
  else
    hash.to_s.html_safe
  end
end

#tax_rate_to_percentage(tax_rate, options = {}) ⇒ Object



8
9
10
11
# File 'app/helpers/effective_orders_helper.rb', line 8

def tax_rate_to_percentage(tax_rate, options = {})
  options[:strip_insignificant_zeros] = true if options[:strip_insignificant_zeros].nil?
  number_to_percentage(tax_rate, strip_insignificant_zeros: true)
end