op_cart 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb4504eab944521ca26e80d782e55b6c943aa7c7
4
- data.tar.gz: a6c332fdbd12c6d11d5e94ac470b55f1a8b36c43
3
+ metadata.gz: 32b387aaa77d790a4531b2632ee96549d64e9e19
4
+ data.tar.gz: f9d4749e1df95fa59d4e958afcfc6e1e8858791e
5
5
  SHA512:
6
- metadata.gz: e8f24610a0805d6de1a5238e848fc221e843313870c8cbfccaf4240d548cd8e3c0ddd1046823db46f2c1cfab86ffa0b74c3250a6802f32c879ab5e46ed6ec02f
7
- data.tar.gz: 5ec244260b96dd7ed28c8dbcb15d1d2bf5bedc520cdf6053c48c96b57fd582c1620b810b723a6aef1bc93f1f2f99d6711bde30caf40a5e4ae0dc7eab4075166d
6
+ metadata.gz: d15243927655156b24360107719bf6e2c08edb8f03d3c3332a3f8601416c4b51fde9f3eeb33b26d3c394a484deccda34e96d01fd4cf904c97931c33c897bc5ac
7
+ data.tar.gz: f9db8fd0ba1c303c0c76b95aa0badd6fc870ae5d577345eb899a6442dbb372593be8f749c6dd15e23267f54ea7124082fe9397dc9ca038c8894263360529f245
@@ -1,33 +1,37 @@
1
1
  @OpCart =
2
2
  ready: ->
3
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
-
8
- $number = $ '#order_credit_cards_number'
9
- $expiry = $ '#order_credit_cards_expiry'
10
- $cvc = $ '#order_credit_cards_cvc'
11
-
12
- $zip.change ->
13
- if $zip.val().length == 5
14
- $.ziptastic $zip.val(), (country, state, state_short, city, zip) ->
15
- $city.val city
16
- $state.val state
17
- $city.prop "disabled", false
18
- $state.prop "disabled", false
19
-
20
- if $('#card_details')
4
+ $number = $ '#credit_card_number'
5
+ $expiry = $ '#credit_card_expiry'
6
+ $cvc = $ '#credit_card_cvc'
7
+
8
+ if $('#card_form_inputs')
21
9
  $number.payment 'formatCardNumber'
22
10
  $expiry.payment 'formatCardExpiry'
23
11
  $cvc.payment 'formatCardCVC'
24
12
 
13
+ $('#shipping_address_postal_code').change => @updateZipFields()
14
+
15
+ @updateZipFields()
25
16
  @updateDisplayedQuantities()
26
17
  @stripeCreateToken()
27
18
 
28
19
  load: ->
29
20
  OpCart.ready()
30
21
 
22
+ updateZipFields: ->
23
+ $locality = $ '#shipping_address_locality'
24
+ $region = $ '#shipping_address_region'
25
+ $zip = $ '#shipping_address_postal_code'
26
+
27
+ if $zip.val().length == 5
28
+ $.ziptastic $zip.val(), (country, region, region_short, locality, zip) ->
29
+ $locality.val locality
30
+ $region.val region
31
+ $locality.prop "disabled", false
32
+ $region.prop "disabled", false
33
+
34
+
31
35
  stripeCreateToken: ->
32
36
  $("#new_order").submit (event) ->
33
37
  $form = $(this)
@@ -35,11 +39,11 @@
35
39
 
36
40
  Stripe.setPublishableKey $form.data("stripe-key")
37
41
 
38
- expiration = $("#order_credit_cards_expiry").payment "cardExpiryVal"
42
+ expiration = $("#credit_card_expiry").payment "cardExpiryVal"
39
43
 
40
44
  Stripe.card.createToken
41
- number: $("#order_credit_cards_number").val()
42
- cvc: $("#order_credit_cards_cvc").val()
45
+ number: $("#credit_card_number").val()
46
+ cvc: $("#credit_card_cvc").val()
43
47
  exp_month: expiration.month || 0
44
48
  exp_year: expiration.year || 0
45
49
  , OpCart.stripeResponseHandler
@@ -78,7 +82,7 @@
78
82
  $quantity.html @lineItemQuantity(productId)
79
83
 
80
84
  lineItemQuantity: (productId, quantity) ->
81
- $liQuantities = $ '#line_items_quantities'
85
+ $liQuantities = $ '#line_items_quantities_json'
82
86
  liQuantities = JSON.parse $liQuantities.val() || "{}"
83
87
 
84
88
  if quantity >= 0
@@ -4,65 +4,80 @@ module OpCart
4
4
  before_action :set_order, only: [:show, :edit, :update, :destroy]
5
5
 
6
6
  def new
7
- @products = Product.all
7
+ @products = Product.all #TODO: Move to find_products
8
8
  @order = Order.new
9
+ if signed_in?
10
+ @shipping_address = current_user.shipping_addresses.first
11
+ end
9
12
  end
10
13
 
11
14
  def create
12
15
  @products = Product.all
13
- @order = Order.new order_params
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
16
+ @order = Order.new order_params #TODO: Move to set_order
17
+ add_user
24
18
  add_line_items
19
+ add_shipping_address
25
20
 
26
- if @order.save
21
+ if signed_in? && @order.save
27
22
  redirect_to @order, notice: 'Thank you for your purchase'
28
23
  else
24
+ # TODO Set flash w/ error
29
25
  render :new
30
26
  end
31
27
  end
32
28
 
33
- def show
34
- end
29
+ def show; end
35
30
 
36
31
  private
37
32
 
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
-
43
33
  def set_order
44
- # TODO prevent viewing of others' orders
45
- # @order = current_user.orders.find params[:id]
46
- @order = Order.find params[:id]
34
+ @order = current_user.orders.find params[:id]
47
35
  end
48
36
 
49
37
  def order_params
50
- params.require(:order).permit(:email, :password, :card_token#,
51
- # { shipping_address: [ :full_name, :address, :zip_code, :city, :state ] },
52
- )
38
+ params.require(:order).permit :email, :password, :card_token
53
39
  end
54
40
 
55
41
  def line_items_params
56
- params.require(:line_items).permit :quantities
42
+ params.require(:line_items).permit :quantities_json
43
+ end
44
+
45
+ def shipping_address_params
46
+ params.require(:shipping_address).permit :full_name, :street, :postal_code,
47
+ :locality, :region
57
48
  end
58
49
 
59
50
  def add_line_items
60
- li_quantities_json = JSON.parse line_items_params[:quantities]
51
+ @line_items = OpenStruct.new(quantities_json: line_items_params[:quantities_json] || {})
52
+ li_quantities_json = JSON.parse @line_items.quantities_json
61
53
  li_quantities_json.each do |product_id, quantity|
62
54
  @order.line_items <<
63
55
  LineItem.new(sellable: Product.find(product_id), quantity: quantity)
64
56
  end
65
57
  end
66
58
 
59
+ def add_user
60
+ if current_user.blank? && user = User.find_by(email: params[:user][:email])
61
+ if user.valid_password? params[:user][:password]
62
+ sign_in user
63
+ else
64
+ flash[:alert] = 'A user with that email already exists. Please sign in or pick another email.'
65
+ return false
66
+ end
67
+ end
68
+
69
+ unless signed_in?
70
+ sign_in User.create name: params[:order][:shipping_address][:full_name],
71
+ email: params[:user][:email], password: params[:user][:password],
72
+ password_confirmation: params[:user][:password]
73
+ end
74
+ @order.user = current_user
75
+ end
76
+
77
+ def add_shipping_address
78
+ @order.shipping_address = @shipping_address ||= ShippingAddress.create(
79
+ shipping_address_params.merge user_id: current_user.try(:id)
80
+ )
81
+ end
67
82
  end
68
83
  end
@@ -1,7 +1,7 @@
1
1
  module OpCart
2
2
  class Order < ActiveRecord::Base
3
3
  has_many :line_items
4
- has_one :shipping_address
4
+ belongs_to :shipping_address
5
5
  belongs_to :user
6
6
 
7
7
  accepts_nested_attributes_for :line_items
@@ -0,0 +1,9 @@
1
+ module OpCart
2
+ class ShippingAddress < ActiveRecord::Base
3
+ has_many :orders
4
+ belongs_to :user
5
+
6
+ validates :full_name, :street, :locality, :region, :postal_code, presence: true
7
+ validates :user, presence: true
8
+ end
9
+ end
@@ -26,7 +26,7 @@
26
26
  span.payment-errors
27
27
  p
28
28
  h2 Your Information
29
- - if current_user.present?
29
+ - if signed_in?
30
30
  strong Email:&nbsp;
31
31
  span = current_user.email
32
32
  - else
@@ -36,25 +36,28 @@
36
36
 
37
37
  p
38
38
  h2 Shipping Address
39
- = f.simple_fields_for :shipping_address do |sa|
40
- = sa.input :full_name, input_html: { autocomplete: 'name' }
41
- = sa.input :address, input_html: { autocomplete: 'street-address' }
42
- = sa.input :zip_code, input_html: { autocomplete: 'postal-code' }
43
- = sa.input :city, disabled: true, input_html: { autocomplete: 'city' }
44
- = sa.input :state, disabled: true, input_html: { autocomplete: 'state' }
39
+ = simple_fields_for :shipping_address do |sa|
40
+ - focus_name = signed_in? ? true : false
41
+ = sa.input :full_name, autofocus: focus_name, input_html: { autocomplete: 'name' }
42
+ = sa.input :street, input_html: { autocomplete: 'street-address' }
43
+ = sa.input :postal_code, input_html: { autocomplete: 'postal-code' }
44
+ = sa.input :locality, disabled: true, input_html: { autocomplete: 'city' }
45
+ = sa.input :region, disabled: true, input_html: { autocomplete: 'state' }
45
46
 
46
- p#card_details
47
+ p
47
48
  h2 Card Details
48
49
  - if f.object.card_token.present?
49
50
  span Card information already provided.
50
51
  - 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' }
52
+ #card_form_inputs
53
+ = simple_fields_for :credit_card do |c|
54
+ = c.input :number, input_html: { autocomplete: 'cc-number' }
55
+ = c.input :expiry, input_html: { autocomplete: 'cc-exp' }
56
+ = c.input :cvc, input_html: { autocomplete: 'cc-csc' }
55
57
 
58
+ = simple_fields_for :line_items do |li|
59
+ = li.input :quantities_json, as: :hidden
56
60
  = f.input :card_token, as: :hidden
57
- = hidden_field :line_items, :quantities, value: params.fetch(:line_items, {}).fetch(:quantities, {})
58
61
 
59
62
  .form-actions
60
63
  = f.button :button, "Submit Payment"
@@ -18,5 +18,7 @@
18
18
  = @order.user.email
19
19
  p
20
20
  h2 Shipping Address
21
- p 123 N Todo Street
22
- p Edmond, Oklahoma 73013
21
+ - sa = @order.shipping_address
22
+ p = sa.full_name
23
+ p = sa.street
24
+ p = "#{sa.locality}, #{sa.region} #{sa.postal_code}"
@@ -3,13 +3,13 @@ en:
3
3
  placeholders:
4
4
  order:
5
5
  email: "Email"
6
- shipping_address:
7
- full_name: "Full Name"
8
- address: "Street Address"
9
- zip_code: "Zip Code"
10
- city: "City"
11
- state: "State"
12
- credit_cards:
13
- number: "Card Number"
14
- expiry: 'MM/YY'
15
- cvc: 'CVC'
6
+ credit_card:
7
+ number: "Card Number"
8
+ expiry: 'MM/YY'
9
+ cvc: 'CVC'
10
+ shipping_address:
11
+ full_name: "Full Name"
12
+ street: "Street Address"
13
+ locality: "City"
14
+ region: "State"
15
+ postal_code: "Zip Code"
@@ -5,6 +5,7 @@ class CreateOpCartOrders < ActiveRecord::Migration
5
5
  t.integer :total, null: false
6
6
  t.integer :tax_amount, default: 0
7
7
  t.string :status, null: false
8
+ t.references :shipping_address, index: true, null: false
8
9
  t.references :user, index: true, null: false
9
10
 
10
11
  t.timestamps null: false
@@ -0,0 +1,15 @@
1
+ class CreateOpCartShippingAddresses < ActiveRecord::Migration
2
+ def change
3
+ create_table :op_cart_shipping_addresses do |t|
4
+ t.string :full_name, null: false
5
+ t.string :street, null: false
6
+ t.string :street_2
7
+ t.string :locality, null: false
8
+ t.string :region, null: false
9
+ t.string :postal_code, null: false
10
+ t.references :user, index: true
11
+
12
+ t.timestamps null: false
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: op_cart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Boehs
@@ -86,6 +86,7 @@ files:
86
86
  - app/models/op_cart/line_item.rb
87
87
  - app/models/op_cart/order.rb
88
88
  - app/models/op_cart/product.rb
89
+ - app/models/op_cart/shipping_address.rb
89
90
  - app/views/op_cart/orders/new.html.slim
90
91
  - app/views/op_cart/orders/show.html.slim
91
92
  - config/initializers/stripe.rb
@@ -94,6 +95,7 @@ files:
94
95
  - db/migrate/20140829191646_create_op_cart_products.rb
95
96
  - db/migrate/20140829200318_create_op_cart_orders.rb
96
97
  - db/migrate/20140829201756_create_op_cart_line_items.rb
98
+ - db/migrate/20150125000000_create_op_cart_shipping_addresses.rb
97
99
  - db/seeds.rb
98
100
  - lib/op_cart.rb
99
101
  - lib/op_cart/engine.rb