spree_api 2.1.2 → 2.1.3

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: 2b88f0bb66f6241fa750c60df09e559515380ff9
4
- data.tar.gz: c8c8111f3b16f720c3219105faf7d5a912dc5c45
3
+ metadata.gz: 40032b519232965869e9496c081b35073f3befff
4
+ data.tar.gz: 27e40bed61616ece0b4b48f569296a878d855549
5
5
  SHA512:
6
- metadata.gz: 1c330a76aaf9c02fdfc52aa1c5da7cec9e1d20bd4eb607ae622dd7b9c3898a389b9f6955c8b0d0f52fda7ae02c3e052e100863fcd5535a80d98897b2cd9753e9
7
- data.tar.gz: 9676ad30355100421c95579e8089ef471f0bd267327887ab9582f081ea5731ca0c7034cb68d37139528e5325bb03419b02f3eb8fbf1b14153de34935a4cd72ff
6
+ metadata.gz: 328477374d05ceee8eaa930ffe6cce7cf379c61cf7398a1655c3e2c82d8647a40984862e7f436ad3456742cdbca0f43cd5f02b9dadb52a197e602b9f1d30748f
7
+ data.tar.gz: e82687169737c1f7c6de712fc5bd9076bc894caa961d474972021b1748e5d07e9dcfb5c2d476abbfed69f9ec73a082dd9fd7c93194e92d57b0f1ada388ed2a73
data/CHANGELOG.md CHANGED
@@ -1,117 +1,18 @@
1
- ## Spree 2.1.2 ##
1
+ ## Spree 2.1.3 ##
2
2
 
3
- * States and countries endpoints now do not require authentication, even if it is forced with the `requires_authentication` setting. This is so the frontend's checkout address page can still work.
3
+ * ApiHelpers attributes can now be extended without overriding instance
4
+ methods. By using the same approach in PermittedAttributes. e.g.
4
5
 
5
- *Ryan Bigg*
6
+ Spree::Api::ApiHelpers.order_attributes.push :locked_at
7
+
8
+ Washington Luiz
6
9
 
7
- * You can now assign a location to a shipment when creating it through the orders API. f3ef2e1d46bc972442acbbcaae928e6ef2dc0eb5
10
+ * Admin users can set the order channel when importing orders. By sing the
11
+ channel attribute on Order model
8
12
 
9
- *Washington Luiz*
13
+ Washington Luiz
10
14
 
11
- * Stock Items, Stock Movements and Stock Locations are now invisible to non-admin users.
15
+ * An order's shipments are now destroyed (to be recreated) when an order is assigned a new line item through the API. #3914
12
16
 
13
- *Ryan Bigg*
17
+ Washington Luiz
14
18
 
15
- * Fixed issue where X-Spree-Token header was being ignored. #3798
16
-
17
- *Washington Luiz*
18
-
19
- ## Spree 2.1.0 ##
20
-
21
- * The Products API endpoint now returns an additional key called `shipping_category_id`, and also requires `shipping_category_id` on create.
22
-
23
- *Jeff Dutil*
24
-
25
- * The Products API endpoint now returns an additional key called `display_price`, which is the proper rendering of the price of a product.
26
-
27
- *Ryan Bigg*
28
-
29
- * The Images API's `attachment_url` key has been removed in favour of keys that reflect the current image styles available in the application, such as `mini_url` and `product_url`. Use these now to references images.
30
-
31
- *Ryan Bigg*
32
-
33
- * Fix issue where calling OrdersController#update with line item parameters would *always* create new line items, rather than updating existing ones.
34
-
35
- *Ryan Bigg*
36
-
37
- * The Orders API endpoint now returns an additional key called `display_item_total`, which is the proper rendering of the total line item price of an order.
38
-
39
- *Ryan Bigg*
40
-
41
- * Include a `per_page` key in Products API end response so that libraries like jQuery.simplePagination can use this to display a pagination element on the page.
42
-
43
- *Ryan Bigg*
44
-
45
- * Line item responses now contain `single_display_amount` and `display_amount` for "pretty" versions of the single and total amount for a line item, as well as a `total` node which is an "ugly" version of the total amount of a line item.
46
-
47
- *Ryan Bigg*
48
-
49
- * /api/orders endpoints now accept a `?order_token` parameter which should be the order's token. This can be used to authorize actions on an order without having to pass in an API key.
50
-
51
- *Ryan Bigg*
52
-
53
- * Requests to POST /api/line_items will now update existing line items. For example if you have a line item with a variant ID=2 and quantity=10 and you attempt to create a new line item for the same variant with a quantity of 5, the existing line item's quantity will be updated to 15. Previously, a new line item would erroneously be created.
54
-
55
- *Ryan Bigg*
56
-
57
- * /api/countries now will a 304 response if no country has been changed since the last request.
58
-
59
- *Ryan Bigg*
60
-
61
- * The Shipments API no longer returns inventory units. Instead, it will return manifest objects. This is necessary due to the split shipments changes brought in by Spree 2.
62
-
63
- *Ryan Bigg*
64
-
65
- * Checkouts API's update action will now correctly process line item attributes (either `line_items` or `line_item_attributes`)
66
-
67
- *Ryan Bigg*
68
-
69
- * The structure of shipments data in the API has changed. Shipments can now have many shipping methods, shipping rates (which in turn have many zones and shipping categories), as well as a new key called "manifest" which returns the list of items contained within just this shipment for the order.
70
-
71
- *Ryan Bigg*
72
-
73
- * Address responses now contain a `full_name` attribute.
74
-
75
- *Ryan Bigg*
76
-
77
- * Shipments responses now contain a `selected_shipping_rate` key, so that you don't have to sort through the list of `shipping_rates` to get the selected one.
78
-
79
- *Ryan Bigg*
80
-
81
- * Checkouts API now correctly processes incoming payment data during the payment step.
82
-
83
- *Ryan Bigg*
84
-
85
- * Fix issue where `set_current_order` before filter would be called when CheckoutsController actions were run, causing the order object to be deleted. #3306
86
-
87
- *Ryan Bigg*
88
-
89
- * An order can no longer transition past the "cart" state without first having a line item. #3312
90
-
91
- *Ryan Bigg*
92
-
93
- * Attributes other than "quantity" and "variant_id" will be added to a line item when creating along with an order. #3404
94
-
95
- *Alex Marles & Ryan Bigg*
96
-
97
- * Requests to POST /api/line_items will now update existing line items. For example if you have a line item with a variant ID=2 and quantity=10 and you attempt to create a new line item for the same variant with a quantity of 5, the existing line item's quantity will be updated to 15. Previously, a new line item would erroneously be created.
98
-
99
- * Ryan Bigg
100
-
101
- * Checkouts API's update action will now correctly process line item attributes (either `line_items` or `line_item_attributes`)
102
-
103
- * Ryan Bigg
104
-
105
- * Taxon attributes from `/api/taxons` are now returned within `taxons` subkey. Before:
106
-
107
- ```json
108
- [{ name: 'Ruby' ... }]
109
- ```
110
-
111
- Now:
112
-
113
- ```json
114
- { taxons: [{ name: 'Ruby' }]}
115
- ```
116
-
117
- * Ryan Bigg
@@ -48,7 +48,7 @@ module Spree
48
48
  # users should be able to set price when importing orders via api
49
49
  def permitted_line_item_attributes
50
50
  if current_api_user.has_spree_role?("admin")
51
- super << [:price]
51
+ super << [:price, :variant_id, :sku]
52
52
  else
53
53
  super
54
54
  end
@@ -37,7 +37,7 @@ module Spree
37
37
  authorize! :update, @order, params[:order_token]
38
38
  order_params = object_params
39
39
  line_items = order_params.delete('line_items_attributes')
40
- if @order.update_attributes(object_params)
40
+ if @order.update_attributes(order_params)
41
41
  @order.update_line_items(line_items)
42
42
  if current_api_user.has_spree_role?('admin') && user_id.present?
43
43
  @order.associate_user!(Spree.user_class.find(user_id))
@@ -67,12 +67,7 @@ module Spree
67
67
  object_params[:payments_attributes].first[:amount] = @order.total.to_s
68
68
  end
69
69
  end
70
-
71
- if params[:order]
72
- params.require(:order).permit(permitted_checkout_attributes)
73
- else
74
- {}
75
- end
70
+ object_params
76
71
  end
77
72
 
78
73
  def user_id
@@ -6,6 +6,7 @@ module Spree
6
6
  variant = Spree::Variant.find(params[:line_item][:variant_id])
7
7
  @line_item = order.contents.add(variant, params[:line_item][:quantity])
8
8
  if @line_item.save
9
+ @order.ensure_updated_shipments
9
10
  respond_with(@line_item, status: 201, default_template: :show)
10
11
  else
11
12
  invalid_resource!(@line_item)
@@ -15,6 +16,7 @@ module Spree
15
16
  def update
16
17
  @line_item = order.line_items.find(params[:id])
17
18
  if @line_item.update_attributes(line_item_params)
19
+ @order.ensure_updated_shipments
18
20
  respond_with(@line_item, default_template: :show)
19
21
  else
20
22
  invalid_resource!(@line_item)
@@ -50,11 +50,9 @@ module Spree
50
50
  # hence the use of the update_line_items method, defined within order_decorator.rb.
51
51
  order_params.delete("line_items_attributes")
52
52
  if @order.update_attributes(order_params)
53
- line_item_attributes = params[:order][:line_items].map do |id, attributes|
54
- [id, attributes.slice(*permitted_line_item_attributes)]
55
- end
56
- line_item_attributes = Hash[line_item_attributes].delete_if { |k,v| v.empty? }
57
- @order.update_line_items(line_item_attributes)
53
+
54
+ deal_with_line_items if params[:order][:line_items]
55
+
58
56
  @order.line_items.reload
59
57
  @order.update!
60
58
  respond_with(@order, default_template: :show)
@@ -64,12 +62,22 @@ module Spree
64
62
  end
65
63
 
66
64
  private
65
+ def deal_with_line_items
66
+ line_item_attributes = params[:order][:line_items].map do |id, attributes|
67
+ [id, attributes.slice(*permitted_line_item_attributes)]
68
+ end
69
+ line_item_attributes = Hash[line_item_attributes].delete_if { |k,v| v.empty? }
70
+ @order.update_line_items(line_item_attributes)
71
+ end
67
72
 
68
73
  def order_params
69
74
  if params[:order]
70
- params[:order][:line_items_attributes] = params[:order][:line_items]
75
+ params[:order][:payments_attributes] = params[:order][:payments] if params[:order][:payments]
76
+ params[:order][:shipments_attributes] = params[:order][:shipments] if params[:order][:shipments]
77
+ params[:order][:line_items_attributes] = params[:order][:line_items] if params[:order][:line_items]
71
78
  params[:order][:ship_address_attributes] = params[:order][:ship_address] if params[:order][:ship_address]
72
79
  params[:order][:bill_address_attributes] = params[:order][:bill_address] if params[:order][:bill_address]
80
+
73
81
  params.require(:order).permit(permitted_order_attributes)
74
82
  else
75
83
  {}
@@ -77,7 +85,27 @@ module Spree
77
85
  end
78
86
 
79
87
  def permitted_order_attributes
80
- super << [:import]
88
+ if current_api_user.has_spree_role? "admin"
89
+ super << admin_order_attributes
90
+ else
91
+ super
92
+ end
93
+ end
94
+
95
+ def permitted_shipment_attributes
96
+ if current_api_user.has_spree_role? "admin"
97
+ super << admin_shipment_attributes
98
+ else
99
+ super
100
+ end
101
+ end
102
+
103
+ def admin_shipment_attributes
104
+ [:shipping_method, :stock_location, :inventory_units => [:variant_id, :sku]]
105
+ end
106
+
107
+ def admin_order_attributes
108
+ [:import, :number, :completed_at, :locked_at, :channel]
81
109
  end
82
110
 
83
111
  def next!(options={})
@@ -4,7 +4,7 @@ module Spree
4
4
 
5
5
  def index
6
6
  if params[:ids]
7
- @products = product_scope.where(:id => params[:ids])
7
+ @products = product_scope.where(:id => params[:ids].split(","))
8
8
  else
9
9
  @products = product_scope.ransack(params[:q]).result
10
10
  end
@@ -35,7 +35,7 @@ module Spree
35
35
  @product.permalink = nil
36
36
  retry
37
37
  end
38
- end
38
+ end
39
39
 
40
40
  def update
41
41
  @product = find_product(params[:id])
@@ -1,6 +1,40 @@
1
1
  module Spree
2
2
  module Api
3
3
  module ApiHelpers
4
+ ATTRIBUTES = [
5
+ :product_attributes,
6
+ :product_property_attributes,
7
+ :variant_attributes,
8
+ :image_attributes,
9
+ :option_value_attributes,
10
+ :order_attributes,
11
+ :line_item_attributes,
12
+ :option_type_attributes,
13
+ :payment_attributes,
14
+ :payment_method_attributes,
15
+ :shipment_attributes,
16
+ :taxonomy_attributes,
17
+ :taxon_attributes,
18
+ :inventory_unit_attributes,
19
+ :return_authorization_attributes,
20
+ :address_attributes,
21
+ :country_attributes,
22
+ :state_attributes,
23
+ :adjustment_attributes,
24
+ :taxon_attributes,
25
+ :inventory_unit_attributes,
26
+ :return_authorization_attributes,
27
+ :adjustment_attributes,
28
+ :creditcard_attributes,
29
+ :user_attributes,
30
+ :property_attributes,
31
+ :stock_location_attributes,
32
+ :stock_movement_attributes,
33
+ :stock_item_attributes
34
+ ]
35
+
36
+ mattr_reader *ATTRIBUTES
37
+
4
38
  def required_fields_for(model)
5
39
  required_fields = model._validators.select do |field, validations|
6
40
  validations.any? { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
@@ -11,105 +45,104 @@ module Spree
11
45
  required_fields
12
46
  end
13
47
 
14
- def product_attributes
15
- [:id, :name, :description, :price, :display_price, :available_on, :permalink, :meta_description, :meta_keywords, :shipping_category_id, :taxon_ids]
16
- end
48
+ @@product_attributes = [
49
+ :id, :name, :description, :price, :display_price, :available_on,
50
+ :permalink, :meta_description, :meta_keywords, :shipping_category_id,
51
+ :taxon_ids
52
+ ]
17
53
 
18
- def product_property_attributes
19
- [:id, :product_id, :property_id, :value, :property_name]
20
- end
54
+ @@product_property_attributes = [
55
+ :id, :product_id, :property_id, :value, :property_name
56
+ ]
21
57
 
22
- def variant_attributes
23
- [:id, :name, :sku, :price, :weight, :height, :width, :depth, :is_master, :cost_price, :permalink, :description]
24
- end
58
+ @@variant_attributes = [
59
+ :id, :name, :sku, :price, :weight, :height, :width, :depth, :is_master,
60
+ :cost_price, :permalink, :description
61
+ ]
25
62
 
26
- def image_attributes
27
- [:id, :position, :attachment_content_type, :attachment_file_name, :type, :attachment_updated_at, :attachment_width, :attachment_height, :alt]
28
- end
63
+ @@image_attributes = [
64
+ :id, :position, :attachment_content_type, :attachment_file_name, :type,
65
+ :attachment_updated_at, :attachment_width, :attachment_height, :alt
66
+ ]
29
67
 
30
- def option_value_attributes
31
- [:id, :name, :presentation, :option_type_name, :option_type_id, :option_type_presentation]
32
- end
68
+ @@option_value_attributes = [
69
+ :id, :name, :presentation, :option_type_name, :option_type_id,
70
+ :option_type_presentation
71
+ ]
33
72
 
34
- def order_attributes
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]
36
- end
73
+ @@order_attributes = [
74
+ :id, :number, :item_total, :total, :ship_total, :state, :adjustment_total,
75
+ :user_id, :created_at, :updated_at, :completed_at, :payment_total,
76
+ :shipment_state, :payment_state, :email, :special_instructions, :channel
77
+ ]
37
78
 
38
- def line_item_attributes
39
- [:id, :quantity, :price, :variant_id]
40
- end
79
+ @@line_item_attributes = [:id, :quantity, :price, :variant_id]
41
80
 
42
- def option_type_attributes
43
- [:id, :name, :presentation, :position]
44
- end
81
+ @@option_type_attributes = [:id, :name, :presentation, :position]
45
82
 
46
- def payment_attributes
47
- [:id, :source_type, :source_id, :amount, :display_amount, :payment_method_id, :response_code, :state, :avs_response, :created_at, :updated_at]
48
- end
83
+ @@payment_attributes = [
84
+ :id, :source_type, :source_id, :amount, :display_amount,
85
+ :payment_method_id, :response_code, :state, :avs_response, :created_at,
86
+ :updated_at
87
+ ]
49
88
 
50
- def payment_method_attributes
51
- [:id, :name, :description]
52
- end
89
+ @@payment_method_attributes = [:id, :name, :description]
53
90
 
54
- def shipment_attributes
55
- [:id, :tracking, :number, :cost, :shipped_at, :state]
56
- end
91
+ @@shipment_attributes = [:id, :tracking, :number, :cost, :shipped_at, :state]
57
92
 
58
- def taxonomy_attributes
59
- [:id, :name]
60
- end
93
+ @@taxonomy_attributes = [:id, :name]
61
94
 
62
- def taxon_attributes
63
- [:id, :name, :pretty_name, :permalink, :position, :parent_id, :taxonomy_id]
64
- end
95
+ @@taxon_attributes = [
96
+ :id, :name, :pretty_name, :permalink, :position, :parent_id,
97
+ :taxonomy_id
98
+ ]
65
99
 
66
- def inventory_unit_attributes
67
- [:id, :lock_version, :state, :variant_id, :shipment_id, :return_authorization_id]
68
- end
100
+ @@inventory_unit_attributes = [
101
+ :id, :lock_version, :state, :variant_id, :shipment_id,
102
+ :return_authorization_id
103
+ ]
69
104
 
70
- def return_authorization_attributes
71
- [:id, :number, :state, :amount, :order_id, :reason, :created_at, :updated_at]
72
- end
105
+ @@return_authorization_attributes = [
106
+ :id, :number, :state, :amount, :order_id, :reason, :created_at,
107
+ :updated_at
108
+ ]
73
109
 
74
- def address_attributes
75
- [:id, :firstname, :lastname, :full_name, :address1, :address2, :city, :zipcode, :phone, :company, :alternative_phone, :country_id, :state_id, :state_name]
76
- end
110
+ @@address_attributes = [
111
+ :id, :firstname, :lastname, :full_name, :address1, :address2, :city,
112
+ :zipcode, :phone, :company, :alternative_phone, :country_id, :state_id,
113
+ :state_name
114
+ ]
77
115
 
78
- def country_attributes
79
- [:id, :iso_name, :iso, :iso3, :name, :numcode]
80
- end
116
+ @@country_attributes = [:id, :iso_name, :iso, :iso3, :name, :numcode]
81
117
 
82
- def state_attributes
83
- [:id, :name, :abbr, :country_id]
84
- end
118
+ @@state_attributes = [:id, :name, :abbr, :country_id]
85
119
 
86
- def adjustment_attributes
87
- [:id, :source_type, :source_id, :adjustable_type, :adjustable_id, :originator_type, :originator_id, :amount, :label, :mandatory, :locked, :eligible, :created_at, :updated_at]
88
- end
120
+ @@adjustment_attributes = [
121
+ :id, :source_type, :source_id, :adjustable_type, :adjustable_id,
122
+ :originator_type, :originator_id, :amount, :label, :mandatory,
123
+ :locked, :eligible, :created_at, :updated_at
124
+ ]
89
125
 
90
- def creditcard_attributes
91
- [:id, :month, :year, :cc_type, :last_digits, :first_name, :last_name, :gateway_customer_profile_id, :gateway_payment_profile_id]
92
- end
126
+ @@creditcard_attributes = [
127
+ :id, :month, :year, :cc_type, :last_digits, :first_name, :last_name,
128
+ :gateway_customer_profile_id, :gateway_payment_profile_id
129
+ ]
93
130
 
94
- def user_attributes
95
- [:id, :email, :created_at, :updated_at]
96
- end
131
+ @@user_attributes = [:id, :email, :created_at, :updated_at]
97
132
 
98
- def property_attributes
99
- [:id, :name, :presentation]
100
- end
133
+ @@property_attributes = [:id, :name, :presentation]
101
134
 
102
- def stock_location_attributes
103
- [:id, :name, :address1, :address2, :city, :state_id, :state_name, :country_id, :zipcode, :phone, :active]
104
- end
135
+ @@stock_location_attributes = [
136
+ :id, :name, :address1, :address2, :city, :state_id, :state_name,
137
+ :country_id, :zipcode, :phone, :active
138
+ ]
105
139
 
106
- def stock_movement_attributes
107
- [:id, :quantity, :stock_item_id]
108
- end
140
+ @@stock_movement_attributes = [:id, :quantity, :stock_item_id]
109
141
 
110
- def stock_item_attributes
111
- [:id, :count_on_hand, :backorderable, :lock_version, :stock_location_id, :variant_id]
112
- end
142
+ @@stock_item_attributes = [
143
+ :id, :count_on_hand, :backorderable, :lock_version, :stock_location_id,
144
+ :variant_id
145
+ ]
113
146
  end
114
147
  end
115
148
  end
@@ -60,7 +60,7 @@ Spree::Order.class_eval do
60
60
  shipment.shipping_rates.create!(:shipping_method => shipping_method,
61
61
  :cost => s[:cost])
62
62
  rescue Exception => e
63
- raise "#{e.message} #{s}"
63
+ raise "Order import shipments: #{e.message} #{s}"
64
64
  end
65
65
  end
66
66
  end
@@ -74,7 +74,7 @@ Spree::Order.class_eval do
74
74
  payment.payment_method = Spree::PaymentMethod.find_by_name!(p[:payment_method])
75
75
  payment.save!
76
76
  rescue Exception => e
77
- raise "#{e.message} #{p}"
77
+ raise "Order import payments: #{e.message} #{p}"
78
78
  end
79
79
  end
80
80
  end
@@ -85,11 +85,11 @@ Spree::Order.class_eval do
85
85
  line_item = line_items_hash[k]
86
86
  self.class.ensure_variant_id_from_api(line_item)
87
87
 
88
- extra_params = line_item.except(:variant_id, :quantity)
88
+ extra_params = line_item.except(:variant_id, :quantity, :sku)
89
89
  line_item = self.contents.add(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity])
90
90
  line_item.update_attributes(extra_params) unless extra_params.empty?
91
91
  rescue Exception => e
92
- raise "#{e.message} #{line_item}"
92
+ raise "Order import line items: #{e.message} #{line_item}"
93
93
  end
94
94
  end
95
95
  end
@@ -102,7 +102,7 @@ Spree::Order.class_eval do
102
102
  adjustment.save!
103
103
  adjustment.finalize!
104
104
  rescue Exception => e
105
- raise "#{e.message} #{a}"
105
+ raise "Order import adjustments: #{e.message} #{a}"
106
106
  end
107
107
  end
108
108
  end
@@ -114,7 +114,7 @@ Spree::Order.class_eval do
114
114
  hash.delete(:sku)
115
115
  end
116
116
  rescue Exception => e
117
- raise "#{e.message} #{hash}"
117
+ raise "Ensure order import variant: #{e.message} #{hash}"
118
118
  end
119
119
  end
120
120
 
@@ -137,7 +137,7 @@ Spree::Order.class_eval do
137
137
  address[:country_id] = Spree::Country.where(search).first!.id
138
138
 
139
139
  rescue Exception => e
140
- raise "#{e.message} #{search}"
140
+ raise "Ensure order import address country: #{e.message} #{search}"
141
141
  end
142
142
  end
143
143
 
@@ -153,9 +153,15 @@ Spree::Order.class_eval do
153
153
  end
154
154
 
155
155
  address.delete(:state)
156
- address[:state_id] = Spree::State.where(search).first!.id
156
+ search[:country_id] = address[:country_id]
157
+
158
+ if state = Spree::State.where(search).first
159
+ address[:state_id] = state.id
160
+ else
161
+ address[:state_name] = search[:name] || search[:abbr]
162
+ end
157
163
  rescue Exception => e
158
- raise "#{e.message} #{search}"
164
+ raise "Ensure order import address state: #{e.message} #{search}"
159
165
  end
160
166
  end
161
167
 
@@ -0,0 +1,7 @@
1
+ class AddIndexToUserSpreeApiKey < ActiveRecord::Migration
2
+ def change
3
+ unless defined?(User)
4
+ add_index :spree_users, :spree_api_key
5
+ end
6
+ end
7
+ end
@@ -38,7 +38,14 @@ module Spree
38
38
  end
39
39
 
40
40
  context "PUT 'update'" do
41
- let(:order) { create(:order_with_line_items) }
41
+ let(:order) do
42
+ order = create(:order_with_line_items)
43
+ # Order should be in a pristine state
44
+ # Without doing this, the order may transition from 'cart' straight to 'delivery'
45
+ order.shipments.delete_all
46
+ order
47
+ end
48
+
42
49
 
43
50
  before(:each) do
44
51
  Order.any_instance.stub(:confirmation_required? => true)
@@ -65,6 +65,25 @@ module Spree
65
65
  response.status.should == 204
66
66
  lambda { line_item.reload }.should raise_error(ActiveRecord::RecordNotFound)
67
67
  end
68
+
69
+ context "order contents changed after shipments were created" do
70
+ let!(:order) { Order.create }
71
+ let!(:line_item) { order.contents.add(product.master) }
72
+
73
+ before { order.create_proposed_shipments }
74
+
75
+ it "clear out shipments on create" do
76
+ expect(order.reload.shipments).not_to be_empty
77
+ api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
78
+ expect(order.reload.shipments).to be_empty
79
+ end
80
+
81
+ it "clear out shipments on update" do
82
+ expect(order.reload.shipments).not_to be_empty
83
+ api_put :update, :id => line_item.id, :line_item => { :quantity => 1000 }
84
+ expect(order.reload.shipments).to be_empty
85
+ end
86
+ end
68
87
  end
69
88
 
70
89
  context "as just another user" do
@@ -87,6 +106,5 @@ module Spree
87
106
  lambda { line_item.reload }.should_not raise_error
88
107
  end
89
108
  end
90
-
91
109
  end
92
110
  end
@@ -145,7 +145,15 @@ module Spree
145
145
  }
146
146
  end
147
147
 
148
- before { Zone.stub default_tax: tax_rate.zone }
148
+ before do
149
+ Zone.stub default_tax: tax_rate.zone
150
+ current_api_user.stub has_spree_role?: true
151
+ end
152
+
153
+ it "sets channel" do
154
+ api_post :create, :order => { channel: "amazon" }
155
+ expect(json_response['channel']).to eq "amazon"
156
+ end
149
157
 
150
158
  it "doesnt persist any automatic tax adjustment" do
151
159
  expect {
@@ -154,6 +162,15 @@ module Spree
154
162
 
155
163
  expect(response.status).to eq 201
156
164
  end
165
+
166
+ it "doesnt blow up when passing a sku into line items hash" do
167
+ order_params[:line_items]["0"][:sku] = variant.sku
168
+ order_params[:line_items]["0"][:variant_id] = nil
169
+ order_params[:line_items]["1"][:sku] = other_variant.sku
170
+
171
+ api_post :create, :order => order_params
172
+ expect(response.status).to eq 201
173
+ end
157
174
  end
158
175
 
159
176
  # Regression test for #3404
@@ -207,6 +224,16 @@ module Spree
207
224
  address
208
225
  end
209
226
 
227
+ context "line_items hash not present in request" do
228
+ it "responds successfully" do
229
+ api_put :update, :id => order.to_param, :order => {
230
+ :email => "hublock@spreecommerce.com"
231
+ }
232
+
233
+ expect(response).to be_success
234
+ end
235
+ end
236
+
210
237
  it "updates quantities of existing line items" do
211
238
  api_put :update, :id => order.to_param, :order => {
212
239
  :line_items => {
@@ -368,6 +395,14 @@ module Spree
368
395
  end
369
396
  end
370
397
 
398
+ it "responds with orders updated_at with miliseconds precision" do
399
+ api_get :index
400
+ milisecond = order.updated_at.strftime("%L")
401
+ updated_at = json_response["orders"].first["updated_at"]
402
+
403
+ expect(updated_at.split("T").last).to have_content(milisecond)
404
+ end
405
+
371
406
  context "with two orders" do
372
407
  before { create(:order) }
373
408
 
@@ -32,6 +32,17 @@ module Spree
32
32
  json_response["per_page"].should == Kaminari.config.default_per_page
33
33
  end
34
34
 
35
+ it "retrieves a list of products by ids string" do
36
+ second_product = create(:product)
37
+ api_get :index, :ids => [product.id, second_product.id].join(",")
38
+ json_response["products"].first.should have_attributes(attributes)
39
+ json_response["products"][1].should have_attributes(attributes)
40
+ json_response["total_count"].should == 2
41
+ json_response["current_page"].should == 1
42
+ json_response["pages"].should == 1
43
+ json_response["per_page"].should == Kaminari.config.default_per_page
44
+ end
45
+
35
46
  it "does not return inactive products when queried by ids" do
36
47
  api_get :index, :ids => [inactive_product.id]
37
48
  json_response["count"].should == 0
@@ -7,7 +7,7 @@ module Spree
7
7
  let(:taxonomy) { create(:taxonomy) }
8
8
  let(:taxon) { create(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
9
  let(:taxon2) { create(:taxon, :name => "Rails", :taxonomy => taxonomy) }
10
- let(:attributes) { ["id", "name", "pretty_name", "permalink", "position", "parent_id", "taxonomy_id"] }
10
+ let(:attributes) { ["id", "name", "pretty_name", "permalink", "parent_id", "taxonomy_id"] }
11
11
 
12
12
  before do
13
13
  stub_authentication!
@@ -105,9 +105,10 @@ module Spree
105
105
  response.status.should == 201
106
106
 
107
107
  taxonomy.reload.root.children.count.should eq 2
108
+ taxon = Spree::Taxon.where(:name => 'Colors').first
108
109
 
109
- Spree::Taxon.last.parent_id.should eq taxonomy.root.id
110
- Spree::Taxon.last.taxonomy_id.should eq taxonomy.id
110
+ taxon.parent_id.should eq taxonomy.root.id
111
+ taxon.taxonomy_id.should eq taxonomy.id
111
112
  end
112
113
 
113
114
  it "cannot create a new taxon with invalid attributes" do
@@ -128,7 +128,7 @@ module Spree
128
128
 
129
129
  it 'can build an order from API with state attributes' do
130
130
  ship_address.delete(:state_id)
131
- ship_address[:state] = { 'name' => 'Alabama' }
131
+ ship_address[:state] = { 'name' => state.name }
132
132
  params = { :ship_address_attributes => ship_address,
133
133
  :line_items_attributes => line_items }
134
134
 
@@ -136,15 +136,33 @@ module Spree
136
136
  order.ship_address.state.name.should eq 'Alabama'
137
137
  end
138
138
 
139
- it 'handles state lookup exceptions' do
139
+ context "state passed is not associated with country" do
140
+ let(:params) do
141
+ params = { :ship_address_attributes => ship_address,
142
+ :line_items_attributes => line_items }
143
+ end
144
+
145
+ let(:other_state) { create(:state, name: "Uhuhuh", country: create(:country)) }
146
+
147
+ before do
148
+ ship_address.delete(:state_id)
149
+ ship_address[:state] = { 'name' => other_state.name }
150
+ end
151
+
152
+ it 'sets states name instead of state id' do
153
+ order = Order.build_from_api(user, params)
154
+ expect(order.ship_address.state_name).to eq other_state.name
155
+ end
156
+ end
157
+
158
+ it 'sets state name if state record not found' do
140
159
  ship_address.delete(:state_id)
141
160
  ship_address[:state] = { 'name' => 'XXX' }
142
161
  params = { :ship_address_attributes => ship_address,
143
162
  :line_items_attributes => line_items }
144
163
 
145
- expect {
146
- order = Order.build_from_api(user, params)
147
- }.to raise_error /XXX/
164
+ order = Order.build_from_api(user, params)
165
+ expect(order.ship_address.state_name).to eq 'XXX'
148
166
  end
149
167
 
150
168
  context 'variant not deleted' do
@@ -182,19 +200,12 @@ module Spree
182
200
 
183
201
  it 'ensures_state_id for state fields' do
184
202
  [:name, :abbr].each do |field|
185
- address = { :state => { field => state.send(field) }}
203
+ address = { country_id: country.id, :state => { field => state.send(field) }}
186
204
  Order.ensure_state_id_from_api(address)
187
205
  address[:state_id].should eq state.id
188
206
  end
189
207
  end
190
208
 
191
- it "raises with proper message when cant find state" do
192
- address = { :state => { "name" => "NoNoState" } }
193
- expect {
194
- Order.ensure_state_id_from_api(address)
195
- }.to raise_error /NoNoState/
196
- end
197
-
198
209
  context "shippments" do
199
210
  let(:params) do
200
211
  { :shipments_attributes => [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bigg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-15 00:00:00.000000000 Z
11
+ date: 2013-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree_core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.2
19
+ version: 2.1.3
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: 2.1.2
26
+ version: 2.1.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rabl
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -185,6 +185,7 @@ files:
185
185
  - db/migrate/20100107141738_add_api_key_to_spree_users.rb
186
186
  - db/migrate/20120411123334_resize_api_key_field.rb
187
187
  - db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb
188
+ - db/migrate/20131017162334_add_index_to_user_spree_api_key.rb
188
189
  - lib/spree/api.rb
189
190
  - lib/spree/api/controller_setup.rb
190
191
  - lib/spree/api/engine.rb