Sha256: eb56d06c736edb02f45a58d60b9b738044c47d182c0f7bf3670e83ff0d6e8d58

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require 'spree_i18n'
require 'spree_mobility/engine'
require 'spree_mobility/version'
require 'spree_mobility/fallbacks'
require 'spree_mobility/translation_query'
require 'deface'

module SpreeMobility
  def self.prepend_once(to_klass, klass)
    to_klass.prepend(klass) unless to_klass.ancestors.include?(klass)
  end

  def self.clear_validations_for(klass, *attrs)
    attrs.each do |attr|
      klass.validators_on(attr).each { |val| val.attributes.delete(attr) }
    end
  end

  def self.translates_for(klass, *attrs)
    klass.translates(*attrs)
    klass.accepts_nested_attributes_for :translations
    klass.whitelisted_ransackable_associations ||= []
    klass.whitelisted_ransackable_associations << 'translations'
    clear_validations_for(klass, *attrs)

    # used for preloading only current locale and its fallbacks
    translations_assoc = klass.reflect_on_association(:translations)
    klass.has_many :active_translations,
      -> { where(locale: SpreeMobility.locale_with_fallbacks) },
      class_name:  translations_assoc.class_name,
      inverse_of:  translations_assoc.inverse_of.name,
      foreign_key: translations_assoc.foreign_key
  end

  def self.locale_with_fallbacks
    result = [::Mobility.locale]
    begin
      # At the moment the easiest way to access Mobility fallbacks properly
      # is through a translated model's attribute
      backend = ::Spree::Product.mobility_backend_class(:name)
      result.concat(backend.fallbacks[::Mobility.locale])
      result.uniq!
    rescue KeyError # backend not found
    end
    result
  end

  def self.product_wysiwyg_editor_enabled?
    spree_backend_config :product_wysiwyg_editor_enabled
  end

  def self.taxon_wysiwyg_editor_enabled?
    spree_backend_config :taxon_wysiwyg_editor_enabled
  end

  def self.spree_backend_config(key)
    if defined?(Spree::Backend::Config) && Spree::Backend::Config.has_preference?(key)
      Spree::Backend::Config[key]
    else
      Spree::Config[key]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_mobility-1.4.0 lib/spree_mobility.rb
spree_mobility-1.3.0 lib/spree_mobility.rb
spree_mobility-1.2.0 lib/spree_mobility.rb