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.
137 138 139 140 141 142 143 144 145 |
# File 'lib/mobility/backends/active_record/table.rb', line 137 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.
125 126 127 128 |
# File 'lib/mobility/backends/active_record/table.rb', line 125 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 114 115 116 117 118 119 |
# 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.classify.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| if [key].is_a?(Enumerable) [key] = [key].map!(&:to_sym) else [key] = [key].to_sym end } end |
Instance Method Details
#translation_for(locale) ⇒ Object
Returns translation for a given locale, or builds one if none is present.
297 298 299 300 301 |
# File 'lib/mobility/backends/active_record/table.rb', line 297 def translation_for(locale, **) translation = translations.in_locale(locale) translation ||= translations.build(locale: locale) translation end |