Sha256: 541e5ebe8adef9be8f332b92f274976a997ed62447ae099ff426aac362c2e12f

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_mobility-1.1.0 lib/spree_mobility.rb
spree_mobility-1.0.0 lib/spree_mobility.rb