Class: Mobility::Backend::ActiveRecord::Table

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backend
Defined in:
lib/mobility/backend/active_record/table.rb

Overview

Implements the Table backend for ActiveRecord models.

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 collapse

Attributes included from Mobility::Backend

#attribute, #model, #options

Backend Accessors collapse

Backend Configuration collapse

Cache Methods collapse

Instance Method Summary collapse

Methods included from Mobility::Backend

included, method_name

Constructor Details

#initialize(model, attribute, **options) ⇒ Table

Returns a new instance of Table.

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • fallbacks (Hash)

    Fallbacks hash

  • association_name (Symbol)

    Name of association



85
86
87
88
# File 'lib/mobility/backend/active_record/table.rb', line 85

def initialize(model, attribute, **options)
  super
  @association_name = options[:association_name]
end

Instance Attribute Details

#association_nameSymbol (readonly)

Returns name of the association method.

Returns:

  • (Symbol)

    name of the association method



81
82
83
# File 'lib/mobility/backend/active_record/table.rb', line 81

def association_name
  @association_name
end

Class Method Details

.configure!(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • association_name (Symbol) — default: :mobility_model_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



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mobility/backend/active_record/table.rb', line 109

def self.configure!(options)
  table_name = options[: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
  else
    options[:association_name] = :mobility_model_translations
    options[:subclass_name] ||= :Translation
  end
  %i[foreign_key association_name subclass_name].each { |key| options[key] = options[key].to_sym }
end

Instance Method Details

#clear_cacheObject



170
171
172
# File 'lib/mobility/backend/active_record/table.rb', line 170

def clear_cache
  model_cache.try(:clear)
end

#new_cacheTable::TranslationsCache



160
161
162
163
# File 'lib/mobility/backend/active_record/table.rb', line 160

def new_cache
  reset_model_cache unless model_cache
  model_cache.for(attribute)
end

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



92
93
94
# File 'lib/mobility/backend/active_record/table.rb', line 92

def read(locale, **options)
  translation_for(locale).send(attribute)
end

#write(locale, value, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



97
98
99
# File 'lib/mobility/backend/active_record/table.rb', line 97

def write(locale, value, **options)
  translation_for(locale).tap { |t| t.send("#{attribute}=", value) }.send(attribute)
end

#write_to_cache?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/mobility/backend/active_record/table.rb', line 166

def write_to_cache?
  true
end