Class: Mobility::Backends::ActiveRecord::Table
- Inherits:
-
Object
- Object
- Mobility::Backends::ActiveRecord::Table
- 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.
Defined Under Namespace
Classes: QueryMethods
Instance Attribute Summary
Attributes included from Table
Backend Configuration collapse
Instance Method Summary collapse
Methods included from Table
#each_locale, #initialize, #read, #write
Methods included from Mobility::Backend::OrmDelegator
Methods included from Mobility::Backends::ActiveRecord
included, #setup_query_methods
Class Method Details
.configure(options) ⇒ Object
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() table_name = [:model_class].table_name [:table_name] ||= "#{table_name.singularize}_translations".freeze [:foreign_key] ||= table_name.downcase.singularize.camelize.foreign_key if (association_name = [:association_name]).present? [:subclass_name] ||= association_name.to_s.singularize.camelize.freeze else [:association_name] = :translations [:subclass_name] ||= :Translation end i[foreign_key association_name subclass_name table_name].each { |key| [key] = [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 |