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
Modules: TranslationsHasManyExtension Classes: Translation, Visitor
Backend Configuration collapse
Class Method Summary collapse
-
.apply_scope(relation, predicate, locale = Mobility.locale, invert: false) ⇒ ActiveRecord::Relation
Joins translations using either INNER/OUTER join appropriate to the query.
-
.build_node(attr, locale) ⇒ Mobility::Plugins::Arel::Attribute
Arel node for column on translation table.
Instance Method Summary collapse
-
#translation_for(locale) ⇒ Object
Returns translation for a given locale, or builds one if none is present.
Methods included from Table
#association_name, #each_locale, #foreign_key, #read, #subclass_name, #table_name, #write
Methods included from Mobility::Backends::ActiveRecord
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.
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.
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
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/mobility/backends/active_record/table.rb', line 102 def configure() table_name = model_class.table_name [:table_name] ||= "#{table_name.singularize}_translations" [: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
Returns translation for a given locale, or builds one if none is present.
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 |