Я пытаюсь создать собственный тип данных MongoMapper в RoR 2.3.5 под названием Translatable:
class Translatable < String
def initialize(translation, culture="en")
end
def languages
end
def has_translation(culture)?
end
def self.to_mongo(value)
end
def self.from_mongo(value)
end
end
Я хочу иметь возможность использовать его так:
class Page
include MongoMapper::Document
key :title, Translatable, :required => true
key :content, String
end
Затем реализуйте так:
p = Page.new
p.title = "Hello"
p.title(:fr) = "Bonjour"
p.title(:es) = "Hola"
p.content = "Some content here"
p.save
p = Page.first
p.languages
=> [:en, :fr, :es]
p.has_translation(:fr)
=> true
en = p.title
=> "Hello"
en = p.title(:en)
=> "Hello"
fr = p.title(:fr)
=> "Bonjour"
es = p.title(:es)
=> "Hola"
Я предполагаю, что в mongoDB информация будет храниться так:
{ "_id" : ObjectId("4b98cd7803bca46ca6000002"), "title" : { "en" :
"Hello", "fr" : "Bonjour", "es" : "Hola" }, "content" : "Some content
here" }
Таким образом, Page.title — это строка, которая по умолчанию имеет значение английский (:en), если культура не указана.
Буду очень признателен за любую помощь.