Module: ActiveVersion::Translations::HasTranslations::ClassMethods
- Defined in:
- lib/active_version/translations/has_translations.rb,
sig/active_version/translations.rbs
Instance Method Summary collapse
-
#scope_for_translated_attribute(attribute_name, value, locale: I18n.locale) ⇒ Object
Scope for translated attribute.
- #setup_translation_associations ⇒ Object
-
#translated_copies(*attribute_names) ⇒ Object
Auto-generate copy methods for translated attributes.
-
#translated_scopes(*attribute_names) ⇒ Object
Auto-generate scopes for translated attributes.
- #translation_class ⇒ Class
- #translation_class_name ⇒ String
- #translation_record? ⇒ Boolean
Instance Method Details
#scope_for_translated_attribute(attribute_name, value, locale: I18n.locale) ⇒ Object
Scope for translated attribute
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/active_version/translations/has_translations.rb', line 131 def scope_for_translated_attribute(attribute_name, value, locale: I18n.locale) foreign_key = if translation_class.respond_to?(:source_foreign_key) translation_class.source_foreign_key else "#{name.underscore}_id" end identity_columns = Array(foreign_key).map(&:to_s) scope = translation_class.where(locale: locale).where(attribute_name => value) if identity_columns.one? ids = scope.select(identity_columns.first) where(id: ids) else ids = scope.pluck(*identity_columns) where(primary_key => ids) end end |
#setup_translation_associations ⇒ Object
8 |
# File 'sig/active_version/translations.rbs', line 8
def setup_translation_associations: () -> untyped
|
#translated_copies(*attribute_names) ⇒ Object
Auto-generate copy methods for translated attributes
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/active_version/translations/has_translations.rb', line 164 def translated_copies(*attribute_names) define_method(:copy_values_from_translation) do attribute_names.each do |attribute_name| if respond_to?(:will_save_change_to_attribute?) && will_save_change_to_attribute?(attribute_name) next end if respond_to?(:attribute_changed?) && attribute_changed?(attribute_name) next end next if self[attribute_name].present? value = translate(attribute_name, locale: I18n.default_locale).presence value ||= translations.first&.send(attribute_name) self[attribute_name] = value if value.present? end end end |
#translated_scopes(*attribute_names) ⇒ Object
Auto-generate scopes for translated attributes
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/active_version/translations/has_translations.rb', line 150 def translated_scopes(*attribute_names) attribute_names.each do |attribute_name| define_singleton_method(:"for_translated_#{attribute_name}") do |value, locale: I18n.locale| scope_for_translated_attribute(attribute_name, value, locale: locale) end define_singleton_method(:"find_by_translated_#{attribute_name}") do |value, locale: I18n.locale| scope_for_translated_attribute(attribute_name, value, locale: locale) .first || find_by(attribute_name => value) end end end |
#translation_class ⇒ Class
6 |
# File 'sig/active_version/translations.rbs', line 6
def translation_class: () -> Class
|
#translation_class_name ⇒ String
7 |
# File 'sig/active_version/translations.rbs', line 7
def translation_class_name: () -> String
|
#translation_record? ⇒ Boolean
5 |
# File 'sig/active_version/translations.rbs', line 5
def translation_record?: () -> bool
|