Module: Spree::RailsCompatibility

Defined in:
lib/spree/rails_compatibility.rb

Overview

Supported Rails versions compatibility

This module is meant to wrap some Rails API changes between supported versions. It's also meant to contain compatibility for features that we use internally in the Solidus code base.

Class Method Summary collapse

Class Method Details

.active_storage_set_current(controller) ⇒ Object

Set current host for ActiveStorage in a controller

Changed from `#host` to including a module in Rails 6

See github.com/rails/rails/commit/e33c3cd8ccbecaca6c6af0438956431b02cb3fb2

TODO: Remove when deprecating Rails 5.2


82
83
84
85
86
87
88
89
90
# File 'lib/spree/rails_compatibility.rb', line 82

def self.active_storage_set_current(controller)
  if version_gte?('6')
    controller.include ActiveStorage::SetCurrent
  else
    controller.before_action do
      ActiveStorage::Current.host = request.base_url
    end
  end
end

.active_storage_url_options_host(value) ⇒ Object

Set current host for ActiveStorage

Changed from `#host` to `#url_options` on Rails 7

See github.com/rails/rails/issues/41388

TODO: Remove when deprecating Rails 6.1


99
100
101
102
103
104
105
# File 'lib/spree/rails_compatibility.rb', line 99

def self.active_storage_url_options_host(value)
  if version_gte?('7')
    ActiveStorage::Current.url_options = { host: value }
  else
    ActiveStorage::Current.host = value
  end
end

.default_image_attachment_moduleObject

Set default image attachment adapter

TODO: Remove when deprecating Rails 6.0


56
57
58
59
60
61
62
# File 'lib/spree/rails_compatibility.rb', line 56

def self.default_image_attachment_module
  if version_gte?("6.1")
    "Spree::Image::ActiveStorageAttachment"
  else
    "Spree::Image::PaperclipAttachment"
  end
end

.default_taxon_attachment_moduleObject

Set default taxon attachment adapter

TODO: Remove when deprecating Rails 6.0


67
68
69
70
71
72
73
# File 'lib/spree/rails_compatibility.rb', line 67

def self.default_taxon_attachment_module
  if version_gte?("6.1")
    "Spree::Taxon::ActiveStorageAttachment"
  else
    "Spree::Taxon::PaperclipAttachment"
  end
end

.raise_on_missing_translations(value) ⇒ Object

`raise_on_missing_translations` config option

Changed from ActionView to I18n on Rails 6.1

See github.com/rails/rails/pull/31571

TODO: Remove when deprecating Rails 6.0


45
46
47
48
49
50
51
# File 'lib/spree/rails_compatibility.rb', line 45

def self.raise_on_missing_translations(value)
  if version_gte?('6.1')
    Rails.application.config.i18n.raise_on_missing_translations = value
  else
    Rails.application.config.action_view.raise_on_missing_translations = value
  end
end

.to_fs(value, *args, **kwargs, &block) ⇒ Object

Method `#to_fs`

Available since Rails 7.0, substitutes `#to_s(format)`

It includes:

ActiveSupport::NumericWithFormat ActiveSupport::RangeWithFormat ActiveSupport::TimeWithZone Array Date DateTime Time

See github.com/rails/rails/pull/43772 & github.com/rails/rails/pull/44354

TODO: Remove when deprecating Rails 6.1


30
31
32
33
34
35
36
# File 'lib/spree/rails_compatibility.rb', line 30

def self.to_fs(value, *args, **kwargs, &block)
  if version_gte?('7.0')
    value.to_fs(*args, **kwargs, &block)
  else
    value.to_s(*args, **kwargs, &block)
  end
end

.variant_processorObject

Default ActiveStorage variant processor

Changed from `:mini_magick` to `vips` on Rails 7

See github.com/rails/rails/issues/42744

TODO: Remove when deprecating Rails 6.1


114
115
116
# File 'lib/spree/rails_compatibility.rb', line 114

def self.variant_processor
  version_gte?('7') ? :vips : :mini_magick
end