Class: Mobility::Backends::Sequel::KeyValue::Translatable
- Inherits:
-
Module
- Object
- Module
- Mobility::Backends::Sequel::KeyValue::Translatable
- Defined in:
- lib/mobility/backends/sequel/key_value.rb
Instance Attribute Summary collapse
-
#belongs_to ⇒ Object
readonly
Returns the value of attribute belongs_to.
-
#id_column ⇒ Object
readonly
Returns the value of attribute id_column.
-
#key_column ⇒ Object
readonly
Returns the value of attribute key_column.
-
#type_column ⇒ Object
readonly
Returns the value of attribute type_column.
-
#value_column ⇒ Object
readonly
Returns the value of attribute value_column.
Instance Method Summary collapse
-
#descendants ⇒ Object
Strictly these are not “descendants”, but to keep terminology consistent with ActiveRecord KeyValue backend.
- #included(base) ⇒ Object
-
#initialize(key_column, value_column, belongs_to) ⇒ Translatable
constructor
A new instance of Translatable.
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_to ⇒ Object (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_column ⇒ Object (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_column ⇒ Object (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_column ⇒ Object (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_column ⇒ Object (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
#descendants ⇒ Object
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 || id_map[.pk].each do |model| model.associations[belongs_to] = 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 |