Skip to content

Commit

Permalink
Demonstrate usage of Mobility with ActionText table as key-value backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sedubois committed Jun 1, 2020
1 parent 568174f commit 7ec6bdf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/mobility/active_record/text_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Mobility
module ActiveRecord
class TextTranslation < Translation
self.table_name = "mobility_text_translations"
self.table_name = "action_text_rich_texts"
end
end
end
6 changes: 3 additions & 3 deletions lib/mobility/active_record/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module ActiveRecord
class Translation < ::ActiveRecord::Base
self.abstract_class = true

belongs_to :translatable, polymorphic: true, touch: true
belongs_to :record, polymorphic: true, touch: true

validates :key, presence: true, uniqueness: { scope: [:translatable_id, :translatable_type, :locale], case_sensitive: true }
validates :translatable, presence: true
validates :name, presence: true, uniqueness: { scope: [:record_id, :record_type, :locale], case_sensitive: true }
validates :record, presence: true
validates :locale, presence: true
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/active_record/uniqueness_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def validate_each(record, attribute, value)

if relation.exists?
error_options = options.except(:case_sensitive, :scope, :conditions)
error_options[:value] = value
error_options[:body] = value

record.errors.add(attribute, :taken, error_options)
end
Expand Down
24 changes: 12 additions & 12 deletions lib/mobility/backends/active_record/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def configure(options)
# translation table value column
def build_node(attr, locale)
aliased_table = class_name.arel_table.alias(table_alias(attr, locale))
Arel::Attribute.new(aliased_table, :value, locale, self, attribute_name: attr.to_sym)
Arel::Attribute.new(aliased_table, :body, locale, self, attribute_name: attr.to_sym)
end

# Joins translations using either INNER/OUTER join appropriate to the query.
Expand All @@ -75,10 +75,10 @@ def join_translations(relation, key, locale, join_type)
m = model_class.arel_table
t = class_name.arel_table.alias(table_alias(key, locale))
relation.joins(m.join(t, join_type).
on(t[:key].eq(key).
on(t[:name].eq(key).
and(t[:locale].eq(locale).
and(t[:translatable_type].eq(model_class.base_class.name).
and(t[:translatable_id].eq(m[:id]))))).join_sources)
and(t[:record_type].eq(model_class.base_class.name).
and(t[:record_id].eq(m[:id]))))).join_sources)
end

def already_joined?(relation, name, locale, join_type)
Expand Down Expand Up @@ -165,13 +165,13 @@ def visit_default(_)
association_attributes = (instance_variable_get(:"@#{attrs_method_name}") || []) + attributes
instance_variable_set(:"@#{attrs_method_name}", association_attributes)

has_many association_name, ->{ where key: association_attributes },
as: :translatable,
has_many association_name, ->{ where name: association_attributes },
as: :record,
class_name: translations_class.name,
inverse_of: :translatable,
inverse_of: :record,
autosave: true
before_save do
send(association_name).select { |t| t.value.blank? }.each do |translation|
send(association_name).select { |t| t.body.blank? }.each do |translation|
send(association_name).destroy(translation)
end
end
Expand All @@ -183,7 +183,7 @@ def visit_default(_)
super(source)
self.send("#{association_name}=", source.send(association_name).map(&:dup))
# Set inverse on associations
send(association_name).each { |translation| translation.translatable = self }
send(association_name).each { |translation| translation.record = self }
end
end
include const_set(module_name, callback_methods)
Expand All @@ -196,8 +196,8 @@ def visit_default(_)
# @param [Symbol] locale
# @return [Mobility::ActiveRecord::TextTranslation,Mobility::ActiveRecord::StringTranslation]
def translation_for(locale, _options = {})
translation = translations.find { |t| t.key == attribute && t.locale == locale.to_s }
translation ||= translations.build(locale: locale, key: attribute)
translation = translations.find { |t| t.name == attribute && t.locale == locale.to_s }
translation ||= translations.build(locale: locale, name: attribute)
translation
end

Expand All @@ -206,7 +206,7 @@ def self.included(model_class)
model_class.after_destroy do
[:string, :text].each do |type|
Mobility::ActiveRecord.const_get("#{type.capitalize}Translation").
where(translatable: self).destroy_all
where(record: self).destroy_all
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mobility/backends/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ module KeyValue
# @!group Backend Accessors
# @!macro backend_reader
def read(locale, options = {})
translation_for(locale, options).value
translation_for(locale, options).body
end

# @!macro backend_writer
def write(locale, value, options = {})
translation_for(locale, options).value = value
translation_for(locale, options).body = value
end
# @!endgroup

# @!macro backend_iterator
def each_locale
translations.each { |t| yield(t.locale.to_sym) if t.key == attribute }
translations.each { |t| yield(t.locale.to_sym) if t.name == attribute }
end

private
Expand Down

0 comments on commit 7ec6bdf

Please sign in to comment.