module Translatable::ClassMethods
Public Instance Methods
translatable(*attributes)
click to toggle source
# File lib/translatable.rb, line 7 def translatable(*attributes) @translatable = attributes attributes&.each do |attribute| define_method attribute do v = self[attribute] v[I18n.locale.to_s] if v.is_a?(Hash) && v.key?(I18n.locale.to_s) end end end
translatable_factory(value)
click to toggle source
if the available locales are `en` and `sv` returns something like: {
en: value, sv: value
}
# File lib/translatable.rb, line 35 def translatable_factory(value) val = {} I18n.available_locales.each do |locale| val[locale.to_sym] = value end val end
where(*args)
click to toggle source
Calls superclass method
# File lib/translatable.rb, line 17 def where(*args) if args.size == 1 && args.first.is_a?(Hash) && @translatable&.include?(args.first.keys.first) key = args.first.keys.first value = args.first[key] return super "#{key}->>'#{I18n.locale}' = ?", value # args = args&.first&.transform_keys do |key| # @translatable.include?(key) ? "#{key}->'#{I18n.locale}'" : key # end end # raise args.inspect super(*args) end