Created
April 1, 2010 20:48
-
-
Save gudata/352346 to your computer and use it in GitHub Desktop.
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
class Buy | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include Mongoid::MongoTranslation | |
has_many_related :criterias | |
field :contact_id, :type => Integer, :index => true | |
field :offer_type_id, :type => Integer, :index => true | |
field :created_by_user_id, :type => Integer, :index => true | |
field :status_id, :type => Integer, :index => true | |
field :number, :type => Integer, :index => true | |
field :user_id, :type => Integer, :index => true | |
field :name | |
field :description | |
translates :name | |
def user | |
@user ||= User.find(user_id) | |
end | |
end |
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
>> Buy.new | |
from -> Mongoid::Document.self.included(base = Buy) | |
from -> Mongoid::Fields.self.included(base = Buy) | |
from -> Mongoid::Document.self.included(base = #<Class:0xb60a4f40>) | |
from -> Mongoid::Fields.self.included(base = #<Class:0xb60a4f40>) | |
belongs_to buy :inverse_of => buy_translations | |
true | |
defining has_many buy_translations | |
initilize nil | |
=> #<Buy _id: 4bb507479ec2255dcd000001, name: nil, number: nil, created_at: nil, updated_at: nil, contact_id: nil, user_id: nil, offer_type_id: nil, description: nil, status_id: nil, created_by_user_id: nil> | |
>> BuyTranslation.new | |
initilize nil | |
NoMethodError: You have a nil object when you didn't expect it! | |
You might have expected an instance of Array. | |
The error occurred while evaluating nil.inject | |
from /usr/lib/ruby/gems/1.8/gems/mongoid-1.2.14/lib/mongoid/fields.rb:38:in `defaults' | |
from /usr/lib/ruby/gems/1.8/gems/mongoid-1.2.14/lib/mongoid/fields.rb:15:in `__send__' | |
from /usr/lib/ruby/gems/1.8/gems/mongoid-1.2.14/lib/mongoid/fields.rb:15:in `defaults' | |
from /usr/lib/ruby/gems/1.8/gems/mongoid-1.2.14/lib/mongoid/document.rb:277:in `attributes_with_defaults' | |
from /usr/lib/ruby/gems/1.8/gems/mongoid-1.2.14/lib/mongoid/document.rb:168:in `initialize' | |
from (irb):2:in `new' | |
from (irb):2 | |
>> |
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
# encoding: utf-8 | |
module Mongoid | |
module MongoTranslation | |
def self.included(klass) | |
klass.extend ClassMethods | |
end | |
def create_or_update_translations(params) | |
translations = send("#{self.class.to_s}Translation".underscore.pluralize.to_sym) | |
translation_class = Kernel.const_get("#{self.class.to_s}Translation") | |
# update existing translations | |
translations.each do |translation| | |
translation.update_attributes(params.delete(translation.locale)) | |
end | |
# create new translations | |
(AVAILABLE_LOCALES - [I18n.default_locale]).each do |lang| | |
attrs = params[lang.to_s] | |
if attrs | |
puts "update translation_class.new(attrs) = #{translation_class}.new(#{attrs.inspect})" | |
tr = translation_class.new(attrs) | |
tr.locale = lang.to_s | |
translations << tr | |
end | |
end | |
save | |
end | |
module ClassMethods | |
def locale=(locale) | |
@@locale = locale | |
end | |
def locale | |
(defined?(@@locale) && @@locale) || I18n.locale | |
end | |
def translates(*fields) | |
klass_name = "#{name.camelcase}Translation" | |
parent_association = name.underscore.to_sym | |
association = klass_name.underscore.pluralize.to_sym | |
# Create the translation model | |
translation_model = Class.new do | |
include Mongoid::Document | |
field :locale, :type => String | |
fields.each do |field_name| | |
field field_name.to_sym, :type => String | |
end | |
puts "belongs_to #{parent_association} :inverse_of => #{association}" | |
belongs_to parent_association, :inverse_of => association | |
puts collection_name | |
puts embedded | |
end | |
Object.const_set klass_name, translation_model | |
instance_eval do | |
puts "defining has_many #{klass_name.underscore.pluralize.to_sym}" | |
has_many klass_name.underscore.pluralize.to_sym | |
end | |
# Add attributes readers | |
class_eval do | |
fields.each do |field_name| | |
define_method(field_name) do | *params | | |
requested_locale = params.first | |
requested_locale = I18n.locale if requested_locale.nil? | |
if requested_locale == I18n.default_locale | |
instance_variable_get("@#{field_name}") | |
else | |
translations = send(klass_name.underscore.pluralize.to_sym) | |
translated_attr = nil | |
translations.each do |translation| | |
translated_attr = translation.send(field_name.to_sym) if translation.locale.to_sym == requested_locale | |
end | |
translated_attr = instance_variable_get("@#{field_name}") if translated_attr.nil? | |
translated_attr | |
end | |
end # define method | |
end | |
end | |
end | |
end | |
end | |
end | |
__END__ | |
b = Buy.new | |
I18n.locale = :bg | |
b.name ="ime" | |
I18n.locale = :ru | |
b.name ="poziwna" | |
b.inspect | |
translations = { | |
'ru' => { 'name' => 'testNL'}, | |
'bg' => { 'name' => 'testEN'} | |
} | |
b.create_or_update_translations(translations) | |
I18n.locale = :bg | |
b.name | |
I18n.locale = :ru | |
b.name | |
b.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment