Module: EmbeddedLocalization::ActiveRecord::ActMacro
- Defined in:
- lib/embedded_localization/active_record/act_macro.rb
Constant Summary collapse
- STORAGES =
[:yaml, :json, :jsonb, :hstore].freeze
Instance Method Summary collapse
-
#translates(*attr_names) ⇒ Object
translates :name, :description #
i18nis a text column, stored as YAML (default) translates :name, :description, storage: :json #i18nis a json or jsonb column translates :name, :description, storage: :hstore #i18nis a PostgreSQL hstore column translates :name, :description, fallbacks: true # a locale without translations reads the I18n.default_locale value. - #translates? ⇒ Boolean
Instance Method Details
#translates(*attr_names) ⇒ Object
translates :name, :description # i18n is a text column, stored as YAML (default)
translates :name, :description, storage: :json # i18n is a json or jsonb column
translates :name, :description, storage: :hstore # i18n is a PostgreSQL hstore column
translates :name, :description, fallbacks: true # a locale without translations reads the I18n.default_locale value
for details about I18n fallbacks, check the source: i18n-0.9.0/lib/i18n/backend/fallbacks.rb i18n-0.9.0/lib/i18n/locale/fallbacks.rb
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/embedded_localization/active_record/act_macro.rb', line 14 def translates(*attr_names) return if translates? # cludge to make sure we don't set this up twice.. = attr_names. storage = ([:storage] || :yaml).to_sym unless STORAGES.include?(storage) raise ArgumentError, "unknown storage: #{storage.inspect} -- use one of #{STORAGES.inspect}" end class_attribute :translated_attribute_names, :translation_options self.translated_attribute_names = attr_names.map(&:to_sym).sort.uniq self. = include InstanceMethods extend ClassMethods # the `i18n` column holds all translations of the record as {locale => {attribute => value}} # we should also protect it from direct assignment by the user case storage when :yaml serialize :i18n, coder: YAML, type: Hash when :json, :jsonb attribute :i18n, EmbeddedLocalization::Storage::Json.new when :hstore attribute :i18n, EmbeddedLocalization::Storage::Hstore.new end after_initialize :initialize_i18n_hashes # dynamically define the accessors for the translated attributes: translated_attribute_names.each do |attr_name| define_method(attr_name) do |locale = I18n.locale| get_localized_attribute(attr_name, locale) end define_method("#{attr_name}=") do |new_translation| set_localized_attribute(attr_name, I18n.locale, new_translation) end end end |
#translates? ⇒ Boolean
55 56 57 |
# File 'lib/embedded_localization/active_record/act_macro.rb', line 55 def translates? included_modules.include?(InstanceMethods) end |