op_cart 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 555f3763a51291752b7b31d2cf101a3c722d8071
4
- data.tar.gz: 6383d96c5fa6673fe1997725c6dade67789ba2d7
3
+ metadata.gz: eb4504eab944521ca26e80d782e55b6c943aa7c7
4
+ data.tar.gz: a6c332fdbd12c6d11d5e94ac470b55f1a8b36c43
5
5
  SHA512:
6
- metadata.gz: 15d459050de75c8fb57f6882e936cf346d96890b4c683a94314715ae686627534c3a611d72b048e8b142953decabca8698cdc6d554baf449cd10a9b476ca26e7
7
- data.tar.gz: bf59c9119eb7d3567d8be022ef33dcb357b80cdef60d6aa12bffc51aee14e48f57e852f6a554f84b7182133fbf2141c98b6e6a09b63339700e1ba73208dc0808
6
+ metadata.gz: e8f24610a0805d6de1a5238e848fc221e843313870c8cbfccaf4240d548cd8e3c0ddd1046823db46f2c1cfab86ffa0b74c3250a6802f32c879ab5e46ed6ec02f
7
+ data.tar.gz: 5ec244260b96dd7ed28c8dbcb15d1d2bf5bedc520cdf6053c48c96b57fd582c1620b810b723a6aef1bc93f1f2f99d6711bde30caf40a5e4ae0dc7eab4075166d
@@ -1,16 +1,13 @@
1
1
  @OpCart =
2
2
  ready: ->
3
- if $('body').is '.op_cart-orders-new'
3
+ if $('body').is '.op_cart-orders-new, .op_cart-orders-create'
4
+ $city = $ '#order_shipping_address_city'
5
+ $state = $ '#order_shipping_address_state'
6
+ $zip = $ '#order_shipping_address_zip_code'
7
+
4
8
  $number = $ '#order_credit_cards_number'
5
9
  $expiry = $ '#order_credit_cards_expiry'
6
10
  $cvc = $ '#order_credit_cards_cvc'
7
- $city = $ '#order_shipping_addresses_city'
8
- $state = $ '#order_shipping_addresses_state'
9
- $zip = $ '#order_shipping_addresses_zip_code'
10
-
11
- $number.payment 'formatCardNumber'
12
- $expiry.payment 'formatCardExpiry'
13
- $cvc.payment 'formatCardCVC'
14
11
 
15
12
  $zip.change ->
16
13
  if $zip.val().length == 5
@@ -20,6 +17,12 @@
20
17
  $city.prop "disabled", false
21
18
  $state.prop "disabled", false
22
19
 
20
+ if $('#card_details')
21
+ $number.payment 'formatCardNumber'
22
+ $expiry.payment 'formatCardExpiry'
23
+ $cvc.payment 'formatCardCVC'
24
+
25
+ @updateDisplayedQuantities()
23
26
  @stripeCreateToken()
24
27
 
25
28
  load: ->
@@ -50,11 +53,12 @@
50
53
  if response.error
51
54
  errorMessage = response.error.message
52
55
  else
53
- errorMessage = 'Email or shipping information missing'
56
+ errorMessage = 'Email or shipping information missing' #TODO: what else is missing?
54
57
  $form.find(".payment-errors").text errorMessage
55
58
  $form.find("button").prop "disabled", false
56
59
  else
57
60
  $('#order_card_token').val response.id
61
+ $('#order_details').remove()
58
62
  $form.get(0).submit()
59
63
 
60
64
  addItemToOrder: (productId, quantity) ->
@@ -66,6 +70,9 @@
66
70
  @lineItemQuantity productId, 0
67
71
  @updateDisplayedQuantity productId
68
72
 
73
+ updateDisplayedQuantities: ->
74
+ $('li[data-product-id]').each -> OpCart.updateDisplayedQuantity $(@).data('product-id')
75
+
69
76
  updateDisplayedQuantity: (productId) ->
70
77
  $quantity = $ "#line_item_product_#{productId} .quantity .value"
71
78
  $quantity.html @lineItemQuantity(productId)
@@ -82,4 +89,3 @@
82
89
 
83
90
  $ -> OpCart.ready()
84
91
  $(document).on 'page:load', OpCart.load
85
-
@@ -9,9 +9,18 @@ module OpCart
9
9
  end
10
10
 
11
11
  def create
12
+ @products = Product.all
12
13
  @order = Order.new order_params
13
- @order.status = :new
14
- @order.user = current_user || User.first #TODO: Create a user from email
14
+ if current_user.blank? && user = User.find_by(email: params[:user][:email])
15
+ if user.valid_password? params[:user][:password]
16
+ sign_in user
17
+ else
18
+ flash[:alert] = 'A user with that email already exists. Please sign in or pick another email.'
19
+ return render :new
20
+ end
21
+ end
22
+ sign_in create_user unless signed_in?
23
+ @order.user = current_user
15
24
  add_line_items
16
25
 
17
26
  if @order.save
@@ -26,6 +35,11 @@ module OpCart
26
35
 
27
36
  private
28
37
 
38
+ def create_user
39
+ User.create name: params[:order][:shipping_address][:full_name], email: params[:user][:email],
40
+ password: params[:user][:password], password_confirmation: params[:user][:password]
41
+ end
42
+
29
43
  def set_order
30
44
  # TODO prevent viewing of others' orders
31
45
  # @order = current_user.orders.find params[:id]
@@ -33,13 +47,17 @@ module OpCart
33
47
  end
34
48
 
35
49
  def order_params
36
- params.require(:order).permit(:email, :card_token,
37
- { shipping_address: [ :full_name, :address, :zip_code, :city, :state ] }
50
+ params.require(:order).permit(:email, :password, :card_token#,
51
+ # { shipping_address: [ :full_name, :address, :zip_code, :city, :state ] },
38
52
  )
39
53
  end
40
54
 
55
+ def line_items_params
56
+ params.require(:line_items).permit :quantities
57
+ end
58
+
41
59
  def add_line_items
42
- li_quantities_json = JSON.parse params[:line_items][:quantities]
60
+ li_quantities_json = JSON.parse line_items_params[:quantities]
43
61
  li_quantities_json.each do |product_id, quantity|
44
62
  @order.line_items <<
45
63
  LineItem.new(sellable: Product.find(product_id), quantity: quantity)
@@ -1,18 +1,19 @@
1
1
  module OpCart
2
2
  class Order < ActiveRecord::Base
3
3
  has_many :line_items
4
+ has_one :shipping_address
4
5
  belongs_to :user
5
6
 
6
7
  accepts_nested_attributes_for :line_items
7
8
 
8
- validates :email, presence: true, format: { with: /@/ }
9
9
  validates :total, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
10
10
  validates :tax_amount, numericality:
11
- { only_integer: true, greater_than_or_equal_to: 0 } if :tax_amount?
11
+ { only_integer: true, greater_than_or_equal_to: 0 }, if: :tax_amount?
12
12
  validates :status, presence: true
13
13
  validates :user, presence: true
14
14
 
15
15
  before_validation :set_total
16
+ before_validation -> { self.status = :new }, unless: :status?
16
17
  before_save :charge_customer
17
18
 
18
19
  private
@@ -24,7 +25,7 @@ module OpCart
24
25
  def charge_customer
25
26
  customer = Stripe::Customer.create(
26
27
  card: card_token,
27
- email: email
28
+ email: user.email
28
29
  )
29
30
 
30
31
  # TODO Create shipping address
@@ -3,13 +3,12 @@
3
3
  h2 Purchasble Items
4
4
  ul style="list-style-type: none"
5
5
  - @products.each do |product|
6
- li
6
+ li id="#{dom_id product}" data-product-id=(product.id)
7
7
  p = product.name
8
8
  p = product.description
9
9
  p = number_to_currency product.price / 100
10
10
  p
11
- a onclick="OpCart.addItemToOrder(#{product.id}, 1)" style="cursor:pointer"
12
- | Add to Order
11
+ a href="#" onclick="OpCart.addItemToOrder(#{product.id}, 1); return false" Add to Order
13
12
 
14
13
  h2 Items in Order
15
14
  ul style="list-style-type: none"
@@ -21,33 +20,41 @@
21
20
  span.name Quantity:&nbsp;
22
21
  span.value 0
23
22
  p
24
- a onclick="OpCart.removeItemFromOrder(#{product.id})" style="cursor:pointer"
25
- | Remove
23
+ a href="#" onclick="OpCart.removeItemFromOrder(#{product.id}); return false" Remove
26
24
 
27
25
  = simple_form_for @order, data: { stripe_key: Rails.configuration.stripe[:publishable_key] } do |f|
28
26
  span.payment-errors
29
27
  p
30
28
  h2 Your Information
31
- = f.input :email, autofocus: true, input_html: { autocomplete: 'email' }
29
+ - if current_user.present?
30
+ strong Email:&nbsp;
31
+ span = current_user.email
32
+ - else
33
+ = simple_fields_for :user do |fu|
34
+ = fu.input :email, autofocus: true, input_html: { autocomplete: 'email' }
35
+ = fu.input :password
32
36
 
33
37
  p
34
38
  h2 Shipping Address
35
- = f.simple_fields_for :shipping_addresses do |sa|
39
+ = f.simple_fields_for :shipping_address do |sa|
36
40
  = sa.input :full_name, input_html: { autocomplete: 'name' }
37
41
  = sa.input :address, input_html: { autocomplete: 'street-address' }
38
42
  = sa.input :zip_code, input_html: { autocomplete: 'postal-code' }
39
43
  = sa.input :city, disabled: true, input_html: { autocomplete: 'city' }
40
44
  = sa.input :state, disabled: true, input_html: { autocomplete: 'state' }
41
45
 
42
- p
46
+ p#card_details
43
47
  h2 Card Details
44
- = f.simple_fields_for :credit_cards do |c|
45
- = c.input :number, input_html: { autocomplete: 'cc-number' }
46
- = c.input :expiry, input_html: { autocomplete: 'cc-exp' }
47
- = c.input :cvc, input_html: { autocomplete: 'cc-csc' }
48
+ - if f.object.card_token.present?
49
+ span Card information already provided.
50
+ - else
51
+ = f.simple_fields_for :credit_cards do |c|
52
+ = c.input :number, input_html: { autocomplete: 'cc-number' }
53
+ = c.input :expiry, input_html: { autocomplete: 'cc-exp' }
54
+ = c.input :cvc, input_html: { autocomplete: 'cc-csc' }
48
55
 
49
56
  = f.input :card_token, as: :hidden
50
- = hidden_field :line_items, :quantities, value: "{}"
57
+ = hidden_field :line_items, :quantities, value: params.fetch(:line_items, {}).fetch(:quantities, {})
51
58
 
52
59
  .form-actions
53
60
  = f.button :button, "Submit Payment"
@@ -15,7 +15,7 @@
15
15
 
16
16
  p
17
17
  h2 Your Information
18
- = @order.email
18
+ = @order.user.email
19
19
  p
20
20
  h2 Shipping Address
21
21
  p 123 N Todo Street
@@ -3,7 +3,7 @@ en:
3
3
  placeholders:
4
4
  order:
5
5
  email: "Email"
6
- shipping_addresses:
6
+ shipping_address:
7
7
  full_name: "Full Name"
8
8
  address: "Street Address"
9
9
  zip_code: "Zip Code"
@@ -1,7 +1,6 @@
1
1
  class CreateOpCartOrders < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :op_cart_orders do |t|
4
- t.string :email, null: false
5
4
  t.string :card_token, null: false
6
5
  t.integer :total, null: false
7
6
  t.integer :tax_amount, default: 0
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: op_cart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Boehs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-19 00:00:00.000000000 Z
11
+ date: 2015-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0.rc
19
+ version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0.rc
26
+ version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: slim-rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.16'
47
+ version: '1.18'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.16'
54
+ version: '1.18'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pg
57
57
  requirement: !ruby/object:Gem::Requirement