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 < ActiveRecord::Base
  translates :title, backend: :table, association_name: :translations
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
  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

Classes: QueryMethods

Instance Attribute Summary

Attributes included from Table

#association_name

Backend Configuration collapse

Instance Method Summary collapse

Methods included from Table

#each_locale, #initialize, #read, #write

Methods included from Mobility::Backend::OrmDelegator

#for

Methods included from Mobility::Backends::ActiveRecord

included, #setup_query_methods

Class Method Details

.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 self.configure(options)
  table_name = options[:model_class].table_name
  options[:table_name]  ||= "#{table_name.singularize}_translations".freeze
  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



144
145
146
147
148
# File 'lib/mobility/backends/active_record/table.rb', line 144

def translation_for(locale, _)
  translation = translations.find { |t| t.locale == locale.to_s.freeze }
  translation ||= translations.build(locale: locale)
  translation
end