Class: Mobility::Backends::ActiveRecord::Table

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backends::ActiveRecord, Table
Defined in:
lib/mobility/backends/active_record/table.rb

Overview

Implements the Table backend for ActiveRecord models.

To generate a translation table for a model Post, you can use the included mobility:translations generator:

rails generate mobility:translations post title:string content:text

This will create a migration which can be run to create the translation table. If the translation table already exists, it will create a migration adding columns to that table.

Examples:

Model with table backend

class Post < ApplicationRecord
  extend Mobility
  translates :title, backend: :table
end

post = Post.create(title: "foo")
#<Post:0x00... id: 1>

post.title
#=> "foo"

post.translations
#=> [#<Post::Translation:0x00...
#  id: 1,
#  locale: "en",
#  post_id: 1,
#  title: "foo">]

Post::Translation.first
#=> #<Post::Translation:0x00...
#  id: 1,
#  locale: "en",
#  post_id: 1,
#  title: "foo">

Model with multiple translation tables

class Post < ActiveRecord::Base
  extend Mobility
  translates :title,   backend: :table, table_name: :post_title_translations,   association_name: :title_translations
  translates :content, backend: :table, table_name: :post_content_translations, association_name: :content_translations
end

post = Post.create(title: "foo", content: "bar")
#<Post:0x00... id: 1>

post.title
#=> "foo"

post.content
#=> "bar"

post.title_translations
#=> [#<Post::TitleTranslation:0x00...
#  id: 1,
#  locale: "en",
#  post_id: 1,
#  title: "foo">]

post.content_translations
#=> [#<Post::ContentTranslation:0x00...
#  id: 1,
#  locale: "en",
#  post_id: 1,
#  content: "bar">]

Post::TitleTranslation.first
#=> #<Post::TitleTranslation:0x00...
#  id: 1,
#  locale: "en",
#  post_id: 1,
#  title: "foo">

Post::ContentTranslation.first
#=> #<Post::ContentTranslation:0x00...
#  id: 1,
#  locale: "en",
#  post_id: 1,
#  title: "bar">

Defined Under Namespace

Modules: TranslationsHasManyExtension Classes: Translation, Visitor

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Table

#association_name, #each_locale, #foreign_key, #read, #subclass_name, #table_name, #write

Methods included from Mobility::Backends::ActiveRecord

included

Class Method Details

.apply_scope(relation, predicate, locale = Mobility.locale, invert: false) ⇒ ActiveRecord::Relation

Joins translations using either INNER/OUTER join appropriate to the query.

Parameters:

  • relation (ActiveRecord::Relation)

    Relation to scope

  • predicate (Object)

    Arel predicate

  • locale (Symbol) (defaults to: Mobility.locale)

    (Mobility.locale) Locale

  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (ActiveRecord::Relation)

    relation Relation with joins applied (if needed)



131
132
133
134
135
136
137
138
139
# File 'lib/mobility/backends/active_record/table.rb', line 131

def apply_scope(relation, predicate, locale = Mobility.locale, invert: false)
  visitor = Visitor.new(self, locale)
  if join_type = visitor.accept(predicate)
    join_type &&= Visitor::INNER_JOIN if invert
    join_translations(relation, locale, join_type)
  else
    relation
  end
end

.build_node(attr, locale) ⇒ Mobility::Plugins::Arel::Attribute

Returns Arel node for column on translation table.

Parameters:

  • attr (String)

    Attribute name

  • _locale (Symbol)

    Locale

Returns:



119
120
121
122
# File 'lib/mobility/backends/active_record/table.rb', line 119

def build_node(attr, locale)
  aliased_table = model_class.const_get(subclass_name).arel_table.alias(table_alias(locale))
  Plugins::Arel::Attribute.new(aliased_table, attr, locale, self)
end

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • association_name (Symbol) — default: :translations

    Name of association method

  • table_name (Symbol)

    Name of translation table

  • foreign_key (Symbol)

    Name of foreign key

  • subclass_name (Symbol) — default: :Translation

    Name of subclass to append to model class to generate translation class



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mobility/backends/active_record/table.rb', line 102

def configure(options)
  table_name = model_class.table_name
  options[:table_name]  ||= "#{table_name.singularize}_translations"
  options[:foreign_key] ||= table_name.downcase.singularize.camelize.foreign_key
  if (association_name = options[:association_name]).present?
    options[:subclass_name] ||= association_name.to_s.singularize.camelize.freeze
  else
    options[:association_name] = :translations
    options[:subclass_name] ||= :Translation
  end
  %i[foreign_key association_name subclass_name table_name].each { |key| options[key] = options[key].to_sym }
end

Instance Method Details

#translation_for(locale) ⇒ Object

Returns translation for a given locale, or builds one if none is present.

Parameters:

  • locale (Symbol)


291
292
293
294
295
# File 'lib/mobility/backends/active_record/table.rb', line 291

def translation_for(locale, **)
  translation = translations.in_locale(locale)
  translation ||= translations.build(locale: locale)
  translation
end