spree_api 1.3.5 → 2.0.0.rc1

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.
Files changed (91) hide show
  1. checksums.yaml +14 -6
  2. data/Rakefile +1 -1
  3. data/app/controllers/spree/api/addresses_controller.rb +0 -1
  4. data/app/controllers/spree/api/base_controller.rb +21 -9
  5. data/app/controllers/spree/api/checkouts_controller.rb +40 -20
  6. data/app/controllers/spree/api/countries_controller.rb +0 -1
  7. data/app/controllers/spree/api/inventory_units_controller.rb +1 -1
  8. data/app/controllers/spree/api/option_types_controller.rb +46 -0
  9. data/app/controllers/spree/api/option_values_controller.rb +56 -0
  10. data/app/controllers/spree/api/orders_controller.rb +1 -21
  11. data/app/controllers/spree/api/product_properties_controller.rb +0 -1
  12. data/app/controllers/spree/api/products_controller.rb +12 -27
  13. data/app/controllers/spree/api/properties_controller.rb +61 -0
  14. data/app/controllers/spree/api/shipments_controller.rb +51 -1
  15. data/app/controllers/spree/api/states_controller.rb +32 -0
  16. data/app/controllers/spree/api/stock_items_controller.rb +70 -0
  17. data/app/controllers/spree/api/stock_locations_controller.rb +45 -0
  18. data/app/controllers/spree/api/stock_movements_controller.rb +55 -0
  19. data/app/controllers/spree/api/taxonomies_controller.rb +5 -0
  20. data/app/controllers/spree/api/taxons_controller.rb +16 -2
  21. data/app/controllers/spree/api/variants_controller.rb +1 -1
  22. data/app/controllers/spree/api/zones_controller.rb +0 -1
  23. data/app/helpers/spree/api/api_helpers.rb +34 -7
  24. data/app/models/spree/api_configuration.rb +1 -2
  25. data/app/models/spree/line_item_decorator.rb +1 -1
  26. data/app/models/spree/order_decorator.rb +6 -169
  27. data/app/overrides/api_admin_user_edit_form.rb +7 -6
  28. data/app/views/spree/admin/users/_api_fields.html.erb +7 -8
  29. data/app/views/spree/api/addresses/show.v1.rabl +2 -2
  30. data/app/views/spree/api/adjustments/show.v1.rabl +2 -0
  31. data/app/views/spree/api/credit_cards/show.v1.rabl +2 -0
  32. data/app/views/spree/api/option_types/index.v1.rabl +3 -0
  33. data/app/views/spree/api/option_types/show.v1.rabl +5 -0
  34. data/app/views/spree/api/option_values/index.v1.rabl +3 -0
  35. data/app/views/spree/api/option_values/show.v1.rabl +2 -0
  36. data/app/views/spree/api/orders/could_not_apply_coupon.v1.rabl +2 -0
  37. data/app/views/spree/api/orders/delivery.v1.rabl +2 -2
  38. data/app/views/spree/api/orders/payment.v1.rabl +3 -0
  39. data/app/views/spree/api/orders/show.v1.rabl +8 -0
  40. data/app/views/spree/api/properties/index.v1.rabl +7 -0
  41. data/app/views/spree/api/properties/new.v1.rabl +2 -0
  42. data/app/views/spree/api/properties/show.v1.rabl +2 -0
  43. data/app/views/spree/api/shared/stock_location_required.v1.rabl +2 -0
  44. data/app/views/spree/api/shipments/show.v1.rabl +7 -0
  45. data/app/views/spree/api/states/index.v1.rabl +14 -0
  46. data/app/views/spree/api/states/show.v1.rabl +2 -0
  47. data/app/views/spree/api/stock_items/index.v1.rabl +7 -0
  48. data/app/views/spree/api/stock_items/show.v1.rabl +5 -0
  49. data/app/views/spree/api/stock_locations/index.v1.rabl +7 -0
  50. data/app/views/spree/api/stock_locations/show.v1.rabl +8 -0
  51. data/app/views/spree/api/stock_movements/index.v1.rabl +7 -0
  52. data/app/views/spree/api/stock_movements/show.v1.rabl +5 -0
  53. data/app/views/spree/api/taxonomies/jstree.rabl +8 -0
  54. data/app/views/spree/api/taxons/jstree.rabl +8 -0
  55. data/app/views/spree/api/variants/index.v1.rabl +14 -1
  56. data/app/views/spree/api/variants/variant.v1.rabl +0 -4
  57. data/config/locales/en.yml +1 -0
  58. data/config/routes.rb +29 -3
  59. data/lib/spree/api.rb +0 -2
  60. data/lib/spree/api/controller_setup.rb +6 -1
  61. data/lib/spree/api/testing_support/helpers.rb +1 -1
  62. data/lib/spree/api/testing_support/setup.rb +13 -0
  63. data/spec/controllers/spree/api/base_controller_spec.rb +14 -1
  64. data/spec/controllers/spree/api/checkouts_controller_spec.rb +59 -28
  65. data/spec/controllers/spree/api/line_items_controller_spec.rb +1 -5
  66. data/spec/controllers/spree/api/option_types_controller_spec.rb +116 -0
  67. data/spec/controllers/spree/api/option_values_controller_spec.rb +128 -0
  68. data/spec/controllers/spree/api/orders_controller_spec.rb +11 -93
  69. data/spec/controllers/spree/api/product_properties_controller_spec.rb +2 -2
  70. data/spec/controllers/spree/api/products_controller_spec.rb +18 -76
  71. data/spec/controllers/spree/api/properties_controller_spec.rb +89 -0
  72. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +1 -9
  73. data/spec/controllers/spree/api/shipments_controller_spec.rb +80 -5
  74. data/spec/controllers/spree/api/states_controller_spec.rb +76 -0
  75. data/spec/controllers/spree/api/stock_items_controller_spec.rb +88 -0
  76. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +86 -0
  77. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +72 -0
  78. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +7 -0
  79. data/spec/controllers/spree/api/taxons_controller_spec.rb +29 -2
  80. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +1 -1
  81. data/spec/controllers/spree/api/variants_controller_spec.rb +5 -16
  82. data/spec/fixtures/thinking-cat.jpg +0 -0
  83. data/spec/models/spree/order_spec.rb +7 -328
  84. data/spec/spec_helper.rb +18 -2
  85. data/spree_api.gemspec +2 -1
  86. metadata +67 -16
  87. data/.rspec +0 -1
  88. data/app/controllers/spree/base_controller_decorator.rb +0 -15
  89. data/app/overrides/api_key_spree_layout.rb +0 -7
  90. data/app/views/spree/api/_key.html.erb +0 -4
  91. data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +0 -7
@@ -4,7 +4,39 @@ module Spree
4
4
  respond_to :json
5
5
 
6
6
  before_filter :find_order
7
- before_filter :find_and_update_shipment, :only => [:ship, :ready]
7
+ before_filter :find_and_update_shipment, :only => [:ship, :ready, :add, :remove]
8
+
9
+ def create
10
+ variant = Spree::Variant.find(params[:variant_id])
11
+ quantity = params[:quantity].to_i
12
+ @shipment = @order.shipments.create(:stock_location_id => params[:stock_location_id])
13
+ @order.contents.add(variant, quantity, nil, @shipment)
14
+
15
+ @shipment.refresh_rates
16
+ @shipment.save!
17
+
18
+ respond_with(@shipment.reload, :default_template => :show)
19
+ end
20
+
21
+ def update
22
+ authorize! :read, Shipment
23
+ @shipment = @order.shipments.find_by_number!(params[:id])
24
+
25
+ unlock = params[:shipment].delete(:unlock)
26
+
27
+ if unlock == 'yes'
28
+ @shipment.adjustment.open
29
+ end
30
+
31
+ @shipment.update_attributes(params[:shipment])
32
+
33
+ if unlock == 'yes'
34
+ @shipment.adjustment.close
35
+ end
36
+
37
+ @shipment.reload
38
+ respond_with(@shipment, :default_template => :show)
39
+ end
8
40
 
9
41
  def ready
10
42
  authorize! :read, Shipment
@@ -26,6 +58,24 @@ module Spree
26
58
  respond_with(@shipment, :default_template => :show)
27
59
  end
28
60
 
61
+ def add
62
+ variant = Spree::Variant.find(params[:variant_id])
63
+ quantity = params[:quantity].to_i
64
+
65
+ @order.contents.add(variant, quantity, nil, @shipment)
66
+
67
+ respond_with(@shipment, :default_template => :show)
68
+ end
69
+
70
+ def remove
71
+ variant = Spree::Variant.find(params[:variant_id])
72
+ quantity = params[:quantity].to_i
73
+
74
+ @order.contents.remove(variant, quantity, @shipment)
75
+
76
+ respond_with(@shipment, :default_template => :show)
77
+ end
78
+
29
79
  private
30
80
 
31
81
  def find_order
@@ -0,0 +1,32 @@
1
+ module Spree
2
+ module Api
3
+ class StatesController < Spree::Api::BaseController
4
+
5
+ def index
6
+ @states = scope.ransack(params[:q]).result.
7
+ includes(:country).order('name ASC')
8
+
9
+ if params[:page] || params[:per_page]
10
+ @states = @states.page(params[:page]).per(params[:per_page])
11
+ end
12
+
13
+ respond_with(@states)
14
+ end
15
+
16
+ def show
17
+ @state = scope.find(params[:id])
18
+ respond_with(@state)
19
+ end
20
+
21
+ private
22
+ def scope
23
+ if params[:country_id]
24
+ @country = Country.find(params[:country_id])
25
+ return @country.states
26
+ else
27
+ return State.scoped
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,70 @@
1
+ module Spree
2
+ module Api
3
+ class StockItemsController < Spree::Api::BaseController
4
+ before_filter :stock_location, except: [:update, :destroy]
5
+
6
+ def index
7
+ @stock_items = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
8
+ respond_with(@stock_items)
9
+ end
10
+
11
+ def show
12
+ @stock_item = scope.find(params[:id])
13
+ respond_with(@stock_item)
14
+ end
15
+
16
+ def create
17
+ authorize! :create, StockItem
18
+
19
+ count_on_hand = 0
20
+ if params[:stock_item].has_key?(:count_on_hand)
21
+ count_on_hand = params[:stock_item][:count_on_hand].to_i
22
+ params[:stock_item].delete(:count_on_hand)
23
+ end
24
+
25
+ @stock_item = scope.new(params[:stock_item])
26
+ if @stock_item.save
27
+ @stock_item.adjust_count_on_hand(count_on_hand)
28
+ respond_with(@stock_item, status: 201, default_template: :show)
29
+ else
30
+ invalid_resource!(@stock_item)
31
+ end
32
+ end
33
+
34
+ def update
35
+ authorize! :update, StockItem
36
+ @stock_item = StockItem.find(params[:id])
37
+
38
+ count_on_hand = 0
39
+ if params[:stock_item].has_key?(:count_on_hand)
40
+ count_on_hand = params[:stock_item][:count_on_hand].to_i
41
+ params[:stock_item].delete(:count_on_hand)
42
+ end
43
+
44
+ if @stock_item.adjust_count_on_hand(count_on_hand)
45
+ respond_with(@stock_item, status: 200, default_template: :show)
46
+ else
47
+ invalid_resource!(@stock_item)
48
+ end
49
+ end
50
+
51
+ def destroy
52
+ authorize! :delete, StockItem
53
+ @stock_item = StockItem.find(params[:id])
54
+ @stock_item.destroy
55
+ respond_with(@stock_item, status: 204)
56
+ end
57
+
58
+ private
59
+
60
+ def stock_location
61
+ render 'spree/api/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
62
+ @stock_location ||= StockLocation.find(params[:stock_location_id])
63
+ end
64
+
65
+ def scope
66
+ @stock_location.stock_items.includes(:variant => :product)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,45 @@
1
+ module Spree
2
+ module Api
3
+ class StockLocationsController < Spree::Api::BaseController
4
+ def index
5
+ @stock_locations = StockLocation.order('name ASC').ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
6
+ respond_with(@stock_locations)
7
+ end
8
+
9
+ def show
10
+ respond_with(stock_location)
11
+ end
12
+
13
+ def create
14
+ authorize! :create, StockLocation
15
+ @stock_location = StockLocation.new(params[:stock_location])
16
+ if @stock_location.save
17
+ respond_with(@stock_location, status: 201, default_template: :show)
18
+ else
19
+ invalid_resource!(@stock_location)
20
+ end
21
+ end
22
+
23
+ def update
24
+ authorize! :update, StockLocation
25
+ if stock_location.update_attributes(params[:stock_location])
26
+ respond_with(stock_location, status: 200, default_template: :show)
27
+ else
28
+ invalid_resource!(stock_location)
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ authorize! :delete, StockLocation
34
+ stock_location.destroy
35
+ respond_with(stock_location, :status => 204)
36
+ end
37
+
38
+ private
39
+
40
+ def stock_location
41
+ @stock_location ||= StockLocation.find(params[:id])
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,55 @@
1
+ module Spree
2
+ module Api
3
+ class StockMovementsController < Spree::Api::BaseController
4
+ before_filter :stock_location, except: [:update, :destroy]
5
+
6
+ def index
7
+ @stock_movements = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
8
+ respond_with(@stock_movements)
9
+ end
10
+
11
+ def show
12
+ @stock_movement = scope.find(params[:id])
13
+ respond_with(@stock_movement)
14
+ end
15
+
16
+ def create
17
+ authorize! :create, StockMovement
18
+ @stock_movement = scope.new(params[:stock_movement])
19
+ if @stock_movement.save
20
+ respond_with(@stock_movement, status: 201, default_template: :show)
21
+ else
22
+ invalid_resource!(@stock_movement)
23
+ end
24
+ end
25
+
26
+ def update
27
+ authorize! :update, StockMovement
28
+ @stock_movement = StockMovement.find(params[:id])
29
+ if @stock_movement.update_attributes(params[:stock_movement])
30
+ respond_with(@stock_movement, status: 200, default_template: :show)
31
+ else
32
+ invalid_resource!(@stock_movement)
33
+ end
34
+ end
35
+
36
+ def destroy
37
+ authorize! :delete, StockMovement
38
+ @stock_movement = StockMovement.find(params[:id])
39
+ @stock_movement.destroy
40
+ respond_with(@stock_movement, status: 204)
41
+ end
42
+
43
+ private
44
+
45
+ def stock_location
46
+ render 'spree/api/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
47
+ @stock_location ||= StockLocation.find(params[:stock_location_id])
48
+ end
49
+
50
+ def scope
51
+ @stock_location.stock_movements
52
+ end
53
+ end
54
+ end
55
+ end
@@ -15,6 +15,11 @@ module Spree
15
15
  respond_with(@taxonomy)
16
16
  end
17
17
 
18
+ # Because JSTree wants parameters in a *slightly* different format
19
+ def jstree
20
+ show
21
+ end
22
+
18
23
  def create
19
24
  authorize! :create, Taxonomy
20
25
  @taxonomy = Taxonomy.new(params[:taxonomy])
@@ -4,7 +4,15 @@ module Spree
4
4
  respond_to :json
5
5
 
6
6
  def index
7
- @taxons = taxonomy.root.children
7
+ if taxonomy
8
+ @taxons = taxonomy.root.children
9
+ else
10
+ if params[:ids]
11
+ @taxons = Taxon.where(:id => params[:ids].split(","))
12
+ else
13
+ @taxons = Taxon.ransack(params[:q]).result
14
+ end
15
+ end
8
16
  respond_with(@taxons)
9
17
  end
10
18
 
@@ -13,6 +21,10 @@ module Spree
13
21
  respond_with(@taxon)
14
22
  end
15
23
 
24
+ def jstree
25
+ show
26
+ end
27
+
16
28
  def create
17
29
  authorize! :create, Taxon
18
30
  @taxon = Taxon.new(params[:taxon])
@@ -51,7 +63,9 @@ module Spree
51
63
  private
52
64
 
53
65
  def taxonomy
54
- @taxonomy ||= Taxonomy.find(params[:taxonomy_id])
66
+ if params[:taxonomy_id].present?
67
+ @taxonomy ||= Taxonomy.find(params[:taxonomy_id])
68
+ end
55
69
  end
56
70
 
57
71
  def taxon
@@ -68,7 +68,7 @@ module Spree
68
68
  variants = variants.active
69
69
  end
70
70
  end
71
- variants.readonly(false)
71
+ variants
72
72
  end
73
73
  end
74
74
  end
@@ -1,7 +1,6 @@
1
1
  module Spree
2
2
  module Api
3
3
  class ZonesController < Spree::Api::BaseController
4
- respond_to :json
5
4
 
6
5
  def index
7
6
  @zones = Zone.order('name ASC').ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
@@ -12,8 +12,7 @@ module Spree
12
12
  end
13
13
 
14
14
  def product_attributes
15
- [:id, :name, :description, :price, :available_on, :permalink,
16
- :count_on_hand, :meta_description, :meta_keywords, :taxon_ids]
15
+ [:id, :name, :description, :price, :available_on, :permalink, :meta_description, :meta_keywords, :taxon_ids]
17
16
  end
18
17
 
19
18
  def product_property_attributes
@@ -21,7 +20,7 @@ module Spree
21
20
  end
22
21
 
23
22
  def variant_attributes
24
- [:id, :name, :count_on_hand, :sku, :price, :weight, :height, :width, :depth, :is_master, :cost_price, :permalink, :product_id, :lock_version]
23
+ [:id, :name, :sku, :price, :weight, :height, :width, :depth, :is_master, :cost_price, :permalink]
25
24
  end
26
25
 
27
26
  def image_attributes
@@ -33,7 +32,7 @@ module Spree
33
32
  end
34
33
 
35
34
  def order_attributes
36
- [:id, :number, :item_total, :total, :state, :adjustment_total, :user_id, :created_at, :updated_at, :completed_at, :payment_total, :shipment_state, :payment_state, :email, :special_instructions, :channel]
35
+ [:id, :number, :item_total, :total, :state, :adjustment_total, :user_id, :created_at, :updated_at, :completed_at, :payment_total, :shipment_state, :payment_state, :email, :special_instructions]
37
36
  end
38
37
 
39
38
  def line_item_attributes
@@ -61,12 +60,11 @@ module Spree
61
60
  end
62
61
 
63
62
  def taxon_attributes
64
- [:id, :name, :permalink, :position, :parent_id, :taxonomy_id]
63
+ [:id, :name, :pretty_name, :permalink, :position, :parent_id, :taxonomy_id]
65
64
  end
66
65
 
67
66
  def inventory_unit_attributes
68
- [:id, :lock_version, :state, :variant_id, :order_id,
69
- :shipment_id, :return_authorization_id]
67
+ [:id, :lock_version, :state, :variant_id, :shipment_id, :return_authorization_id]
70
68
  end
71
69
 
72
70
  def return_authorization_attributes
@@ -77,9 +75,38 @@ module Spree
77
75
  [:id, :iso_name, :iso, :iso3, :name, :numcode]
78
76
  end
79
77
 
78
+ def state_attributes
79
+ [:id, :name, :abbr, :country_id]
80
+ end
81
+
82
+ def adjustment_attributes
83
+ [:id, :source_type, :source_id, :adjustable_type, :adjustable_id, :originator_type, :originator_id, :amount, :label, :mandatory, :locked, :eligible, :created_at, :updated_at]
84
+ end
85
+
86
+ def creditcard_attributes
87
+ [:id, :month, :year, :cc_type, :last_digits, :first_name, :last_name, :gateway_customer_profile_id, :gateway_payment_profile_id]
88
+ end
89
+
80
90
  def user_attributes
81
91
  [:id, :email, :created_at, :updated_at]
82
92
  end
93
+
94
+ def property_attributes
95
+ [:id, :name, :presentation]
96
+ end
97
+
98
+ def stock_location_attributes
99
+ [:id, :name, :address1, :address2, :city, :state_id, :state_name, :country_id, :zipcode, :phone, :active]
100
+ end
101
+
102
+ def stock_movement_attributes
103
+ [:id, :quantity, :stock_item_id]
104
+ end
105
+
106
+ def stock_item_attributes
107
+ [:id, :count_on_hand, :backorderable, :lock_version, :stock_location_id, :variant_id]
108
+ end
83
109
  end
84
110
  end
85
111
  end
112
+
@@ -1,6 +1,5 @@
1
1
  module Spree
2
2
  class ApiConfiguration < Preferences::Configuration
3
- # Make sure the api requires authentication by default to prevent unwanted access
4
- preference :requires_authentication, :boolean, :default => true
3
+ preference :requires_authentication, :boolean, :default => false
5
4
  end
6
5
  end
@@ -1,3 +1,3 @@
1
1
  Spree::LineItem.class_eval do
2
- attr_accessible :quantity, :variant_id, :sku, :as => :api
2
+ attr_accessible :quantity, :variant_id, :as => :api
3
3
  end
@@ -1,176 +1,13 @@
1
1
  Spree::Order.class_eval do
2
- attr_accessible :channel, :as => :api_admin
3
-
4
2
  def self.build_from_api(user, params)
5
- begin
6
- ensure_country_id_from_api params[:ship_address_attributes]
7
- ensure_state_id_from_api params[:ship_address_attributes]
8
- ensure_country_id_from_api params[:bill_address_attributes]
9
- ensure_state_id_from_api params[:bill_address_attributes]
10
-
11
- order = create!
12
-
13
- order.create_shipments_from_api params.delete(:shipments_attributes) || []
14
- order.create_line_items_from_api params.delete(:line_items_attributes) || {}
15
- order.create_adjustments_from_api params.delete(:adjustments_attributes) || []
16
- order.create_payments_from_api params.delete(:payments_attributes) || []
17
- order.complete_from_api params.delete(:completed_at)
18
-
19
- destroy_automatic_taxes_on_import(order, params)
20
-
21
- if user.has_spree_role? "admin"
22
- order.update_attributes!(params, without_protection: true)
23
- else
24
- order.update_attributes!(params)
25
- end
26
-
27
- order.reload
28
- rescue Exception => e
29
- order.destroy if order && order.persisted?
30
- raise e.message
31
- end
32
- end
33
-
34
- def self.destroy_automatic_taxes_on_import(order, params)
35
- if params.delete :import
36
- order.adjustments.tax.destroy_all
37
- end
38
- end
39
-
40
- def complete_from_api(completed_at)
41
- if completed_at
42
- self.completed_at = completed_at
43
- self.state = 'complete'
44
- end
45
- end
46
-
47
- def create_shipments_from_api(shipments_hash)
48
- shipments_hash.each do |s|
49
- begin
50
- shipment = shipments.build
51
- shipment.tracking = s[:tracking]
52
-
53
- inventory_units = s[:inventory_units] || []
54
- inventory_units.each do |iu|
55
- self.class.ensure_variant_id_from_api(iu)
56
-
57
- unit = shipment.inventory_units.build
58
- unit.order = self
59
- unit.variant_id = iu[:variant_id]
60
- end
61
-
62
- shipment.shipping_method = Spree::ShippingMethod.find_by_name!(s[:shipping_method])
63
- shipment.save!
64
-
65
- shipment.adjustment.locked = true
66
- shipment.adjustment.amount = s[:cost].to_f
67
- shipment.adjustment.save
68
- rescue Exception => e
69
- raise "Order import shipments: #{e.message} #{s}"
70
- end
71
- end
72
- end
73
-
74
- def create_payments_from_api(payments_hash)
75
- payments_hash.each do |p|
76
- begin
77
- payment = payments.build
78
- payment.amount = p[:amount].to_f
79
- payment.state = p.fetch(:state, 'completed')
80
- payment.payment_method = Spree::PaymentMethod.find_by_name!(p[:payment_method])
81
- payment.save!
82
- rescue Exception => e
83
- raise "Order import payments: #{e.message} #{p}"
84
- end
85
- end
86
- end
87
-
88
- def create_line_items_from_api(line_items_hash)
89
- line_items_hash.each_key do |k|
90
- begin
91
- line_item = line_items_hash[k]
92
- self.class.ensure_variant_id_from_api(line_item)
93
-
94
- item = self.add_variant(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity].to_i)
95
-
96
- if line_item.key? :price
97
- item.price = line_item[:price]
98
- item.save!
99
- end
100
- rescue Exception => e
101
- raise "Order import line items: #{e.message} #{line_item}"
102
- end
103
- end
104
- end
105
-
106
- def create_adjustments_from_api(adjustments)
107
- adjustments.each do |a|
108
- begin
109
- adjustment = self.adjustments.build(:amount => a['amount'].to_f,
110
- :label => a['label'])
111
- adjustment.locked = true
112
- adjustment.save!
113
- rescue Exception => e
114
- raise "Order import adjustments: #{e.message} #{a}"
115
- end
116
- end
117
- end
118
-
119
- def self.ensure_variant_id_from_api(hash)
120
- begin
121
- unless hash[:variant_id].present?
122
- hash[:variant_id] = Spree::Variant.active.find_by_sku!(hash[:sku]).id
123
- hash.delete(:sku)
124
- end
125
- rescue Exception => e
126
- raise "Ensure order import variant: #{e.message} #{hash}"
127
- end
128
- end
129
-
130
- def self.ensure_country_id_from_api(address)
131
- return if address.nil? or address[:country_id].present? or address[:country].nil?
132
-
133
- begin
134
- search = {}
135
- if name = address[:country]['name']
136
- search[:name] = name
137
- elsif iso_name = address[:country]['iso_name']
138
- search[:iso_name] = iso_name.upcase
139
- elsif iso = address[:country]['iso']
140
- search[:iso] = iso.upcase
141
- elsif iso3 = address[:country]['iso3']
142
- search[:iso3] = iso3.upcase
3
+ order = create
4
+ params[:line_items_attributes] ||= []
5
+ unless params[:line_items_attributes].empty?
6
+ params[:line_items_attributes].each_key do |k|
7
+ order.contents.add(Spree::Variant.find(params[:line_items_attributes][k][:variant_id]), params[:line_items_attributes][k][:quantity])
143
8
  end
144
-
145
- address.delete(:country)
146
- address[:country_id] = Spree::Country.where(search).first!.id
147
-
148
- rescue Exception => e
149
- raise "Ensure order import address country: #{e.message} #{search}"
150
9
  end
151
- end
152
10
 
153
- def self.ensure_state_id_from_api(address)
154
- return if address.nil? or address[:state_id].present? or address[:state].nil?
155
-
156
- begin
157
- search = {}
158
- if name = address[:state]['name']
159
- search[:name] = name
160
- elsif abbr = address[:state]['abbr']
161
- search[:abbr] = abbr.upcase
162
- end
163
-
164
- address.delete(:state)
165
- search[:country_id] = address[:country_id]
166
-
167
- if state = Spree::State.where(search).first
168
- address[:state_id] = state.id
169
- else
170
- address[:state_name] = search[:name] || search[:abbr]
171
- end
172
- rescue Exception => e
173
- raise "Ensure order import address state: #{e.message} #{search}"
174
- end
11
+ order
175
12
  end
176
13
  end