Module: Mobility::Sequel::Translation

Included in:
StringTranslation, TextTranslation
Defined in:
lib/mobility/sequel/translation.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mobility/sequel/translation.rb', line 4

def self.included(base)
  base.class_eval do
    plugin :validation_helpers

    # Paraphased from sequel_polymorphic gem
    #
    model = underscore(self.to_s)
    plural_model = pluralize(model)
    many_to_one :translatable,
      reciprocal: plural_model.to_sym,
      reciprocal_type: :many_to_one,
      setter: (proc do |able_instance|
        self[:translatable_id]   = (able_instance.pk if able_instance)
        self[:translatable_type] = (able_instance.class.name if able_instance)
      end),
      dataset: (proc do
        translatable_type = send :translatable_type
        translatable_id   = send :translatable_id
        return if translatable_type.nil? || translatable_id.nil?
        klass = self.class.send(:constantize, translatable_type)
        klass.where(klass.primary_key => translatable_id)
      end),
      eager_loader: (proc do |eo|
        id_map = {}
        eo[:rows].each do |model|
          model_able_type = model.send :translatable_type
          model_able_id = model.send :translatable_id
          model.associations[:translatable] = nil
          ((id_map[model_able_type] ||= {})[model_able_id] ||= []) << model if !model_able_type.nil? && !model_able_id.nil?
        end
        id_map.each do |klass_name, id_map|
          klass = constantize(camelize(klass_name))
          klass.where(klass.primary_key=>id_map.keys).all do |related_obj|
            id_map[related_obj.pk].each do |model|
              model.associations[:translatable] = related_obj
            end
          end
        end
      end)

    def validate
      super
      validates_presence [:locale, :key, :translatable_id, :translatable_type]
      validates_unique   [:locale, :key, :translatable_id, :translatable_type]
    end
  end
end