spree_core 4.2.5 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (263) hide show
  1. checksums.yaml +4 -4
  2. data/app/finders/concerns/spree/product_filterable.rb +9 -0
  3. data/app/finders/spree/cms_pages/find.rb +41 -0
  4. data/app/finders/spree/menus/find.rb +11 -0
  5. data/app/finders/spree/option_values/find_available.rb +20 -0
  6. data/app/finders/spree/orders/find_complete.rb +14 -2
  7. data/app/finders/spree/orders/find_current.rb +1 -13
  8. data/app/finders/spree/product_properties/find_available.rb +20 -0
  9. data/app/finders/spree/products/find.rb +80 -21
  10. data/app/finders/spree/stores/find_current.rb +24 -0
  11. data/app/finders/spree/taxons/find.rb +22 -6
  12. data/app/helpers/spree/base_helper.rb +69 -7
  13. data/app/helpers/spree/locale_helper.rb +5 -1
  14. data/app/helpers/spree/products_helper.rb +7 -3
  15. data/app/models/concerns/spree/display_link.rb +42 -0
  16. data/app/models/concerns/spree/display_money.rb +6 -2
  17. data/app/models/concerns/spree/filter_param.rb +21 -0
  18. data/app/models/concerns/spree/image_methods.rb +24 -0
  19. data/app/models/concerns/spree/memoized_data.rb +24 -0
  20. data/app/models/concerns/spree/multi_store_resource.rb +24 -0
  21. data/app/models/concerns/spree/product_scopes.rb +71 -8
  22. data/app/models/concerns/spree/single_store_resource.rb +19 -0
  23. data/app/models/concerns/spree/user_address.rb +19 -0
  24. data/app/models/concerns/spree/user_methods.rb +14 -12
  25. data/app/models/concerns/spree/user_payment_source.rb +1 -1
  26. data/app/models/concerns/spree/user_reporting.rb +35 -10
  27. data/app/models/concerns/spree/user_roles.rb +25 -0
  28. data/app/models/spree/ability.rb +3 -1
  29. data/app/models/spree/address.rb +27 -6
  30. data/app/models/spree/app_configuration.rb +8 -0
  31. data/app/models/spree/app_dependencies.rb +20 -6
  32. data/app/models/spree/base.rb +18 -0
  33. data/app/models/spree/classification.rb +3 -0
  34. data/app/models/spree/cms/pages/feature_page.rb +7 -0
  35. data/app/models/spree/cms/pages/homepage.rb +20 -0
  36. data/app/models/spree/cms/pages/standard_page.rb +4 -0
  37. data/app/models/spree/cms/sections/featured_article.rb +22 -0
  38. data/app/models/spree/cms/sections/hero_image.rb +38 -0
  39. data/app/models/spree/cms/sections/image_gallery.rb +103 -0
  40. data/app/models/spree/cms/sections/product_carousel.rb +14 -0
  41. data/app/models/spree/cms/sections/rich_text_content.rb +13 -0
  42. data/app/models/spree/cms/sections/side_by_side_images.rb +74 -0
  43. data/app/models/spree/cms_page.rb +66 -0
  44. data/app/models/spree/cms_section.rb +69 -0
  45. data/app/models/spree/country.rb +14 -6
  46. data/app/models/spree/credit_card.rb +2 -8
  47. data/app/models/spree/customer_return.rb +8 -2
  48. data/app/models/spree/gateway/bogus.rb +6 -4
  49. data/app/models/spree/icon.rb +9 -0
  50. data/app/models/spree/image/configuration/active_storage.rb +2 -0
  51. data/app/models/spree/image.rb +5 -24
  52. data/app/models/spree/inventory_unit.rb +1 -1
  53. data/app/models/spree/line_item.rb +1 -1
  54. data/app/models/spree/menu.rb +63 -0
  55. data/app/models/spree/menu_item.rb +76 -0
  56. data/app/models/spree/option_type.rb +1 -1
  57. data/app/models/spree/option_value.rb +11 -0
  58. data/app/models/spree/option_value_variant.rb +1 -1
  59. data/app/models/spree/order/address_book.rb +3 -5
  60. data/app/models/spree/order/currency_updater.rb +1 -1
  61. data/app/models/spree/order/emails.rb +32 -0
  62. data/app/models/spree/order/payments.rb +1 -1
  63. data/app/models/spree/order/store_credit.rb +1 -1
  64. data/app/models/spree/order.rb +46 -45
  65. data/app/models/spree/payment.rb +7 -0
  66. data/app/models/spree/payment_method.rb +16 -0
  67. data/app/models/spree/preferences/preferable.rb +12 -0
  68. data/app/models/spree/preferences/preferable_class_methods.rb +10 -1
  69. data/app/models/spree/product.rb +37 -30
  70. data/app/models/spree/product_property.rb +19 -4
  71. data/app/models/spree/promotion/rules/country.rb +1 -1
  72. data/app/models/spree/promotion/rules/first_order.rb +4 -3
  73. data/app/models/spree/promotion/rules/taxon.rb +10 -7
  74. data/app/models/spree/promotion.rb +6 -15
  75. data/app/models/spree/promotion_handler/cart.rb +7 -2
  76. data/app/models/spree/promotion_handler/coupon.rb +5 -4
  77. data/app/models/spree/promotion_handler/free_shipping.rb +5 -6
  78. data/app/models/spree/promotion_handler/page.rb +3 -2
  79. data/app/models/spree/promotion_handler/promotion_duplicator.rb +1 -0
  80. data/app/models/spree/promotion_rule.rb +2 -0
  81. data/app/models/spree/property.rb +28 -1
  82. data/app/models/spree/reimbursement.rb +3 -1
  83. data/app/models/spree/reimbursement_type/reimbursement_helpers.rb +2 -1
  84. data/app/models/spree/shipment_handler.rb +4 -2
  85. data/app/models/spree/stock/quantifier.rb +6 -6
  86. data/app/models/spree/stock_location.rb +9 -0
  87. data/app/models/spree/store.rb +90 -7
  88. data/app/models/spree/store_credit.rb +7 -3
  89. data/app/models/spree/store_credit_event.rb +2 -2
  90. data/app/models/spree/store_payment_method.rb +11 -0
  91. data/app/models/spree/store_product.rb +11 -0
  92. data/app/models/spree/store_promotion.rb +11 -0
  93. data/app/models/spree/taxon.rb +22 -1
  94. data/app/models/spree/taxon_image/configuration/active_storage.rb +2 -0
  95. data/app/models/spree/taxon_image.rb +1 -0
  96. data/app/models/spree/taxonomy.rb +3 -1
  97. data/app/models/spree/variant.rb +21 -6
  98. data/app/presenters/spree/filters/options_presenter.rb +47 -0
  99. data/app/presenters/spree/filters/price_presenter.rb +22 -0
  100. data/app/presenters/spree/filters/price_range_presenter.rb +29 -0
  101. data/app/presenters/spree/filters/properties_presenter.rb +23 -0
  102. data/app/presenters/spree/filters/property_presenter.rb +42 -0
  103. data/app/presenters/spree/filters/quantified_price_range_presenter.rb +44 -0
  104. data/app/services/spree/account/addresses/create.rb +1 -0
  105. data/app/services/spree/account/addresses/helper.rb +6 -0
  106. data/app/services/spree/account/addresses/update.rb +9 -3
  107. data/app/services/spree/account/create.rb +17 -0
  108. data/app/services/spree/account/update.rb +15 -0
  109. data/app/services/spree/build_localized_redirect_url.rb +1 -1
  110. data/app/services/spree/cart/associate.rb +16 -0
  111. data/app/services/spree/cart/change_currency.rb +27 -0
  112. data/app/services/spree/cart/create.rb +5 -3
  113. data/app/services/spree/cart/destroy.rb +40 -0
  114. data/app/services/spree/cart/empty.rb +36 -0
  115. data/app/services/spree/cart/estimate_shipping_rates.rb +3 -3
  116. data/app/services/spree/checkout/add_store_credit.rb +1 -1
  117. data/app/services/spree/classifications/reposition.rb +18 -0
  118. data/app/services/spree/credit_cards/destroy.rb +41 -0
  119. data/app/sorters/spree/base_sorter.rb +23 -11
  120. data/app/sorters/spree/products/sort.rb +23 -7
  121. data/app/validators/db_maximum_length_validator.rb +5 -0
  122. data/app/validators/email_validator.rb +3 -1
  123. data/config/initializers/active_storage.rb +2 -0
  124. data/config/locales/en.yml +177 -3
  125. data/config/routes.rb +0 -5
  126. data/db/migrate/20201023152810_add_filterable_to_spree_properties.rb +8 -0
  127. data/db/migrate/20210407200948_create_spree_menus.rb +16 -0
  128. data/db/migrate/20210408092939_create_spree_menu_items.rb +31 -0
  129. data/db/migrate/20210504163720_add_filter_param_to_spree_product_properties.rb +8 -0
  130. data/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb +8 -0
  131. data/db/migrate/20210512191732_create_spree_cms_pages.rb +24 -0
  132. data/db/migrate/20210514204251_create_spree_cms_sections.rb +22 -0
  133. data/db/migrate/20210527094055_create_spree_products_stores.rb +36 -0
  134. data/db/migrate/20210608045519_ensure_store_default_country_is_set.rb +5 -0
  135. data/db/migrate/20210702112334_add_missing_timestamp_columns.rb +46 -0
  136. data/db/migrate/20210713131614_add_unique_index_on_property_id_and_product_id_to_product_properties.rb +29 -0
  137. data/db/migrate/20210715091956_add_store_id_to_spree_store_credits.rb +10 -0
  138. data/db/migrate/20210716093151_add_store_id_to_spree_taxonomies.rb +11 -0
  139. data/db/migrate/20210716104141_add_index_on_name_parent_id_and_taxonomy_id_on_spree_taxons.rb +31 -0
  140. data/db/migrate/20210721120857_add_index_on_permalink_parent_id_and_taxonomy_id_on_spree_taxons.rb +31 -0
  141. data/db/migrate/20210721125657_create_spree_promotions_stores.rb +29 -0
  142. data/db/migrate/20210722090705_add_store_id_to_spree_customer_returns.rb +11 -0
  143. data/db/migrate/20210726065456_change_integer_id_columns_into_bigint.rb +305 -0
  144. data/db/migrate/20210730154425_fix_promotion_code_and_path_unique_indexes.rb +9 -0
  145. data/lib/generators/spree/dummy/dummy_generator.rb +11 -6
  146. data/lib/generators/spree/dummy/templates/rails/test.rb +2 -0
  147. data/lib/generators/spree/dummy_model/templates/model.rb.tt +1 -1
  148. data/lib/generators/spree/install/install_generator.rb +12 -24
  149. data/lib/spree/core/components.rb +8 -0
  150. data/lib/spree/core/controller_helpers/auth.rb +7 -1
  151. data/lib/spree/core/controller_helpers/common.rb +1 -1
  152. data/lib/spree/core/controller_helpers/order.rb +4 -4
  153. data/lib/spree/core/controller_helpers/search.rb +1 -0
  154. data/lib/spree/core/controller_helpers/store.rb +24 -1
  155. data/lib/spree/core/engine.rb +6 -0
  156. data/lib/spree/core/importer/product.rb +3 -1
  157. data/lib/spree/core/product_duplicator.rb +1 -0
  158. data/lib/spree/core/search/base.rb +17 -22
  159. data/lib/spree/core/version.rb +1 -1
  160. data/lib/spree/core.rb +8 -4
  161. data/lib/spree/i18n.rb +1 -1
  162. data/lib/spree/money.rb +2 -1
  163. data/lib/spree/permitted_attributes.rb +21 -3
  164. data/lib/spree/testing_support/authorization_helpers.rb +6 -3
  165. data/lib/spree/testing_support/capybara_config.rb +3 -1
  166. data/lib/spree/testing_support/common_rake.rb +17 -4
  167. data/lib/spree/testing_support/extension_rake.rb +2 -2
  168. data/lib/spree/testing_support/factories/address_factory.rb +1 -1
  169. data/lib/spree/testing_support/factories/classification_factory.rb +8 -0
  170. data/lib/spree/testing_support/factories/cms_page_factory.rb +20 -0
  171. data/lib/spree/testing_support/factories/cms_section_factory.rb +31 -0
  172. data/lib/spree/testing_support/factories/customer_return_factory.rb +24 -17
  173. data/lib/spree/testing_support/factories/icon_factory.rb +7 -0
  174. data/lib/spree/testing_support/factories/line_item_factory.rb +6 -2
  175. data/lib/spree/testing_support/factories/menu_factory.rb +16 -0
  176. data/lib/spree/testing_support/factories/menu_item_factory.rb +10 -0
  177. data/lib/spree/testing_support/factories/options_factory.rb +5 -0
  178. data/lib/spree/testing_support/factories/order_factory.rb +11 -2
  179. data/lib/spree/testing_support/factories/payment_method_factory.rb +6 -2
  180. data/lib/spree/testing_support/factories/product_factory.rb +17 -1
  181. data/lib/spree/testing_support/factories/product_property_factory.rb +1 -1
  182. data/lib/spree/testing_support/factories/promotion_factory.rb +18 -9
  183. data/lib/spree/testing_support/factories/property_factory.rb +22 -0
  184. data/lib/spree/testing_support/factories/stock_location_factory.rb +5 -4
  185. data/lib/spree/testing_support/factories/store_credit_factory.rb +1 -0
  186. data/lib/spree/testing_support/factories/store_factory.rb +11 -0
  187. data/lib/spree/testing_support/factories/taxon_factory.rb +3 -1
  188. data/lib/spree/testing_support/factories/taxon_image_factory.rb +7 -0
  189. data/lib/spree/testing_support/factories/taxonomy_factory.rb +1 -0
  190. data/lib/spree/testing_support/factories/user_factory.rb +7 -2
  191. data/lib/spree/testing_support/factories/variant_factory.rb +2 -2
  192. data/lib/spree/testing_support/flatpickr_capybara.rb +58 -35
  193. data/lib/spree/testing_support/locale_helpers.rb +1 -1
  194. data/lib/spree/testing_support/order_walkthrough.rb +9 -9
  195. data/lib/spree/testing_support/rspec_retry_config.rb +5 -0
  196. data/lib/tasks/core.rake +1 -1
  197. data/spec/fixtures/favicon.ico +0 -0
  198. data/spec/fixtures/files/icon_256x256.gif +0 -0
  199. data/spec/fixtures/files/icon_256x256.png +0 -0
  200. data/spec/fixtures/files/icon_512x512.png +0 -0
  201. data/spec/fixtures/files/img_256x128.png +0 -0
  202. data/spree_core.gemspec +12 -11
  203. metadata +211 -159
  204. data/app/assets/images/logo/spree_50.png +0 -0
  205. data/app/assets/images/noimage/large.png +0 -0
  206. data/app/assets/images/noimage/mini.png +0 -0
  207. data/app/assets/images/noimage/product.png +0 -0
  208. data/app/assets/images/noimage/small.png +0 -0
  209. data/app/assets/javascripts/spree.js +0 -78
  210. data/app/controllers/spree/errors_controller.rb +0 -11
  211. data/app/helpers/spree/mail_helper.rb +0 -29
  212. data/app/mailers/spree/base_mailer.rb +0 -46
  213. data/app/mailers/spree/order_mailer.rb +0 -26
  214. data/app/mailers/spree/reimbursement_mailer.rb +0 -12
  215. data/app/mailers/spree/shipment_mailer.rb +0 -12
  216. data/app/mailers/spree/test_mailer.rb +0 -8
  217. data/app/models/spree/validations/db_maximum_length_validator.rb +0 -22
  218. data/app/views/layouts/spree/base_mailer.html.erb +0 -46
  219. data/app/views/spree/errors/forbidden.html.erb +0 -0
  220. data/app/views/spree/errors/unauthorized.html.erb +0 -0
  221. data/app/views/spree/order_mailer/cancel_email.html.erb +0 -24
  222. data/app/views/spree/order_mailer/cancel_email.text.erb +0 -38
  223. data/app/views/spree/order_mailer/confirm_email.html.erb +0 -23
  224. data/app/views/spree/order_mailer/confirm_email.text.erb +0 -39
  225. data/app/views/spree/order_mailer/store_owner_notification_email.html.erb +0 -23
  226. data/app/views/spree/order_mailer/store_owner_notification_email.text.erb +0 -38
  227. data/app/views/spree/reimbursement_mailer/reimbursement_email.html.erb +0 -56
  228. data/app/views/spree/reimbursement_mailer/reimbursement_email.text.erb +0 -24
  229. data/app/views/spree/shared/_base_mailer_footer.html.erb +0 -12
  230. data/app/views/spree/shared/_base_mailer_header.html.erb +0 -13
  231. data/app/views/spree/shared/_base_mailer_stylesheets.html.erb +0 -456
  232. data/app/views/spree/shared/_error_messages.html.erb +0 -11
  233. data/app/views/spree/shared/_mailer_line_item.html.erb +0 -16
  234. data/app/views/spree/shared/_paths.html.erb +0 -8
  235. data/app/views/spree/shared/_purchased_items_table.html.erb +0 -69
  236. data/app/views/spree/shared/purchased_items_table/_adjustment.html.erb +0 -13
  237. data/app/views/spree/shared/purchased_items_table/_line_item.html.erb +0 -27
  238. data/app/views/spree/shared/purchased_items_table/_subtotal.html.erb +0 -13
  239. data/app/views/spree/shared/purchased_items_table/_total.html.erb +0 -13
  240. data/app/views/spree/shipment_mailer/shipped_email.html.erb +0 -36
  241. data/app/views/spree/shipment_mailer/shipped_email.text.erb +0 -17
  242. data/app/views/spree/test_mailer/test_email.html.erb +0 -40
  243. data/app/views/spree/test_mailer/test_email.text.erb +0 -4
  244. data/config/initializers/assets.rb +0 -2
  245. data/config/initializers/premailer_assets.rb +0 -1
  246. data/config/initializers/premailer_rails.rb +0 -3
  247. data/db/migrate/20140805171219_make_existing_credit_cards_default.rb +0 -10
  248. data/lib/generators/spree/dummy/templates/initializers/bullet.rb +0 -5
  249. data/lib/generators/spree/install/templates/vendor/assets/javascripts/spree/backend/all.js +0 -15
  250. data/lib/generators/spree/install/templates/vendor/assets/javascripts/spree/frontend/all.js +0 -16
  251. data/lib/generators/spree/install/templates/vendor/assets/stylesheets/spree/backend/all.css +0 -16
  252. data/lib/generators/spree/install/templates/vendor/assets/stylesheets/spree/frontend/all.css +0 -16
  253. data/lib/generators/spree/mailers_preview/mailers_preview_generator.rb +0 -23
  254. data/lib/generators/spree/mailers_preview/templates/mailers/previews/order_preview.rb +0 -13
  255. data/lib/generators/spree/mailers_preview/templates/mailers/previews/reimbursement_preview.rb +0 -5
  256. data/lib/generators/spree/mailers_preview/templates/mailers/previews/shipment_preview.rb +0 -5
  257. data/lib/generators/spree/mailers_preview/templates/mailers/previews/user_preview.rb +0 -11
  258. data/lib/tasks/email.rake +0 -10
  259. data/vendor/assets/javascripts/cleave.js +0 -1669
  260. data/vendor/assets/javascripts/fetch.umd.js +0 -531
  261. data/vendor/assets/javascripts/jquery.payment.js +0 -652
  262. data/vendor/assets/javascripts/jsuri.js +0 -458
  263. data/vendor/assets/javascripts/polyfill.min.js +0 -1
@@ -0,0 +1,22 @@
1
+ module Spree
2
+ module Filters
3
+ class PricePresenter
4
+ def initialize(amount:, currency:)
5
+ @amount = amount.to_i
6
+ @currency = currency
7
+ end
8
+
9
+ def to_i
10
+ amount
11
+ end
12
+
13
+ def to_s
14
+ Spree::Money.new(amount, currency: currency, no_cents_if_whole: true).to_s
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :amount, :currency
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module Spree
2
+ module Filters
3
+ class PriceRangePresenter
4
+ def self.from_param(param, currency:)
5
+ prices = param.split('-')
6
+
7
+ new(
8
+ min_price: PricePresenter.new(amount: prices.first, currency: currency),
9
+ max_price: PricePresenter.new(amount: prices.last, currency: currency)
10
+ )
11
+ end
12
+
13
+ def initialize(min_price:, max_price:)
14
+ @min_price = min_price
15
+ @max_price = max_price
16
+ end
17
+
18
+ attr_reader :min_price, :max_price
19
+
20
+ def to_param
21
+ "#{min_price.to_i}-#{max_price.to_i}"
22
+ end
23
+
24
+ def to_s
25
+ "#{min_price} - #{max_price}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ module Spree
2
+ module Filters
3
+ class PropertiesPresenter
4
+ def initialize(product_properties_scope:)
5
+ @product_properties = product_properties_scope.includes(:property)
6
+ end
7
+
8
+ def to_a
9
+ grouped_options.map do |property, product_properties|
10
+ PropertyPresenter.new(property: property, product_properties: product_properties)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :product_properties
17
+
18
+ def grouped_options
19
+ product_properties.group_by(&:property)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ module Spree
2
+ module Filters
3
+ class PropertyPresenter
4
+ def initialize(property:, product_properties:)
5
+ @property = property
6
+ @product_properties = product_properties
7
+ end
8
+
9
+ attr_reader :product_properties
10
+
11
+ delegate_missing_to :property
12
+
13
+ def uniq_values
14
+ property.uniq_values(product_properties_scope: product_properties)
15
+ end
16
+
17
+ def to_h
18
+ {
19
+ id: property.id,
20
+ name: property.name,
21
+ presentation: property.presentation,
22
+ values: values_hash
23
+ }
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :property
29
+
30
+ def values_hash
31
+ value_hashes = uniq_values.map do |filter_param, value|
32
+ {
33
+ value: value,
34
+ filter_param: filter_param
35
+ }
36
+ end
37
+
38
+ value_hashes.sort_by { |e| e[:value] }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ module Spree
2
+ module Filters
3
+ class QuantifiedPriceRangePresenter
4
+ ALLOWED_QUANTIFIERS = [
5
+ :less_than,
6
+ :more_than
7
+ ].freeze
8
+
9
+ def initialize(price:, quantifier:)
10
+ if ALLOWED_QUANTIFIERS.exclude?(quantifier.to_sym)
11
+ raise ArgumentError, "quantifier must be one of: #{ALLOWED_QUANTIFIERS.join(', ')}"
12
+ end
13
+
14
+ @price = price
15
+ @quantifier = quantifier.to_sym
16
+ end
17
+
18
+ def to_param
19
+ case quantifier
20
+ when :less_than
21
+ less_than_param
22
+ when :more_than
23
+ more_than_param
24
+ end
25
+ end
26
+
27
+ def to_s
28
+ "#{I18n.t("activerecord.attributes.spree/product.#{quantifier}")} #{price}"
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :price, :quantifier
34
+
35
+ def less_than_param
36
+ "0-#{price.to_i}"
37
+ end
38
+
39
+ def more_than_param
40
+ "#{price.to_i}-Infinity"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -12,6 +12,7 @@ module Spree
12
12
 
13
13
  address = user.addresses.new(address_params)
14
14
  if address.save
15
+ assign_to_user_as_default(user: user, address_id: address.id)
15
16
  success(address)
16
17
  else
17
18
  failure(address)
@@ -31,6 +31,12 @@ module Spree
31
31
  params[:state_id] = country.states.find_by(name: state_name)&.id
32
32
  params
33
33
  end
34
+
35
+ def assign_to_user_as_default(user:, address_id:)
36
+ if user.addresses.pluck(:id) == [address_id] # check if it's user first address
37
+ user.update(bill_address_id: address_id, ship_address_id: address_id)
38
+ end
39
+ end
34
40
  end
35
41
  end
36
42
  end
@@ -11,12 +11,18 @@ module Spree
11
11
  address_params[:country_id] ||= address.country_id
12
12
  fill_country_and_state_ids(address_params)
13
13
 
14
- if address.update(address_params)
15
- success(address)
14
+ if address&.editable?
15
+ address.update(address_params) ? success(address) : failure(address)
16
16
  else
17
- failure(address)
17
+ new_address(address_params).valid? ? address.destroy && success(new_address) : failure(new_address)
18
18
  end
19
19
  end
20
+
21
+ private
22
+
23
+ def new_address(address_params = {})
24
+ @new_address ||= ::Spree::Address.find_or_create_by(address_params.except(:id, :updated_at, :created_at))
25
+ end
20
26
  end
21
27
  end
22
28
  end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Account
3
+ class Create
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(user_params: {})
7
+ user = Spree.user_class.new(user_params)
8
+
9
+ if user.save
10
+ success(user)
11
+ else
12
+ failure(user)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Spree
2
+ module Account
3
+ class Update
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(user:, user_params: {})
7
+ if user.update(user_params)
8
+ success(user)
9
+ else
10
+ failure(user)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -6,7 +6,7 @@ module Spree
6
6
 
7
7
  LOCALE_REGEX = /^\/[A-Za-z]{2}\/|^\/[A-Za-z]{2}-[A-Za-z]{2}\/|^\/[A-Za-z]{2}$|^\/[A-Za-z]{2}-[A-Za-z]{2}$/.freeze
8
8
 
9
- SUPPORTED_PATHS_REGEX = /\/(products|t\/|cart|checkout|addresses|content)/.freeze
9
+ SUPPORTED_PATHS_REGEX = /\/(#{Spree::Config[:storefront_products_path]}|#{Spree::Config[:storefront_taxons_path]}\/|cart|checkout|addresses|content|#{Spree::Config[:storefront_pages_path]})/.freeze
10
10
 
11
11
  # rubocop:disable Lint/UnusedMethodArgument
12
12
  def call(url:, locale:, default_locale: nil)
@@ -0,0 +1,16 @@
1
+ module Spree
2
+ module Cart
3
+ class Associate
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(guest_order:, user:)
7
+ if guest_order.user.nil?
8
+ guest_order.associate_user!(user)
9
+ success(guest_order)
10
+ else
11
+ failure(guest_order, 'Already assigned to a user')
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module Spree
2
+ module Cart
3
+ class ChangeCurrency
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(order:, new_currency:)
7
+ return failure('Currency not supported') unless supported_currency?(order, new_currency)
8
+
9
+ result = order.update!(currency: new_currency) rescue false
10
+
11
+ if result
12
+ success(order)
13
+ else
14
+ failure('Failed to update order')
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def supported_currency?(order, currency)
21
+ store = order.store
22
+ supported_currencies = store.supported_currencies_list
23
+ supported_currencies.map(&:iso_code).include?(currency.upcase)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -6,14 +6,16 @@ module Spree
6
6
  def call(user:, store:, currency:, order_params: nil)
7
7
  order_params ||= {}
8
8
 
9
+ # we cannot create an order without store
10
+ return failure(:store_is_required) if store.nil?
11
+
9
12
  default_params = {
10
13
  user: user,
11
- store: store,
12
- currency: currency,
14
+ currency: currency || store.default_currency,
13
15
  token: Spree::GenerateToken.new.call(Spree::Order)
14
16
  }
15
17
 
16
- order = Spree::Order.create!(default_params.merge(order_params))
18
+ order = store.orders.create!(default_params.merge(order_params))
17
19
  success(order)
18
20
  end
19
21
  end
@@ -0,0 +1,40 @@
1
+ module Spree
2
+ module Cart
3
+ class Destroy
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(order:)
7
+ run :check_if_can_be_destroyed
8
+ run :cancel_shipments
9
+ run :void_payments
10
+ run :destroy_order
11
+ end
12
+
13
+ private
14
+
15
+ def check_if_can_be_destroyed(order:)
16
+ return failure(Spree.t(:cannot_be_destroyed)) unless order&.can_be_destroyed?
17
+
18
+ success(order: order)
19
+ end
20
+
21
+ def cancel_shipments(order:)
22
+ order.shipments.each(&:cancel!)
23
+
24
+ success(order: order)
25
+ end
26
+
27
+ def void_payments(order:)
28
+ order.payments.each(&:void!)
29
+
30
+ success(order: order)
31
+ end
32
+
33
+ def destroy_order(order:)
34
+ order.destroy
35
+
36
+ success(order)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ module Spree
2
+ module Cart
3
+ class Empty
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(order:)
7
+ run :check_if_can_be_empty
8
+ run :empty_order
9
+ end
10
+
11
+ private
12
+
13
+ def check_if_can_be_empty(order:)
14
+ return failure(Spree.t(:cannot_empty)) if order.nil? || order.completed?
15
+
16
+ success(order: order)
17
+ end
18
+
19
+ def empty_order(order:)
20
+ ActiveRecord::Base.transaction do
21
+ order.line_items.destroy_all
22
+ order.updater.update_item_count
23
+ order.adjustments.destroy_all
24
+ order.shipments.destroy_all
25
+ order.state_changes.destroy_all
26
+ order.order_promotions.destroy_all
27
+ order.update_totals
28
+ order.persist_totals
29
+ order.restart_checkout_flow
30
+
31
+ success(order)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -4,7 +4,7 @@ module Spree
4
4
  prepend Spree::ServiceModule::Base
5
5
 
6
6
  def call(order:, country_iso: nil)
7
- country_id = country_id(country_iso)
7
+ country_id = country_id(order.store, country_iso)
8
8
  dummy_order = generate_dummy_order(order, country_id)
9
9
 
10
10
  packages = ::Spree::Stock::Coordinator.new(dummy_order).packages
@@ -20,11 +20,11 @@ module Spree
20
20
 
21
21
  private
22
22
 
23
- def country_id(country_iso)
23
+ def country_id(store, country_iso)
24
24
  if country_iso.present?
25
25
  ::Spree::Country.by_iso(country_iso)&.id
26
26
  else
27
- Spree::Config[:default_country_id]
27
+ store.default_country_id
28
28
  end
29
29
  end
30
30
 
@@ -23,7 +23,7 @@ module Spree
23
23
  payment_method = Spree::PaymentMethod::StoreCredit.available.first
24
24
  raise 'Store credit payment method could not be found' unless payment_method
25
25
 
26
- @order.user.store_credits.order_by_priority.each do |credit|
26
+ @order.user.store_credits.for_store(@order.store).order_by_priority.each do |credit|
27
27
  break if remaining_total.zero?
28
28
  next if credit.amount_remaining.zero?
29
29
 
@@ -0,0 +1,18 @@
1
+ module Spree
2
+ module Classifications
3
+ class Reposition
4
+ prepend Spree::ServiceModule::Base
5
+
6
+ def call(classification:, position:)
7
+ if position.is_a?(String) && !position.match(/^\d+$/)
8
+ return failure(nil, I18n.t('errors.messages.not_a_number'))
9
+ end
10
+
11
+ # Because position we get back is 0-indexed.
12
+ # acts_as_list is 1-indexed.
13
+ classification.insert_at(position.to_i + 1)
14
+ success(classification)
15
+ end
16
+ end
17
+ end
18
+ end