Last active
August 29, 2015 13:57
-
-
Save galetahub/9911105 to your computer and use it in GitHub Desktop.
MongoId translates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Adriano | |
module Translate | |
extend ::ActiveSupport::Concern | |
module ClassMethods | |
attr_accessor :translated_fields | |
def translates(*args) | |
self.translated_fields = *args | |
I18n.available_locales.each do |locale| | |
translated_fields.each do |field| | |
define_method "#{field}_#{locale}" do | |
read_translation(field, locale) | |
end | |
define_method "#{field}_#{locale}=" do |value| | |
write_translation(field, value, locale) | |
end | |
end | |
end | |
end | |
end | |
protected | |
def read_translation(field, locale) | |
hash = send("#{field}_translations") | |
hash[field.to_s][locale.to_s] if hash && hash[field.to_s] | |
end | |
def write_translation(field, value, locale) | |
hash = send("#{field}_translations") | |
if hash && hash[field.to_s] | |
hash[field.to_s][locale.to_s] = value | |
send("#{field}_translations=", hash) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment