Class: Mobility::Backends::Sequel::KeyValue::Translatable

Inherits:
Module
  • Object
show all
Defined in:
lib/mobility/backends/sequel/key_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_column, value_column, belongs_to) ⇒ Translatable

Returns a new instance of Translatable.



235
236
237
238
239
240
241
# File 'lib/mobility/backends/sequel/key_value.rb', line 235

def initialize(key_column, value_column, belongs_to)
  @key_column = key_column
  @value_column = value_column
  @belongs_to = belongs_to
  @id_column = :"#{belongs_to}_id"
  @type_column = :"#{belongs_to}_type"
end

Instance Attribute Details

#belongs_toObject (readonly)

Returns the value of attribute belongs_to.



233
234
235
# File 'lib/mobility/backends/sequel/key_value.rb', line 233

def belongs_to
  @belongs_to
end

#id_columnObject (readonly)

Returns the value of attribute id_column.



233
234
235
# File 'lib/mobility/backends/sequel/key_value.rb', line 233

def id_column
  @id_column
end

#key_columnObject (readonly)

Returns the value of attribute key_column.



233
234
235
# File 'lib/mobility/backends/sequel/key_value.rb', line 233

def key_column
  @key_column
end

#type_columnObject (readonly)

Returns the value of attribute type_column.



233
234
235
# File 'lib/mobility/backends/sequel/key_value.rb', line 233

def type_column
  @type_column
end

#value_columnObject (readonly)

Returns the value of attribute value_column.



233
234
235
# File 'lib/mobility/backends/sequel/key_value.rb', line 233

def value_column
  @value_column
end

Instance Method Details

#descendantsObject

Strictly these are not “descendants”, but to keep terminology consistent with ActiveRecord KeyValue backend.



245
246
247
# File 'lib/mobility/backends/sequel/key_value.rb', line 245

def descendants
  @descendants ||= Set.new
end

#included(base) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/mobility/backends/sequel/key_value.rb', line 249

def included(base)
  @descendants ||= Set.new
  @descendants << base

  mod = self
  key_column = mod.key_column
  id_column = mod.id_column
  type_column = mod.type_column

  base.class_eval do
    plugin :validation_helpers

    # Paraphased from sequel_polymorphic gem
    #
    model = underscore(self.to_s)
    plural_model = pluralize(model)
    many_to_one mod.belongs_to,
      reciprocal: plural_model.to_sym,
      reciprocal_type: :many_to_one,
      setter: (proc do |able_instance|
        self[id_column]   = (able_instance.pk if able_instance)
        self[type_column] = (able_instance.class.name if able_instance)
      end),
      dataset: (proc do
        translatable_type = send type_column
        translatable_id   = send id_column
        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 type_column
          model_able_id = model.send id_column
          model.associations[belongs_to] = 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[belongs_to] = related_obj
            end
          end
        end
      end)

    define_method :validate do
      super()
      validates_presence [:locale, key_column, id_column, type_column]
      validates_unique   [:locale, key_column, id_column, type_column]
    end
  end
end