Class: Mobility::Backends::ActionText

Inherits:
ActiveRecord::KeyValue
  • Object
show all
Defined in:
lib/mobility/backends/action_text.rb

Overview

Implements the KeyValue backend for ActionText.

Examples:

class Post < ApplicationRecord
  extend Mobility
  translates :content, backend: :action_text
end

post = Post.create(content: "<h1>My text is rich</h1>")
post.rich_text_translations
#=> #<ActionText::RichText::ActiveRecord_Associations_CollectionProxy ... >
post.rich_text_translations.first.to_s
#=> "<div class=\"trix-content\">\n  <h1>My text is rich</h1>\n</div>\n"
post.content
#=> "<div class=\"trix-content\">\n  <h1>My text is rich</h1>\n</div>\n"
post.rich_text_translations.first.class
#=> Mobility::Backends::ActionText::RichTextTranslation

Defined Under Namespace

Modules: ActionTextValidations, WithAllRichText Classes: PlainTextTranslation, RichTextTranslation

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mobility/backends/action_text.rb', line 47

def configure(options)
  options[:plain] = false unless options.has_key?(:plain)
  if options[:plain]
    options[:association_name] ||= 'plain_text_translations'
    options[:class_name]       ||= PlainTextTranslation
  else
    options[:association_name] ||= 'rich_text_translations'
    options[:class_name]       ||= RichTextTranslation
  end
  options[:key_column]       ||= :name
  options[:value_column]     ||= :body
  options[:belongs_to]       ||= :record
  super
end

.define_after_destroy_callback(klass) ⇒ Object

override destroy logic because we are not using the db tables from the subclassed KeyValue implementation



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mobility/backends/action_text.rb', line 65

def define_after_destroy_callback(klass)
  # Ensure we only call after destroy hook once per translations class
  b = self
  translation_classes = [class_name, RichTextTranslation, PlainTextTranslation].uniq
  klass.after_destroy do
    @mobility_after_destroy_translation_classes = [] unless defined?(@mobility_after_destroy_translation_classes)
    (translation_classes - @mobility_after_destroy_translation_classes).each do |translation_class|
      translation_class.where(b.belongs_to => self).destroy_all
    end
    @mobility_after_destroy_translation_classes += translation_classes
  end
end

.valid_keysObject



41
42
43
# File 'lib/mobility/backends/action_text.rb', line 41

def valid_keys
  super.tap { |keys| keys.delete(:type) } << :plain
end

Instance Method Details

#read(locale, **options) ⇒ Object

override to return record instead of value



29
30
31
32
33
34
35
36
37
38
# File 'lib/mobility/backends/action_text.rb', line 29

def read(locale, **options)
  return super if self.options[:plain]

  if model.association_cached?("rich_text_#{attribute}")
    eager_loaded = model.public_send("rich_text_#{attribute}")
    return eager_loaded if eager_loaded.locale == locale.to_s
  end

  translation_for(locale, **options)
end