module Spree

This file exists solely to test whether or not there are missing translations within the code that Spree's test suite covers.

If there is a translation referenced which has no corresponding key within the .yml file, then there will be a message output at the end of the suite showing that.

If there is a translation within the locale file which *isn't* used in the test, this will also be shown at the end of the suite run.

Constants

Config
Dependencies
VERSION

Attributes

missing_translation_messages[RW]
unused_translation_messages[RW]
unused_translations[RW]
used_translations[RW]

Public Class Methods

admin_path() click to toggle source
# File lib/spree/core.rb, line 33
def self.admin_path
  Spree::Config[:admin_path]
end
admin_path=(path) click to toggle source

Used to configure admin_path for Spree

Example:

write the following line in `config/initializers/spree.rb`

Spree.admin_path = '/custom-path'
# File lib/spree/core.rb, line 44
def self.admin_path=(path)
  Spree::Config[:admin_path] = path
end
available_locales() click to toggle source
# File lib/spree/i18n.rb, line 19
def available_locales
  locales_from_i18n = I18n.available_locales
  locales =
    if defined?(SpreeI18n)
      (SpreeI18n::Locale.all << :en).map(&:to_s)
    else
      [Rails.application.config.i18n.default_locale, I18n.locale, :en]
    end

  (locales + locales_from_i18n).uniq.compact
end
check_missing_translations() click to toggle source
# File lib/spree/testing_support/i18n.rb, line 28
def self.check_missing_translations
  self.missing_translation_messages = []
  self.used_translations ||= []
  used_translations.map { |a| a.split('.') }.each do |translation_keys|
    root = translations
    processed_keys = []
    translation_keys.each do |key|
      root = root.fetch(key.to_sym)
      processed_keys << key.to_sym
    rescue KeyError
      error = "#{(processed_keys << key).join('.')} (#{I18n.locale})"
      unless Spree.missing_translation_messages.include?(error)
        Spree.missing_translation_messages << error
      end
    end
  end
end
check_unused_translations() click to toggle source
# File lib/spree/testing_support/i18n.rb, line 46
def self.check_unused_translations
  self.used_translations ||= []
  self.unused_translation_messages = []
  self.unused_translations = []
  load_translations(translations)
  translation_diff = unused_translations - used_translations
  translation_diff.each do |translation|
    Spree.unused_translation_messages << "#{translation} (#{I18n.locale})"
  end
end
config() { |Config| ... } click to toggle source

Used to configure Spree.

Example:

Spree.config do |config|
  config.track_inventory_levels = false
end

This method is defined within the core gem on purpose. Some people may only wish to use the Core part of Spree.

# File lib/spree/core.rb, line 58
def self.config
  yield(Spree::Config)
end
dependencies() { |Dependencies| ... } click to toggle source

Used to set dependencies for Spree.

Example:

Spree.dependencies do |dependency|
  dependency.cart_add_item_service = MyCustomAddToCart
end

This method is defined within the core gem on purpose. Some people may only wish to use the Core part of Spree.

# File lib/spree/core.rb, line 72
def self.dependencies
  yield(Spree::Dependencies)
end
normal_t(key, options = {})
Alias for: t
t(key, options = {})
Also aliased as: normal_t
Alias for: translate
translate(key, options = {}) click to toggle source

Add spree namespace and delegate to Rails TranslationHelper for some nice extra functionality. e.g return reasonable strings for missing translations

# File lib/spree/i18n.rb, line 13
def translate(key, options = {})
  options[:scope] = [*options[:scope]].unshift(:spree).uniq

  TranslationHelperWrapper.new.translate(key, **options)
end
Also aliased as: t
user_class(constantize: true) click to toggle source
# File lib/spree/core.rb, line 25
def self.user_class(constantize: true)
  if @@user_class.is_a?(Class)
    raise 'Spree.user_class MUST be a String or Symbol object, not a Class object.'
  elsif @@user_class.is_a?(String) || @@user_class.is_a?(Symbol)
    constantize ? @@user_class.to_s.constantize : @@user_class.to_s
  end
end
version() click to toggle source
# File lib/spree/core/version.rb, line 4
def self.version
  VERSION
end

Private Class Methods

load_translations(hash, root = []) click to toggle source
# File lib/spree/testing_support/i18n.rb, line 59
def self.load_translations(hash, root = [])
  hash.each do |k, v|
    if v.is_a?(Hash)
      load_translations(v, root.dup << k)
    else
      key = (root + [k]).join('.')
      unused_translations << key
    end
  end
end
translations() click to toggle source
# File lib/spree/testing_support/i18n.rb, line 70
def self.translations
  @translations ||= I18n.backend.send(:translations)[I18n.locale][:spree]
end