Sha256: 23dfb26c461da97da5c1462fdcb320817f9684421fdb9e6339485328af8b2c0a

Contents?: true

Size: 1.99 KB

Versions: 5

Compression:

Stored size: 1.99 KB

Contents

module SpreeMobility
  module Translatable
    extend ActiveSupport::Concern

    include Spree::RansackableAttributes

    included do |klass|
      klass.send :extend, Mobility
      klass.send(:default_scope) { i18n }
    end

    class_methods do
      def translation_class
        const_get('Translation')
      end
      
      def ransack(params = {}, options = {})
        params ||= {}
        names = params.keys

        # TODO: this should be used together with search param, only fallback if first is empty
        # params[:translations_locale_in] ||= fallback_locales.map(&:to_s)

        names.each do |n|
          mobility_attributes.each do |t|
            if n.to_s.starts_with? t.to_s
              params[:"translations_#{n}"] = params[n]
              params.delete n
            end
          end
        end

        super(params, options)
      end
      alias :search :ransack unless respond_to? :search

      module CopyPreloadedActiveTranslationsToTranslations
        # Although it has :nodoc, preload_associations is a public method of
        # ActiveRecord::Relation and should be relatively safe to use.
        # After the active_translations assoc is preloaded, we copy results over
        # into translations - basically we load translations with results from
        # active_translations.
        def preload_associations(records)
          super.tap do
            records.each do |record|
              # We don't want to overwrite translations if already present
              next if record.association(:translations).target.present?
              record.association(:translations).target =
                record.association(:active_translations).target
            end
          end
        end
      end

      def spree_base_scopes
        # Only preload translations for current locale and its fallbacks
        scope = super.preload(:active_translations)
        scope.singleton_class.prepend CopyPreloadedActiveTranslationsToTranslations
        return scope
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_mobility-1.4.0 app/models/concerns/spree_mobility/translatable.rb
spree_mobility-1.3.0 app/models/concerns/spree_mobility/translatable.rb
spree_mobility-1.2.0 app/models/concerns/spree_mobility/translatable.rb
spree_mobility-1.1.0 app/models/concerns/spree_mobility/translatable.rb
spree_mobility-1.0.0 app/models/concerns/spree_mobility/translatable.rb