solidus_core 3.0.8 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of solidus_core might be problematic. Click here for more details.

Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/app/helpers/spree/base_helper.rb +1 -1
  3. data/app/helpers/spree/products_helper.rb +1 -1
  4. data/app/models/concerns/spree/active_storage_adapter/attachment.rb +2 -4
  5. data/app/models/concerns/spree/default_price.rb +63 -10
  6. data/app/models/concerns/spree/user_methods.rb +1 -20
  7. data/app/models/spree/adjustment.rb +6 -5
  8. data/app/models/spree/customer_return.rb +3 -2
  9. data/app/models/spree/image/active_storage_attachment.rb +2 -7
  10. data/app/models/spree/image/paperclip_attachment.rb +2 -2
  11. data/app/models/spree/line_item.rb +2 -2
  12. data/app/models/spree/log_entry.rb +1 -74
  13. data/app/models/spree/order.rb +10 -5
  14. data/app/models/spree/order_shipping.rb +9 -6
  15. data/app/models/spree/price.rb +2 -2
  16. data/app/models/spree/product/scopes.rb +5 -5
  17. data/app/models/spree/product.rb +12 -1
  18. data/app/models/spree/promotion/rules/item_total.rb +50 -6
  19. data/app/models/spree/promotion.rb +2 -2
  20. data/app/models/spree/promotion_code.rb +3 -3
  21. data/app/models/spree/refund.rb +0 -8
  22. data/app/models/spree/shipping_rate_tax.rb +1 -1
  23. data/app/models/spree/stock/availability.rb +11 -3
  24. data/app/models/spree/stock/simple_coordinator.rb +0 -10
  25. data/app/models/spree/stock_location.rb +1 -1
  26. data/app/models/spree/store_credit.rb +6 -9
  27. data/app/models/spree/tax_calculator/shipping_rate.rb +1 -1
  28. data/app/models/spree/taxon/active_storage_attachment.rb +2 -2
  29. data/app/models/spree/taxon/paperclip_attachment.rb +3 -3
  30. data/app/models/spree/variant/price_selector.rb +16 -3
  31. data/app/models/spree/variant.rb +26 -16
  32. data/config/locales/en.yml +2 -249
  33. data/db/migrate/20210312061050_change_column_null_on_prices.rb +7 -0
  34. data/lib/generators/solidus/install/install_generator.rb +2 -3
  35. data/lib/generators/solidus/install/templates/config/initializers/spree.rb.tt +3 -1
  36. data/lib/generators/solidus/update/templates/config/initializers/new_solidus_defaults.rb.tt +30 -0
  37. data/lib/generators/solidus/update/update_generator.rb +112 -0
  38. data/lib/generators/spree/dummy/templates/rails/application.rb.tt +0 -1
  39. data/lib/generators/spree/dummy/templates/rails/database.yml +78 -35
  40. data/lib/spree/app_configuration.rb +64 -19
  41. data/lib/spree/core/engine.rb +7 -22
  42. data/lib/spree/core/product_filters.rb +1 -1
  43. data/lib/spree/core/search/base.rb +1 -1
  44. data/lib/spree/core/state_machines/order.rb +1 -1
  45. data/lib/spree/core/validators/email.rb +1 -1
  46. data/lib/spree/core/version.rb +5 -1
  47. data/lib/spree/core/versioned_value.rb +75 -0
  48. data/lib/spree/core.rb +17 -0
  49. data/lib/spree/permitted_attributes.rb +1 -1
  50. data/lib/spree/preferences/configuration.rb +62 -0
  51. data/lib/spree/preferences/preferable.rb +8 -0
  52. data/lib/spree/preferences/preferable_class_methods.rb +5 -3
  53. data/lib/spree/preferences/preference_differentiator.rb +28 -0
  54. data/lib/spree/testing_support/blacklist_urls.rb +1 -1
  55. data/lib/spree/testing_support/dummy_app/database.yml +42 -22
  56. data/lib/spree/testing_support/dummy_app.rb +33 -19
  57. data/lib/spree/testing_support/factories/user_factory.rb +0 -6
  58. data/lib/tasks/solidus/delete_prices_with_nil_amount.rake +8 -0
  59. data/solidus_core.gemspec +0 -1
  60. metadata +9 -26
  61. data/app/models/spree/tax/shipping_rate_taxer.rb +0 -24
  62. data/lib/tasks/solidus/check_orders_with_invalid_email.rake +0 -18
  63. data/lib/tasks/upgrade.rake +0 -15
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spree/preferences/preference_differentiator'
4
+ require 'rails/generators'
5
+
6
+ module Solidus
7
+ # @private
8
+ class UpdateGenerator < ::Rails::Generators::Base
9
+ FROM = Spree.previous_solidus_minor_version
10
+
11
+ desc 'Generates a new initializer to preview the new defaults for current Solidus version'
12
+
13
+ source_root File.expand_path('templates', __dir__)
14
+
15
+ class_option :initializer_basename,
16
+ type: :string,
17
+ default: 'new_solidus_defaults',
18
+ banner: 'The name for the new initializer'
19
+
20
+ class_option :previous_version_prompt,
21
+ type: :boolean,
22
+ default: true,
23
+ banner: 'Prompt to warn about only previous version support'
24
+
25
+ class_option :from,
26
+ type: :string,
27
+ default: FROM,
28
+ banner: 'Solidus version from which you are upgrading'
29
+
30
+ class_option :to,
31
+ type: :string,
32
+ default: Spree.solidus_version,
33
+ hide: true
34
+
35
+ class_option :initializer_directory,
36
+ type: :string,
37
+ default: 'config/initializers/',
38
+ hide: true
39
+
40
+ def create_new_defaults_initializer
41
+ previous_version_prompt = options[:previous_version_prompt]
42
+ return if previous_version_prompt && !yes?(<<~MSG, :red)
43
+ The update process is only supported if you are coming from version #{FROM}. If this is not the case, please, skip it and update your application to use Solidus #{FROM} before retrying.
44
+ If you are confident you want to upgrade from a previous version, you must rerun the generator with the "--from={OLD_VERSION}" argument.
45
+ Are you sure you want to continue? (y/N)
46
+ MSG
47
+
48
+ from = options[:from]
49
+ to = options[:to]
50
+ @from = from
51
+ @core_changes = core_changes_template(from, to)
52
+ @frontend_changes = frontend_changes_template(from, to)
53
+ @backend_changes = backend_changes_template(from, to)
54
+ @api_changes = api_changes_template(from, to)
55
+
56
+ template 'config/initializers/new_solidus_defaults.rb.tt',
57
+ File.join(options[:initializer_directory], "#{options[:initializer_basename]}.rb")
58
+ end
59
+
60
+ def print_message
61
+ say <<~MSG
62
+
63
+ ***********************************************************************
64
+
65
+ Other tasks may be needed to update to the new Solidus version. Please,
66
+ check https://github.com/solidusio/solidus/blob/v#{options[:to]}/CHANGELOG.md
67
+ for details.
68
+
69
+ Thanks for using Solidus!
70
+
71
+ ***********************************************************************
72
+
73
+ MSG
74
+ end
75
+
76
+ private
77
+
78
+ def core_changes_template(from, to)
79
+ changes_template_for(Spree::AppConfiguration, from, to)
80
+ end
81
+
82
+ def frontend_changes_template(from, to)
83
+ return '' unless defined?(Spree::Frontend::Engine)
84
+
85
+ changes_template_for(Spree::FrontendConfiguration, from, to)
86
+ end
87
+
88
+ def backend_changes_template(from, to)
89
+ return '' unless defined?(Spree::Backend::Engine)
90
+
91
+ changes_template_for(Spree::BackendConfiguration, from, to)
92
+ end
93
+
94
+ def api_changes_template(from, to)
95
+ return '' unless defined?(Spree::Api::Engine)
96
+
97
+ changes_template_for(Spree::ApiConfiguration, from, to)
98
+ end
99
+
100
+ def changes_template_for(klass, from, to)
101
+ changes = Spree::Preferences::PreferenceDifferentiator.new(klass).call(from: from, to: to)
102
+ return '# No changes' if changes.empty?
103
+
104
+ [
105
+ ["config.load_defaults('#{from}')"] +
106
+ changes.map do |pref_key, change|
107
+ " # config.#{pref_key} = #{change[:to]}"
108
+ end.flatten
109
+ ].join("\n")
110
+ end
111
+ end
112
+ end
@@ -7,4 +7,3 @@ Bundler.require(*Rails.groups(assets: %w(development test)))
7
7
  require '<%= lib_name %>'
8
8
 
9
9
  <%= application_definition %>
10
-
@@ -1,66 +1,111 @@
1
1
  <% if agent_number = ENV['TC_AGENT_NUMBER']
2
2
  database_prefix = agent_number + '_'
3
3
  end %>
4
+ <% db = case ENV['DB']
5
+ when 'mysql'
6
+ 'mysql'
7
+ when 'postgres', 'postgresql'
8
+ 'postgres'
9
+ when 'sqlite', '', nil
10
+ 'sqlite'
11
+ else
12
+ raise "Invalid DB specified: #{ENV['DB']}"
13
+ end %>
14
+ <% db_host = case db
15
+ when 'mysql'
16
+ ENV['DB_MYSQL_HOST'] || ENV['DB_HOST']
17
+ when 'postgres'
18
+ ENV['DB_POSTGRES_HOST'] || ENV['DB_HOST']
19
+ else
20
+ ENV['DB_HOST']
21
+ end %>
22
+ <% db_username = ENV['DB_USERNAME'] %>
23
+ <% db_password = ENV['DB_PASSWORD'] %>
24
+
25
+
26
+
27
+
4
28
  <% case ENV['DB']
5
- when 'sqlite' %>
6
- development:
7
- adapter: sqlite3
8
- database: db/solidus_development.sqlite3
9
- test:
10
- adapter: sqlite3
11
- database: db/solidus_test.sqlite3
12
- timeout: 10000
13
- production:
14
- adapter: sqlite3
15
- database: db/solidus_production.sqlite3
16
- <% when 'mysql' %>
29
+ when 'mysql' %>
17
30
  development:
18
31
  adapter: mysql2
19
32
  database: <%= database_prefix %><%= options[:lib_name] %>_solidus_development
33
+ <% unless db_username.blank? %>
34
+ username: <%= db_username %>
35
+ <% end %>
36
+ <% unless db_password.blank? %>
37
+ password: <%= db_password %>
38
+ <% end %>
39
+ <% unless db_host.blank? %>
40
+ host: <%= db_host %>
41
+ <% end %>
20
42
  encoding: utf8
21
43
  test:
22
44
  adapter: mysql2
23
- <% if ENV['TRAVIS'] %>
24
- username: root
25
- password:
26
- <% end %>
27
45
  database: <%= database_prefix %><%= options[:lib_name] %>_solidus_test
46
+ <% unless db_username.blank? %>
47
+ username: <%= db_username %>
48
+ <% end %>
49
+ <% unless db_password.blank? %>
50
+ password: <%= db_password %>
51
+ <% end %>
52
+ <% unless db_host.blank? %>
53
+ host: <%= db_host %>
54
+ <% end %>
28
55
  encoding: utf8
29
56
  production:
30
57
  adapter: mysql2
31
58
  database: <%= database_prefix %><%= options[:lib_name] %>_solidus_production
59
+ <% unless db_username.blank? %>
60
+ username: <%= db_username %>
61
+ <% end %>
62
+ <% unless db_password.blank? %>
63
+ password: <%= db_password %>
64
+ <% end %>
65
+ <% unless db_host.blank? %>
66
+ host: <%= db_host %>
67
+ <% end %>
32
68
  encoding: utf8
33
69
  <% when 'postgres', 'postgresql' %>
34
- <% db_host = ENV['DB_HOST'] -%>
35
- <% db_username = ENV['DB_USERNAME'] -%>
36
- <% db_password = ENV['DB_PASSWORD'] -%>
37
70
  development:
38
71
  adapter: postgresql
39
72
  database: <%= database_prefix %><%= options[:lib_name] %>_solidus_development
40
- username: postgres
41
- min_messages: warning
42
- <% unless db_host.blank? %>
73
+ <% unless db_username.blank? %>
74
+ username: <%= db_username %>
75
+ <% end %>
76
+ <% unless db_password.blank? %>
77
+ password: <%= db_password %>
78
+ <% end %>
79
+ <% unless db_host.blank? %>
43
80
  host: <%= db_host %>
44
- <% end %>
81
+ <% end %>
82
+ min_messages: warning
45
83
  test:
46
84
  adapter: postgresql
47
85
  database: <%= database_prefix %><%= options[:lib_name] %>_solidus_test
48
- username: <%= db_username || 'postgres' %>
49
- <% unless db_password.blank? %>
86
+ <% unless db_username.blank? %>
87
+ username: <%= db_username %>
88
+ <% end %>
89
+ <% unless db_password.blank? %>
50
90
  password: <%= db_password %>
51
- <% end %>
52
- min_messages: warning
53
- <% unless db_host.blank? %>
91
+ <% end %>
92
+ <% unless db_host.blank? %>
54
93
  host: <%= db_host %>
55
- <% end %>
94
+ <% end %>
95
+ min_messages: warning
56
96
  production:
57
97
  adapter: postgresql
58
98
  database: <%= database_prefix %><%= options[:lib_name] %>_solidus_production
59
- username: postgres
60
- min_messages: warning
61
- <% unless db_host.blank? %>
99
+ <% unless db_username.blank? %>
100
+ username: <%= db_username %>
101
+ <% end %>
102
+ <% unless db_password.blank? %>
103
+ password: <%= db_password %>
104
+ <% end %>
105
+ <% unless db_host.blank? %>
62
106
  host: <%= db_host %>
63
- <% end %>
107
+ <% end %>
108
+ min_messages: warning
64
109
  <% when 'sqlite', '', nil %>
65
110
  development:
66
111
  adapter: sqlite3
@@ -71,6 +116,4 @@ test:
71
116
  production:
72
117
  adapter: sqlite3
73
118
  database: db/solidus_production.sqlite3
74
- <% else %>
75
- <% raise "Invalid DB specified: #{ENV['DB']}" %>
76
119
  <% end %>
@@ -21,7 +21,6 @@ require "spree/core/search/base"
21
21
  require "spree/core/search/variant"
22
22
  require 'spree/preferences/configuration'
23
23
  require 'spree/core/environment'
24
- require 'rails/gem_version'
25
24
 
26
25
  module Spree
27
26
  class AppConfiguration < Preferences::Configuration
@@ -166,22 +165,6 @@ module Spree
166
165
  # @return [String] URL of logo used on frontend (default: +'logo/solidus.svg'+)
167
166
  preference :logo, :string, default: 'logo/solidus.svg'
168
167
 
169
- # @!attribute [rw] log_entry_permitted_classes
170
- # @return [Array<String>] An array of extra classes that are allowed to be
171
- # loaded from a serialized YAML as details in {Spree::LogEntry}
172
- # (defaults to a non-frozen empty array, so that extensions can add
173
- # their own classes).
174
- # @example
175
- # config.log_entry_permitted_classes = ['Date']
176
- preference :log_entry_permitted_classes, :array, default: []
177
-
178
- # @!attribute [rw] log_entry_allow_aliases
179
- # @return [Boolean] Whether YAML aliases are allowed when loading
180
- # serialized data in {Spree::LogEntry}. It defaults to true. Depending
181
- # on the source of your data, you may consider disabling it to prevent
182
- # entity expansion attacks.
183
- preference :log_entry_allow_aliases, :boolean, default: true
184
-
185
168
  # @!attribute [rw] mails_from
186
169
  # @return [String] Email address used as +From:+ field in transactional emails.
187
170
  preference :mails_from, :string, default: 'solidus@example.com'
@@ -389,6 +372,28 @@ module Spree
389
372
  # Spree::Wallet::DefaultPaymentBuilder.
390
373
  class_name_attribute :default_payment_builder_class, default: 'Spree::Wallet::DefaultPaymentBuilder'
391
374
 
375
+ # Allows providing your own class for managing the contents of an order.
376
+ #
377
+ # @!attribute [rw] order_contents_class
378
+ # @return [Class] a class with the same public interfaces as
379
+ # Spree::OrderContents.
380
+ class_name_attribute :order_contents_class, default: 'Spree::OrderContents'
381
+
382
+ # Allows providing your own class for shipping an order.
383
+ #
384
+ # @!attribute [rw] order_shipping_class
385
+ # @return [Class] a class with the same public interfaces as
386
+ # Spree::OrderShipping.
387
+ class_name_attribute :order_shipping_class, default: 'Spree::OrderShipping'
388
+
389
+ # Allows providing your own class for managing the inventory units of a
390
+ # completed order.
391
+ #
392
+ # @!attribute [rw] order_cancellations_class
393
+ # @return [Class] a class with the same public interfaces as
394
+ # Spree::OrderCancellations.
395
+ class_name_attribute :order_cancellations_class, default: 'Spree::OrderCancellations'
396
+
392
397
  # Allows providing your own class for canceling payments.
393
398
  #
394
399
  # @!attribute [rw] payment_canceller
@@ -460,7 +465,7 @@ module Spree
460
465
  # @!attribute [rw] image_attachment_module
461
466
  # @return [Module] a module that can be included into Spree::Image to allow attachments
462
467
  # Enumerable of images adhering to the present_image_class interface
463
- class_name_attribute :image_attachment_module, default: Rails.gem_version >= Gem::Version.new("6.1.0") ? "Spree::Image::ActiveStorageAttachment" : "Spree::Image::PaperclipAttachment"
468
+ class_name_attribute :image_attachment_module, default: 'Spree::Image::ActiveStorageAttachment'
464
469
 
465
470
  # @!attribute [rw] allowed_image_mime_types
466
471
  #
@@ -470,6 +475,46 @@ module Spree
470
475
  # @return [Array]
471
476
  class_name_attribute :allowed_image_mime_types, default: %w(image/jpeg image/jpg image/png image/gif).freeze
472
477
 
478
+ # @!attribute [rw] product_image_style_default
479
+ #
480
+ # Defines which style to default to when style is not provided
481
+ # :product is the default.
482
+ #
483
+ # @return [Symbol]
484
+ class_name_attribute :product_image_style_default, default: :product
485
+
486
+ # @!attribute [rw] product_image_styles
487
+ #
488
+ # Defines image styles/sizes hash for styles
489
+ # `{ mini: '48x48>',
490
+ # small: '400x400>',
491
+ # product: '680x680>',
492
+ # large: '1200x1200>' } is the default.
493
+ #
494
+ # @return [Hash]
495
+ class_name_attribute :product_image_styles, default: { mini: '48x48>',
496
+ small: '400x400>',
497
+ product: '680x680>',
498
+ large: '1200x1200>' }
499
+ # @!attribute [rw] taxon_image_style_default
500
+ #
501
+ # Defines which style to default to when style is not provided
502
+ # :mini is the default.
503
+ #
504
+ # @return [Symbol]
505
+ class_name_attribute :taxon_image_style_default, default: :mini
506
+
507
+ # @!attribute [rw] taxon_styles
508
+ #
509
+ # Defines taxon styles/sizes hash for styles
510
+ # `{ mini: '48x48>',
511
+ # small: '400x400>',
512
+ # product: '680x680>',
513
+ # large: '1200x1200>' } is the default.
514
+ #
515
+ # @return [Hash]
516
+ class_name_attribute :taxon_image_styles, default: { mini: '32x32>', normal: '128x128>' }
517
+
473
518
  # Allows switching attachment library for Taxon
474
519
  #
475
520
  # `Spree::Taxon::ActiveStorageAttachment`
@@ -478,7 +523,7 @@ module Spree
478
523
  # @!attribute [rw] taxon_attachment_module
479
524
  # @return [Module] a module that can be included into Spree::Taxon to allow attachments
480
525
  # Enumerable of taxons adhering to the present_taxon_class interface
481
- class_name_attribute :taxon_attachment_module, default: Rails.gem_version >= Gem::Version.new("6.1.0") ? "Spree::Taxon::ActiveStorageAttachment" : "Spree::Taxon::PaperclipAttachment"
526
+ class_name_attribute :taxon_attachment_module, default: 'Spree::Taxon::ActiveStorageAttachment'
482
527
 
483
528
  # Allows providing your own class instance for generating order numbers.
484
529
  #
@@ -15,12 +15,6 @@ module Spree
15
15
  generator.test_framework :rspec
16
16
  end
17
17
 
18
- if ActiveRecord.respond_to?(:yaml_column_permitted_classes) || ActiveRecord::Base.respond_to?(:yaml_column_permitted_classes)
19
- config.active_record.yaml_column_permitted_classes ||= []
20
- config.active_record.yaml_column_permitted_classes |=
21
- [Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess]
22
- end
23
-
24
18
  initializer "spree.environment", before: :load_config_initializers do |app|
25
19
  app.config.spree = Spree::Config.environment
26
20
  end
@@ -61,26 +55,17 @@ module Spree
61
55
  end
62
56
  end
63
57
 
64
- config.after_initialize do
65
- # Load in mailer previews for apps to use in development.
66
- # We need to make sure we call `Preview.all` before requiring our
67
- # previews, otherwise any previews the app attempts to add need to be
68
- # manually required.
69
- if Rails.env.development? || Rails.env.test?
70
- ActionMailer::Preview.all
58
+ # Load in mailer previews for apps to use in development.
59
+ initializer "spree.core.action_mailer.set_preview_path", after: "action_mailer.set_configs" do |app|
60
+ original_preview_path = app.config.action_mailer.preview_path
61
+ solidus_preview_path = Spree::Core::Engine.root.join 'lib/spree/mailer_previews'
71
62
 
72
- Dir[root.join("lib/spree/mailer_previews/**/*_preview.rb")].each do |file|
73
- require_dependency file
74
- end
75
- end
63
+ app.config.action_mailer.preview_path = "{#{original_preview_path},#{solidus_preview_path}}"
64
+ ActionMailer::Base.preview_path = app.config.action_mailer.preview_path
76
65
  end
77
66
 
78
67
  config.after_initialize do
79
- if defined?(Spree::Auth::Engine) &&
80
- Gem::Version.new(Spree::Auth::VERSION) < Gem::Version.new('2.5.4') &&
81
- defined?(Spree::UsersController)
82
- Spree::UsersController.protect_from_forgery with: :exception
83
- end
68
+ Spree::Config.check_load_defaults_called('Spree::Config')
84
69
  end
85
70
  end
86
71
  end
@@ -61,7 +61,7 @@ module Spree
61
61
  scope = scope.or(new_scope)
62
62
  end
63
63
 
64
- Spree::Product.joins(master: :default_price).where(scope)
64
+ Spree::Product.joins(master: :prices).where(scope)
65
65
  end
66
66
 
67
67
  def self.format_price(amount)
@@ -51,7 +51,7 @@ module Spree
51
51
  # separate queries most of the time but opt for a join as soon as any
52
52
  # `where` constraints affecting joined tables are added to the search;
53
53
  # which is the case as soon as a taxon is added to the base scope.
54
- scope = scope.preload(master: :currently_valid_prices)
54
+ scope = scope.preload(master: :prices)
55
55
  scope = scope.preload(master: :images) if @properties[:include_images]
56
56
  scope
57
57
  end
@@ -73,7 +73,7 @@ module Spree
73
73
  end
74
74
 
75
75
  event :complete do
76
- transition to: :complete, from: :confirm
76
+ transition to: :complete, from: klass.checkout_steps.keys.last
77
77
  end
78
78
 
79
79
  if states[:payment]
@@ -12,7 +12,7 @@ module Spree
12
12
  # end
13
13
  #
14
14
  class EmailValidator < ActiveModel::EachValidator
15
- EMAIL_REGEXP = URI::MailTo::EMAIL_REGEXP
15
+ EMAIL_REGEXP = /\A([^@\.]|[^@\.]([^@\s]*)[^@\.])@([^@\s]+\.)+[^@\s]+\z/
16
16
 
17
17
  def validate_each(record, attribute, value)
18
18
  unless EMAIL_REGEXP.match? value
@@ -1,12 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spree
4
- VERSION = "3.0.8"
4
+ VERSION = "3.1.0"
5
5
 
6
6
  def self.solidus_version
7
7
  VERSION
8
8
  end
9
9
 
10
+ def self.previous_solidus_minor_version
11
+ '3.0'
12
+ end
13
+
10
14
  def self.solidus_gem_version
11
15
  Gem::Version.new(solidus_version)
12
16
  end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spree
4
+ module Core
5
+ # Wrapper for a value that can be different depending on the Solidus version
6
+ #
7
+ # Some configuration defaults can be added or changed when a new Solidus
8
+ # version is released. This class encapsulates getting the correct value for a
9
+ # given Solidus version.
10
+ #
11
+ # The way it works is you provide an initial value in time, plus the version
12
+ # boundary where it got changed. Then you can fetch the value providing the
13
+ # desired Solidus version:
14
+ #
15
+ # @example
16
+ # value = VersionedValue.new(true, "3.0.0" => false)
17
+ # value.call("2.7.0") # => true
18
+ # value.call("3.0.0") # => false
19
+ # value.call("3.1.0") # => false
20
+ #
21
+ # Remember that you must provide the exact boundary when a value got changed,
22
+ # which could easily be during a pre-release:
23
+ #
24
+ # @example
25
+ # value = VersionedValue.new(true, "3.0.0" => false)
26
+ # value.call("3.0.0.alpha") # => true
27
+ #
28
+ # value = VersionedValue.new(true, "3.0.0.alpha" => false)
29
+ # value.call("3.0.0.alpha") # => false
30
+ #
31
+ # Multiple boundaries can also be provided:
32
+ #
33
+ # @example
34
+ # value = VersionedValue.new(0, "2.0.0" => 1, "3.0.0" => 2)
35
+ # value.call("1.0.0") # => 0
36
+ # value.call("2.1.0") # => 1
37
+ # value.call("3.0.0") # => 2
38
+ class VersionedValue
39
+ attr_reader :boundaries
40
+
41
+ # @param initial_value [Any]
42
+ # @param boundary [Hash<String, Any>] Map from version number to new value
43
+ def initialize(initial_value, boundaries = {})
44
+ @boundaries = Hash[
45
+ { '0' => initial_value }
46
+ .merge(boundaries)
47
+ .transform_keys { |version| to_gem_version(version) }
48
+ .sort
49
+ ]
50
+ end
51
+
52
+ # @param solidus_version [String]
53
+ def call(solidus_version = Spree.solidus_version)
54
+ solidus_version = to_gem_version(solidus_version)
55
+ boundaries.fetch(
56
+ boundaries
57
+ .keys
58
+ .reduce do |target, following|
59
+ if target <= solidus_version && solidus_version < following
60
+ target
61
+ else
62
+ following
63
+ end
64
+ end
65
+ )
66
+ end
67
+
68
+ private
69
+
70
+ def to_gem_version(string)
71
+ Gem::Version.new(string)
72
+ end
73
+ end
74
+ end
75
+ end
data/lib/spree/core.rb CHANGED
@@ -37,6 +37,16 @@ module Spree
37
37
  end
38
38
  end
39
39
 
40
+ # Load the same version defaults for all available Solidus components
41
+ #
42
+ # @see Spree::Preferences::Configuration#load_defaults
43
+ def self.load_defaults(version)
44
+ Spree::Config.load_defaults(version)
45
+ Spree::Frontend::Config.load_defaults(version) if defined?(Spree::Frontend::Config)
46
+ Spree::Backend::Config.load_defaults(version) if defined?(Spree::Backend::Config)
47
+ Spree::Api::Config.load_defaults(version) if defined?(Spree::Api::Config)
48
+ end
49
+
40
50
  # Used to configure Spree.
41
51
  #
42
52
  # Example:
@@ -52,6 +62,13 @@ module Spree
52
62
  end
53
63
 
54
64
  module Core
65
+ def self.has_install_generator_been_run?
66
+ (Rails.env.test? && Rails.application.class.name == 'DummyApp::Application') ||
67
+ Rails.application.paths['config/initializers'].paths.any? do |path|
68
+ File.exist?(path.join('spree.rb'))
69
+ end
70
+ end
71
+
55
72
  class GatewayError < RuntimeError; end
56
73
  end
57
74
  end
@@ -74,7 +74,7 @@ module Spree
74
74
  @@product_properties_attributes = [:property_name, :value, :position]
75
75
 
76
76
  @@product_attributes = [
77
- :name, :description, :available_on, :discontinue_on, :permalink, :meta_description,
77
+ :name, :description, :available_on, :discontinue_on, :meta_description,
78
78
  :meta_keywords, :price, :sku, :deleted_at,
79
79
  :option_values_hash, :weight, :height, :width, :depth,
80
80
  :shipping_category_id, :tax_category_id,