Class: Mobility::Backend::ActiveRecord::Table
- Includes:
- Mobility::Backend
- Defined in:
- lib/mobility/backend/active_record/table.rb
Overview
Implements the Table backend for ActiveRecord models.
Defined Under Namespace
Classes: QueryMethods
Instance Attribute Summary collapse
-
#association_name ⇒ Symbol
readonly
Name of the association method.
Attributes included from Mobility::Backend
Backend Accessors collapse
-
#read(locale, **options) ⇒ Object
Value of translation.
-
#write(locale, value, **options) ⇒ Object
Value of translation.
Backend Configuration collapse
Cache Methods collapse
Instance Method Summary collapse
-
#initialize(model, attribute, **options) ⇒ Table
constructor
A new instance of Table.
Methods included from Mobility::Backend
Constructor Details
#initialize(model, attribute, **options) ⇒ Table
Returns a new instance of Table.
85 86 87 88 |
# File 'lib/mobility/backend/active_record/table.rb', line 85 def initialize(model, attribute, **) super @association_name = [:association_name] end |
Instance Attribute Details
#association_name ⇒ Symbol (readonly)
Returns 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
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!() 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 else [:association_name] = :mobility_model_translations [:subclass_name] ||= :Translation end %i[foreign_key association_name subclass_name].each { |key| [key] = [key].to_sym } end |
Instance Method Details
#clear_cache ⇒ Object
170 171 172 |
# File 'lib/mobility/backend/active_record/table.rb', line 170 def clear_cache model_cache.try(:clear) end |
#new_cache ⇒ Table::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.
92 93 94 |
# File 'lib/mobility/backend/active_record/table.rb', line 92 def read(locale, **) translation_for(locale).send(attribute) end |
#write(locale, value, **options) ⇒ Object
Returns Value of translation.
97 98 99 |
# File 'lib/mobility/backend/active_record/table.rb', line 97 def write(locale, value, **) translation_for(locale).tap { |t| t.send("#{attribute}=", value) }.send(attribute) end |
#write_to_cache? ⇒ Boolean
166 167 168 |
# File 'lib/mobility/backend/active_record/table.rb', line 166 def write_to_cache? true end |