Created
April 6, 2011 09:54
A MongoMapper Model, the LocalizedString custom data type, and the Console Log.
This file contains hidden or 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
>> f = Foo.new({:title => "Title"}) | |
NoMethodError: undefined method `translations' for "Title":String | |
from /Volumes/MEDIA/projects/oratsouyts/lib/LocalizedString.rb:19:in `to_mongo' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys/key.rb:51:in `set' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:310:in `write_key' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/dirty.rb:59:in `write_key' | |
from (eval):10:in `title=' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:183:in `send' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:183:in `attributes=' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:181:in `each_pair' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:181:in `attributes=' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:211:in `assign' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/accessible.rb:21:in `assign' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/protected.rb:30:in `assign' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/keys.rb:165:in `initialize' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/dirty.rb:14:in `initialize' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/sci.rb:26:in `initialize' | |
from /Library/Ruby/Gems/1.8/bundler/gems/mongomapper-b3ab248dd907/lib/mongo_mapper/plugins/callbacks.rb:7:in `initialize' | |
from (irb):1:in `new' | |
from (irb):1>> |
This file contains hidden or 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
class Foo | |
include MongoMapper::Document | |
key :title, LocalizedString | |
timestamps! | |
end |
This file contains hidden or 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
class LocalizedString | |
attr_accessor :translations | |
def initialize( translations = {} ) | |
@translations = translations | |
end | |
def self.from_mongo(value) | |
if value.is_a?(Hash) | |
LocalizedString.new(value) | |
elsif value.nil? | |
LocalizedString.new() | |
else | |
LocalizedString.new( { I18n.locale.to_s => value }) | |
end | |
end | |
def self.to_mongo(value) | |
value.translations if value.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment