Permalink
Browse files

Merge branch '1-3-stable' of git://github.com/spree/spree into add_fi…

…lter_by_taxons_to_admin

* '1-3-stable' of git://github.com/spree/spree: (106 commits)
  expand order number field size to 32
  allow importing of order number through api
  refactor complete_from_api for order
  [promo] Remove a whole bunch o' stuff from 'should allow an admin to create an automatic promotion with free shipping (no code)' that simply didn't need to be there
  Evaluate coupon codes ignoring case
  allow completed_at to be passed in new orders api
  use .present? for imported order fields, to avoid empty strings
  improved exception reporting while importing orders through api
  Add option_values to variant template [api]
  enhanced order import though api. support for shipping address, shipments, adjustments and payments
  Update rails version in README [ci skip]
  Bump to Rails 3.2.14
  Fix sample order generator to make the new orders show up as complete
  Fix issue where adjustments page would not have 'continue' link in certain circumstances
  Use Ruby 1.8 hash in core/spec/requests/products_spec.rb
  Dsplay right price when product has variants
  Add ability to convert Spree::CreditCard objects into ActiveMerchant::Billing::CreditCard objects
  Generate sensibly lengthed SKUs
  Show useful error messages for promotion rules and actions
  Fix coupon code activation
  ...
  • Loading branch information...
2 parents 9cfa35d + d697313 commit 882c31e447cbf6f0a257cc7a66a033f8ac34c90a @sbounmy committed Aug 17, 2013
Showing with 2,475 additions and 2,320 deletions.
  1. +3 −3 README.md
  2. +5 −5 Rakefile
  3. +1 −1 SPREE_VERSION
  4. +9 −1 api/app/controllers/spree/api/checkouts_controller.rb
  5. +1 −1 api/app/controllers/spree/api/line_items_controller.rb
  6. +8 −4 api/app/controllers/spree/api/products_controller.rb
  7. +2 −2 api/app/controllers/spree/api/taxons_controller.rb
  8. +15 −0 api/app/controllers/spree/base_controller_decorator.rb
  9. +144 −6 api/app/models/spree/order_decorator.rb
  10. +6 −0 api/app/overrides/api_key_spree_layout.rb
  11. +4 −0 api/app/views/spree/api/_key.html.erb
  12. +4 −0 api/app/views/spree/api/variants/variant.v1.rabl
  13. +1 −0 api/config/locales/en.yml
  14. +1 −1 api/config/routes.rb
  15. +17 −10 api/spec/controllers/spree/api/checkouts_controller_spec.rb
  16. +18 −18 api/spec/controllers/spree/api/orders_controller_spec.rb
  17. +4 −4 api/spec/controllers/spree/api/taxons_controller_spec.rb
  18. +194 −4 api/spec/models/spree/order_spec.rb
  19. +1 −1 cmd/lib/spree_cmd/extension.rb
  20. +1 −0 cmd/lib/spree_cmd/templates/extension/rspec
  21. +1 −1 core/app/assets/javascripts/admin/address_states.js
  22. +24 −17 core/app/assets/javascripts/admin/admin.js.erb
  23. +20 −18 core/app/assets/javascripts/admin/variant_autocomplete.js.erb
  24. +37 −0 core/app/assets/javascripts/spree.js.coffee
  25. +7 −2 core/app/assets/javascripts/store/product.js.coffee
  26. +1 −0 core/app/assets/javascripts/store/spree_core.js
  27. +16 −12 core/app/assets/stylesheets/admin/components/_states.scss
  28. +8 −0 core/app/assets/stylesheets/admin/shared/_tables.scss
  29. +16 −16 core/app/assets/stylesheets/store/screen.css.scss
  30. +4 −2 core/app/assets/stylesheets/store/variables.css.scss
  31. +7 −3 core/app/controllers/spree/admin/adjustments_controller.rb
  32. +2 −1 core/app/controllers/spree/admin/line_items_controller.rb
  33. +3 −2 core/app/controllers/spree/admin/orders/customer_details_controller.rb
  34. +1 −1 core/app/controllers/spree/admin/taxons_controller.rb
  35. +1 −1 core/app/controllers/spree/checkout_controller.rb
  36. +5 −0 core/app/controllers/spree/orders_controller.rb
  37. +16 −0 core/app/mailers/spree/base_mailer.rb
  38. +7 −16 core/app/mailers/spree/order_mailer.rb
  39. +2 −10 core/app/mailers/spree/shipment_mailer.rb
  40. +2 −8 core/app/mailers/spree/test_mailer.rb
  41. +1 −2 core/app/models/spree/app_configuration.rb
  42. +14 −5 core/app/models/spree/credit_card.rb
  43. +3 −1 core/app/models/spree/gateway.rb
  44. +10 −5 core/app/models/spree/order.rb
  45. +39 −0 core/app/models/spree/order/checkout.rb
  46. +4 −0 core/app/models/spree/order_updater.rb
  47. +2 −1 core/app/models/spree/payment.rb
  48. +6 −1 core/app/models/spree/payment/processing.rb
  49. +8 −3 core/app/models/spree/product/scopes.rb
  50. +1 −1 core/app/views/spree/admin/adjustments/_adjustments_table.html.erb
  51. +2 −2 core/app/views/spree/admin/adjustments/index.html.erb
  52. +1 −1 core/app/views/spree/admin/orders/_form.html.erb
  53. +2 −0 core/app/views/spree/admin/orders/index.html.erb
  54. +3 −1 core/app/views/spree/admin/payments/_form.html.erb
  55. +2 −0 core/app/views/spree/admin/products/index.html.erb
  56. +3 −1 core/app/views/spree/admin/shared/_configuration_menu.html.erb
  57. +1 −1 core/app/views/spree/admin/shared/_order_details.html.erb
  58. +0 −1 core/app/views/spree/admin/shared/_tabs.html.erb
  59. +1 −1 core/app/views/spree/admin/shipments/edit.html.erb
  60. +1 −1 core/app/views/spree/admin/shipments/index.html.erb
  61. +2 −1 core/app/views/spree/admin/tax_rates/_form.html.erb
  62. +4 −4 core/app/views/spree/admin/zones/_form.html.erb
  63. +3 −1 core/app/views/spree/admin/zones/index.html.erb
  64. +0 −1 core/app/views/spree/layouts/spree_application.html.erb
  65. +3 −3 core/app/views/spree/products/_thumbnails.html.erb
  66. +1 −1 core/app/views/spree/shared/_google_analytics.html.erb
  67. +1 −1 core/config/initializers/user_class_extensions.rb
  68. +4 −1 core/config/locales/en.yml
  69. +3 −3 core/config/routes.rb
  70. +6 −0 core/db/migrate/20130626232741_add_cvv_result_code_and_cvv_result_message_to_spree_payments.rb
  71. +5 −0 core/db/migrate/20130628021056_add_unique_index_to_permalink_on_spree_products.rb
  72. +6 −0 core/db/migrate/20130628022817_add_unique_index_to_orders_shipments_and_stock_transfers.rb
  73. +9 −0 core/db/migrate/20130813140619_expand_order_number_size.rb
  74. +1 −1 core/lib/spree/core/calculated_adjustments.rb
  75. +3 −3 core/lib/spree/core/controller_helpers/auth.rb
  76. +1 −1 core/lib/spree/core/controller_helpers/common.rb
  77. +3 −5 core/lib/spree/core/mail_interceptor.rb
  78. +52 −25 core/lib/spree/core/mail_settings.rb
  79. +17 −13 core/lib/spree/core/permalinks.rb
  80. +1 −1 core/lib/spree/core/s3_support.rb
  81. +7 −2 core/lib/spree/core/testing_support/factories/product_factory.rb
  82. +5 −3 core/lib/spree/core/testing_support/factories/variant_factory.rb
  83. +1 −1 core/lib/spree/core/version.rb
  84. +1 −1 core/lib/spree/product_filters.rb
  85. +21 −0 core/spec/controllers/spree/admin/line_items_controller_spec.rb
  86. +1 −1 core/spec/controllers/spree/admin/resource_controller_spec.rb
  87. +12 −0 core/spec/controllers/spree/admin/taxons_controller_spec.rb
  88. +2 −2 core/spec/controllers/spree/checkout_controller_spec.rb
  89. +14 −2 core/spec/controllers/spree/orders_controller_spec.rb
  90. +31 −63 core/spec/lib/mail_interceptor_spec.rb
  91. +59 −55 core/spec/lib/mail_settings_spec.rb
  92. +5 −5 core/spec/mailers/order_mailer_spec.rb
  93. +5 −5 core/spec/mailers/shipment_mailer_spec.rb
  94. +18 −18 core/spec/models/credit_card_spec.rb
  95. +68 −1 core/spec/models/order/checkout_spec.rb
  96. +25 −0 core/spec/models/order_spec.rb
  97. +48 −13 core/spec/models/payment_spec.rb
  98. +5 −0 core/spec/models/product_filter_spec.rb
  99. +6 −9 core/spec/models/product_spec.rb
  100. +14 −0 core/spec/models/spree/user_spec.rb
  101. +1 −1 core/spec/requests/admin/homepage_spec.rb
  102. +6 −0 core/spec/requests/admin/orders/adjustments_spec.rb
  103. +6 −0 core/spec/requests/admin/orders/customer_details_spec.rb
  104. +1 −1 core/spec/requests/admin/orders/shipments_spec.rb
  105. +36 −0 core/spec/requests/admin/products/option_types_spec.rb
  106. +16 −0 core/spec/requests/products_spec.rb
  107. +5 −5 core/spree_core.gemspec
  108. +6 −22 core/vendor/assets/javascripts/jquery.validate/additional-methods.min.js
  109. +6 −45 core/vendor/assets/javascripts/jquery.validate/jquery.validate.min.js
  110. +23 −22 core/vendor/assets/javascripts/jquery.validate/localization/messages_ar.js
  111. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_bg.js
  112. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_ca.js
  113. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_cs.js
  114. +19 −17 core/vendor/assets/javascripts/jquery.validate/localization/messages_da.js
  115. +19 −18 core/vendor/assets/javascripts/jquery.validate/localization/messages_de.js
  116. +22 −21 core/vendor/assets/javascripts/jquery.validate/localization/messages_el.js
  117. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_es.js
  118. +23 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_et.js
  119. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_eu.js
  120. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_fa.js
  121. +20 −18 core/vendor/assets/javascripts/jquery.validate/localization/messages_fi.js
  122. +44 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_fr.js
  123. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_he.js
  124. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_hr.js
  125. +21 −18 core/vendor/assets/javascripts/jquery.validate/localization/messages_hu.js
  126. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_it.js
  127. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_ja.js
  128. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_ka.js
  129. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_kk.js
  130. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_ko.js
  131. +23 −21 core/vendor/assets/javascripts/jquery.validate/localization/messages_lt.js
  132. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_lv.js
  133. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_my.js
  134. +32 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_nl.js
  135. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_no.js
  136. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_pl.js
  137. +26 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_pt_BR.js
  138. +26 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_pt_PT.js
  139. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_ro.js
  140. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_ru.js
  141. +21 −19 core/vendor/assets/javascripts/jquery.validate/localization/messages_si.js
  142. +19 −18 core/vendor/assets/javascripts/jquery.validate/localization/messages_sk.js
  143. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_sl.js
  144. +23 −21 core/vendor/assets/javascripts/jquery.validate/localization/messages_sr.js
  145. +23 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_sv.js
  146. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_th.js
  147. +22 −21 core/vendor/assets/javascripts/jquery.validate/localization/messages_tr.js
  148. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_uk.js
  149. +22 −20 core/vendor/assets/javascripts/jquery.validate/localization/messages_vi.js
  150. +25 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_zh.js
  151. +26 −0 core/vendor/assets/javascripts/jquery.validate/localization/messages_zh_TW.js
  152. +6 −6 core/vendor/assets/javascripts/jquery.validate/localization/methods_de.js
  153. +3 −3 core/vendor/assets/javascripts/jquery.validate/localization/methods_nl.js
  154. +3 −3 core/vendor/assets/javascripts/jquery.validate/localization/methods_pt.js
  155. +3 −3 core/vendor/assets/javascripts/modernizr.js
  156. +0 −1 dash/.rspec
  157. +0 −5 dash/Gemfile
  158. +0 −13 dash/Guardfile
  159. +0 −26 dash/LICENSE
  160. +0 −15 dash/README.md
  161. +0 −28 dash/Rakefile
  162. BIN dash/app/assets/images/analytics_dashboard_preview.jpg
  163. +0 −83 dash/app/controllers/spree/admin/analytics_controller.rb
  164. +0 −31 dash/app/controllers/spree/admin/overview_controller.rb
  165. +0 −13 dash/app/helpers/spree/admin/overview_helper.rb
  166. +0 −236 dash/app/helpers/spree/analytics_helper.rb
  167. +0 −13 dash/app/models/spree/dash_configuration.rb
  168. +0 −5 dash/app/overrides/analytics_header.rb
  169. +0 −5 dash/app/overrides/configuration_menu_link.rb
  170. +0 −42 dash/app/views/spree/admin/analytics/edit.html.erb
  171. +0 −61 dash/app/views/spree/admin/analytics/sign_up.html.erb
  172. +0 −51 dash/app/views/spree/admin/overview/index.html.erb
  173. +0 −12 dash/app/views/spree/analytics/_header.html.erb
  174. +0 −24 dash/config/locales/en.yml
  175. +0 −9 dash/config/routes.rb
  176. +0 −15 dash/lib/spree/dash.rb
  177. +0 −20 dash/lib/spree/dash/engine.rb
  178. +0 −83 dash/lib/spree/dash/jirafe.rb
  179. +0 −1 dash/lib/spree_dash.rb
  180. +0 −40 dash/spec/controllers/admin/analytics_controller_spec.rb
  181. +0 −18 dash/spec/controllers/admin/overview_controller_spec.rb
  182. +0 −107 dash/spec/helpers/spree/analytics_helper_spec.rb
  183. +0 −45 dash/spec/requests/admin/analytics_spec.rb
  184. +0 −43 dash/spec/requests/header_tags_spec.rb
  185. +0 −39 dash/spec/spec_helper.rb
  186. +0 −23 dash/spree_dash.gemspec
  187. +0 −1 lib/spree.rb
  188. +25 −23 promo/app/assets/javascripts/admin/product_picker.js
  189. +25 −23 promo/app/assets/javascripts/admin/user_picker.js
  190. +6 −0 promo/app/controllers/spree/orders_controller_decorator.rb
  191. +10 −10 promo/app/controllers/spree/store_controller_decorator.rb
  192. +7 −5 promo/app/models/spree/calculator/percent_per_item.rb
  193. +22 −5 promo/app/models/spree/order_decorator.rb
  194. +7 −4 promo/app/models/spree/promotion.rb
  195. +2 −0 promo/app/models/spree/promotion/actions/create_line_items.rb
  196. +1 −8 promo/app/models/spree/promotion/rules/user_logged_in.rb
  197. +2 −1 promo/app/views/spree/admin/promotions/_promotion_action.html.erb
  198. +1 −0 promo/app/views/spree/admin/promotions/_promotion_rule.html.erb
  199. +2 −2 promo/config/locales/en.yml
  200. +1 −1 promo/config/routes.rb
  201. +6 −0 promo/spec/models/calculator/percent_per_item_spec.rb
  202. +13 −1 promo/spec/models/promotion/actions/create_line_items_spec.rb
  203. +21 −0 promo/spec/models/promotion/promotion/rules/user_logged_in_spec.rb
  204. +21 −0 promo/spec/models/promotion/rules/user_logged_in_spec.rb
  205. +21 −0 promo/spec/models/promotion/rules/user_logged_in_spec.rb~HEAD
  206. +2 −20 promo/spec/requests/promotion_adjustments_spec.rb
  207. +2 −0 sample/db/samples/orders.rb
  208. +0 −1 spree.gemspec
View
@@ -21,17 +21,17 @@ All of the gems are designed to work together to provide a fully functional e-co
however, to use only the pieces you are interested in. So for example, you could use just the barebones spree\_core gem
and perhaps combine it with your own custom promotion scheme instead of using spree_promo.
-[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/spree/spree)
+[![Code Climate](https://codeclimate.com/github/spree/spree.png)](https://codeclimate.com/github/spree/spree)
Installation
------------
The fastest way to get started is by using the spree command line tool
available in the spree gem which will add Spree to an existing Rails application.
- $ gem install rails -v 3.2.13
+ $ gem install rails -v 3.2.14
$ gem install spree
- $ rails _3.2.13_ new my_store
+ $ rails _3.2.14_ new my_store
$ spree install my_store
This will add the Spree gem to your Gemfile, create initializers, copy migrations and
View
@@ -16,7 +16,7 @@ end
desc "Generates a dummy app for testing for every Spree engine"
task :test_app do
- %w(api core dash promo).each do |engine|
+ %w(api core promo).each do |engine|
ENV['LIB_NAME'] = File.join('spree', engine)
ENV['DUMMY_PATH'] = File.expand_path("../#{engine}/spec/dummy", __FILE__)
Rake::Task['common:test_app'].execute
@@ -30,7 +30,7 @@ task :clean do
puts "Deleting pkg directory.."
FileUtils.rm_rf("pkg")
- %w(api cmd core dash promo).each do |gem_name|
+ %w(api cmd core promo).each do |gem_name|
puts "Cleaning #{gem_name}:"
puts " Deleting #{gem_name}/Gemfile"
FileUtils.rm_f("#{gem_name}/Gemfile")
@@ -46,7 +46,7 @@ end
namespace :gem do
desc "run rake gem for all gems"
task :build do
- %w(core api dash promo sample cmd).each do |gem_name|
+ %w(core api promo sample cmd).each do |gem_name|
puts "########################### #{gem_name} #########################"
puts "Deleting #{gem_name}/pkg"
FileUtils.rm_rf("#{gem_name}/pkg")
@@ -63,7 +63,7 @@ namespace :gem do
task :install do
version = File.read(File.expand_path("../SPREE_VERSION", __FILE__)).strip
- %w(core api dash promo sample cmd).each do |gem_name|
+ %w(core api promo sample cmd).each do |gem_name|
puts "########################### #{gem_name} #########################"
puts "Deleting #{gem_name}/pkg"
FileUtils.rm_rf("#{gem_name}/pkg")
@@ -82,7 +82,7 @@ namespace :gem do
task :release do
version = File.read(File.expand_path("../SPREE_VERSION", __FILE__)).strip
- %w(core api dash promo sample cmd).each do |gem_name|
+ %w(core api promo sample cmd).each do |gem_name|
puts "########################### #{gem_name} #########################"
cmd = "cd #{gem_name}/pkg && gem push spree_#{gem_name}-#{version}.gem"; puts cmd; system cmd
end
View
@@ -1 +1 @@
-1.3.3.beta
+1.3.3
@@ -4,8 +4,14 @@ class CheckoutsController < Spree::Api::BaseController
before_filter :load_order, :only => :update
before_filter :associate_user, :only => :update
+ # Spree::Core::ControllerHelpers::Auth overrides
+ # Spree::Api::BaseController's unauthorized method...
+ # Which is not a good thing.
+ # Here's a small hack to shuffle around the method.
+ alias_method :real_unauthorized, :unauthorized
include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Order
+ alias_method :unauthorized, :real_unauthorized
respond_to :json
@@ -15,6 +21,7 @@ def create
end
def update
+ authorize! :update, @order, params[:order_token]
respond_with(@order, :default_template => 'spree/api/orders/show') and return if @order.state == "complete"
if object_params && object_params[:user_id].present?
@@ -34,7 +41,8 @@ def update
def object_params
# For payment step, filter order parameters to produce the expected nested attributes for a single payment and its source, discarding attributes for payment methods other than the one selected
- if @order.payment?
+ # respond_to check is necessary due to issue described in #2910
+ if @order.has_checkout_step?("payment") && @order.payment?
if params[:payment_source].present? && source_params = params.delete(:payment_source)[params[:order][:payments_attributes].first[:payment_method_id].underscore]
params[:order][:payments_attributes].first[:source_attributes] = source_params
end
@@ -16,7 +16,7 @@ def create
def update
authorize! :read, order
@line_item = order.line_items.find(params[:id])
- if @line_item.update_attributes(params[:line_item])
+ if @line_item.update_attributes(params[:line_item], :as => :api)
respond_with(@line_item, :default_template => :show)
else
invalid_resource!(@line_item)
@@ -20,10 +20,14 @@ def create
authorize! :create, Product
params[:product][:available_on] ||= Time.now
@product = Product.new(params[:product])
- if @product.save
- respond_with(@product, :status => 201, :default_template => :show)
- else
- invalid_resource!(@product)
+ begin
+ if @product.save
+ respond_with(@product, :status => 201, :default_template => :show)
+ else
+ invalid_resource!(@product)
+ end
+ rescue ActiveRecord::RecordNotUnique
+ retry
end
end
@@ -20,8 +20,8 @@ def create
taxonomy = Taxonomy.find_by_id(params[:taxonomy_id])
if taxonomy.nil?
- @taxon.errors[:taxonomy_id] = "Invalid taxonomy_id."
- invalid_resource!(@taxon) and return
+ @taxon.errors[:taxonomy_id] = I18n.t(:invalid_taxonomy_id, :scope => 'spree.api')
+ invalid_resource!(@taxon) and return
end
@taxon.parent_id = taxonomy.root.id unless params[:taxon][:parent_id]
@@ -0,0 +1,15 @@
+require_dependency 'spree/base_controller'
+
+Spree::BaseController.class_eval do
+ before_filter :ensure_api_key
+
+ # Need to generate an API key for a user due to some actions potentially
+ # requiring authentication to the Spree API
+ def ensure_api_key
+ if user = try_spree_current_user
+ if user.respond_to?(:spree_api_key) && user.spree_api_key.blank?
+ user.generate_spree_api_key!
+ end
+ end
+ end
+end
@@ -1,13 +1,151 @@
Spree::Order.class_eval do
def self.build_from_api(user, params)
- order = create
- params[:line_items_attributes] ||= []
- unless params[:line_items_attributes].empty?
- params[:line_items_attributes].each_key do |k|
- order.add_variant(Spree::Variant.find(params[:line_items_attributes][k][:variant_id]), params[:line_items_attributes][k][:quantity])
+ completed_at = params.delete(:completed_at)
+ line_items = params.delete(:line_items_attributes) || {}
+ shipments = params.delete(:shipments_attributes) || []
+ payments = params.delete(:payments_attributes) || []
+ adjustments = params.delete(:adjustments_attributes) || []
+
+ ensure_country_id_from_api params[:ship_address_attributes]
+ ensure_state_id_from_api params[:ship_address_attributes]
+
+ ensure_country_id_from_api params[:bill_address_attributes]
+ ensure_state_id_from_api params[:bill_address_attributes]
+
+ order = create!(params)
+
+ order.create_shipments_from_api(shipments)
+ order.create_line_items_from_api(line_items)
+ order.create_adjustments_from_api(adjustments)
+ order.create_payments_from_api(payments)
+ order.complete_from_api(completed_at)
+
+ order.save!
+ order
+ end
+
+ def complete_from_api(completed_at)
+ if completed_at
+ self.completed_at = completed_at
+ self.state = 'complete'
+ end
+ end
+
+ def create_shipments_from_api(shipments_hash)
+ shipments_hash.each do |s|
+ begin
+ shipment = shipments.build
+ shipment.tracking = s[:tracking]
+
+ inventory_units = s[:inventory_units] || []
+ inventory_units.each do |iu|
+ self.class.ensure_variant_id_from_api(iu)
+
+ unit = shipment.inventory_units.build
+ unit.order = self
+ unit.variant_id = iu[:variant_id]
+ end
+
+ shipment.shipping_method = Spree::ShippingMethod.find_by_name!(s[:shipping_method])
+ shipment.save!
+
+ shipment.adjustment.locked = true
+ shipment.adjustment.amount = s[:cost].to_f
+ shipment.adjustment.save
+ rescue Exception => e
+ raise "#{e.message} #{s}"
end
end
+ end
- order
+ def create_payments_from_api(payments_hash)
+ payments_hash.each do |p|
+ begin
+ payment = payments.build
+ payment.amount = p[:amount].to_f
+ payment.state = p.fetch(:state, 'completed')
+ payment.payment_method = Spree::PaymentMethod.find_by_name!(p[:payment_method])
+ payment.save!
+ rescue Exception => e
+ raise "#{e.message} #{p}"
+ end
+ end
+ end
+
+ def create_line_items_from_api(line_items_hash)
+ line_items_hash.each_key do |k|
+ begin
+ line_item = line_items_hash[k]
+ self.class.ensure_variant_id_from_api(line_item)
+ self.add_variant(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity])
+ rescue Exception => e
+ raise "#{e.message} #{line_item}"
+ end
+ end
+ end
+
+ def create_adjustments_from_api(adjustments)
+ adjustments.each do |a|
+ begin
+ adjustment = self.adjustments.build(:amount => a['amount'].to_f,
+ :label => a['label'])
+ adjustment.locked = true
+ adjustment.save!
+ rescue Exception => e
+ raise "#{e.message} #{a}"
+ end
+ end
+ end
+
+ def self.ensure_variant_id_from_api(hash)
+ begin
+ unless hash[:variant_id].present?
+ hash[:variant_id] = Spree::Variant.find_by_sku!(hash[:sku]).id
+ hash.delete(:sku)
+ end
+ rescue Exception => e
+ raise "#{e.message} #{hash}"
+ end
+ end
+
+ def self.ensure_country_id_from_api(address)
+ return if address.nil? or address[:country_id].present? or address[:country].nil?
+
+ begin
+ search = {}
+ if name = address[:country]['name']
+ search[:name] = name
+ elsif iso_name = address[:country]['iso_name']
+ search[:iso_name] = iso_name.upcase
+ elsif iso = address[:country]['iso']
+ search[:iso] = iso.upcase
+ elsif iso3 = address[:country]['iso3']
+ search[:iso3] = iso3.upcase
+ end
+
+ address.delete(:country)
+ address[:country_id] = Spree::Country.where(search).first!.id
+
+ rescue Exception => e
+ raise "#{e.message} #{search}"
+ end
+ end
+
+ def self.ensure_state_id_from_api(address)
+ return if address.nil? or address[:state_id].present? or address[:state].nil?
+
+ begin
+ search = {}
+ if name = address[:state]['name']
+ search[:name] = name
+ elsif abbr = address[:state]['abbr']
+ search[:abbr] = abbr.upcase
+ end
+
+ address.delete(:state)
+ address[:state_id] = Spree::State.where(search).first!.id
+ rescue Exception => e
+ raise "#{e.message} #{search}"
+ end
end
end
@@ -0,0 +1,6 @@
+Deface::Override.new(:virtual_path => "spree/layouts/spree_application",
+ :name => "api_key_spree_layout",
+ :insert_bottom => "body",
+ :partial => "spree/api/key",
+ :disabled => false)
+
@@ -0,0 +1,4 @@
+<script>
+ Spree.api_key = <%= raw(try_spree_current_user.try(:spree_api_key).to_s.inspect) %>;
+</script>
+
@@ -1 +1,5 @@
attributes *variant_attributes
+node(:options_text) { |v| v.options_text }
+child :option_values => :option_values do
+ attributes *option_value_attributes
+end
@@ -21,3 +21,4 @@ en:
invalid_shipping_method: "Invalid shipping method specified."
shipment:
cannot_ready: "Cannot ready shipment."
+ invalid_taxonomy_id: "Invalid taxonomy id."
View
@@ -1,4 +1,4 @@
-Spree::Core::Engine.routes.prepend do
+Spree::Core::Engine.routes.draw do
namespace :admin do
resources :users do
member do
Oops, something went wrong.

0 comments on commit 882c31e

Please sign in to comment.