Module: SpreeMobility

Defined in:
lib/spree_mobility.rb,
lib/spree_mobility/engine.rb,
lib/spree_mobility/version.rb,
lib/spree_mobility/fallbacks.rb,
lib/spree_mobility/configuration.rb,
lib/spree_mobility/translation_query.rb,
app/helpers/spree_mobility/locale_helper.rb,
app/models/concerns/spree_mobility/translatable.rb,
lib/generators/spree_mobility/install/install_generator.rb,
app/controllers/concerns/spree_mobility/controller_mobility_helper.rb

Defined Under Namespace

Modules: ControllerMobilityHelper, Fallbacks, Generators, LocaleHelper, Translatable, VERSION Classes: Configuration, Engine, TranslationQuery

Class Method Summary collapse

Class Method Details

.clear_validations_for(klass, *attrs) ⇒ Object



13
14
15
16
17
# File 'lib/spree_mobility.rb', line 13

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

.locale_with_fallbacksObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spree_mobility.rb', line 35

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

.prepend_once(to_klass, klass) ⇒ Object



9
10
11
# File 'lib/spree_mobility.rb', line 9

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

.product_wysiwyg_editor_enabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/spree_mobility.rb', line 48

def self.product_wysiwyg_editor_enabled?
  spree_backend_config :product_wysiwyg_editor_enabled
end

.spree_backend_config(key) ⇒ Object



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

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

.taxon_wysiwyg_editor_enabled?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/spree_mobility.rb', line 52

def self.taxon_wysiwyg_editor_enabled?
  spree_backend_config :taxon_wysiwyg_editor_enabled
end

.translates_for(klass, *attrs) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spree_mobility.rb', line 19

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

.versionObject

Returns the version of the currently loaded SpreeMobility as a Gem::Version.



6
7
8
# File 'lib/spree_mobility/version.rb', line 6

def version
  Gem::Version.new VERSION::STRING
end