Hi. I have two types of fields in my database, e.g. "name" and "name_ru". First one is a user''s name in english and the second one is in russian. I want to intercept calls like "c.name" and add "_ru" to it if current language is russian (I have my own Locale class like n Globalize). So I want such thing: -- Locale.set("en-US") c.name = "Johny English" # will be stored in the "name" column Locale.set("ru-RU") c.name = "Johny Russian" # will be stored in the "name_ru" column -- What ActiveRecord class and method should I overwrite to implement such thing? Thanks. P.S. I''ve googled but without any result. I''m reading Globalize''s source code atm and I hope to get something from here. -- Posted via http://www.ruby-forum.com/.
On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote:> Hi. > > I have two types of fields in my database, e.g. "name" and "name_ru". > First one is a user''s name in english and the second one is in russian. > I want to intercept calls like "c.name" and add "_ru" to it if current > language is russian (I have my own Locale class like n Globalize). > > So I want such thing: > -- > Locale.set("en-US") > c.name = "Johny English" # will be stored in the "name" column > Locale.set("ru-RU") > c.name = "Johny Russian" # will be stored in the "name_ru" column > -- > > What ActiveRecord class and method should I overwrite to implement such > thing? > > Thanks. > > P.S. I''ve googled but without any result. I''m reading Globalize''s source > code atm and I hope to get something from here. >One way would be to do something like: class Something < ActiveRecord::Base def name_before_type_cast if locale_is_russian read_attribute(:name_ru) else read_attribute(:name) end end end I''ll leave implementing the "locale_is_russian" method up to you. Heh.
Wilson Bilkovich wrote:> On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote: >> c.name = "Johny English" # will be stored in the "name" column >> code atm and I hope to get something from here. >> > > One way would be to do something like: > class Something < ActiveRecord::Base > def name_before_type_cast > if locale_is_russian > read_attribute(:name_ru) > else > read_attribute(:name) > end > end > end > > I''ll leave implementing the "locale_is_russian" method up to you. Heh.I can''t do that because I don''t know exact name of an attribute. It can be "name" or "position", or "description". -- Posted via http://www.ruby-forum.com/.
On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote:> Wilson Bilkovich wrote: > > On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote: > >> c.name = "Johny English" # will be stored in the "name" column > >> code atm and I hope to get something from here. > >> > > > > One way would be to do something like: > > class Something < ActiveRecord::Base > > def name_before_type_cast > > if locale_is_russian > > read_attribute(:name_ru) > > else > > read_attribute(:name) > > end > > end > > end > > > > I''ll leave implementing the "locale_is_russian" method up to you. Heh. > > I can''t do that because I don''t know exact name of an attribute. It can > be "name" or "position", or "description". >Is there a reason you can''t just define three methods? def position_before_type_cast, description_before_type_cast, etc? Are there only three, or are there numerous methods like this?
Wilson Bilkovich wrote:> On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote: >> > read_attribute(:name_ru) >> > Is there a reason you can''t just define three methods? > def position_before_type_cast, description_before_type_cast, etc? > > Are there only three, or are there numerous methods like this?I don''t know what methods will I need. It depends on db scheme (as usual) and on translates method (as in Globalize: translates :name, :bla, :bla, ...). -- Posted via http://www.ruby-forum.com/.
Anton Kovalyov wrote:> Wilson Bilkovich wrote: >> On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote: >>> > read_attribute(:name_ru) >>> >> Is there a reason you can''t just define three methods? >> def position_before_type_cast, description_before_type_cast, etc? >> >> Are there only three, or are there numerous methods like this? > > I don''t know what methods will I need. It depends on db scheme (as > usual) and on translates method (as in Globalize: translates :name, > :bla, :bla, ...).Actually, I already have a solution: class ActiveRecord::Base def self.multilingual_field(fieldname) module_eval <<-end_eval def #{fieldname} send("#{fieldname}_\#{Locale.language.short_name}") end def #{fieldname}=(value) send("#{fieldname}_\#{Locale.language.short_name}=",value) end end_eval end end -- Posted via http://www.ruby-forum.com/.
Wilson Bilkovich
2006-Jul-03 16:36 UTC
[Rails] Re: Re: How can I intercept attribute calls?
On 7/3/06, Anton Kovalyov <anton@kovalyov.net> wrote:> Anton Kovalyov wrote: > > Wilson Bilkovich wrote: > >> On 7/2/06, Anton Kovalyov <anton@kovalyov.net> wrote: > >>> > read_attribute(:name_ru) > >>> > >> Is there a reason you can''t just define three methods? > >> def position_before_type_cast, description_before_type_cast, etc? > >> > >> Are there only three, or are there numerous methods like this? > > > > I don''t know what methods will I need. It depends on db scheme (as > > usual) and on translates method (as in Globalize: translates :name, > > :bla, :bla, ...). > > Actually, I already have a solution: > > class ActiveRecord::Base > def self.multilingual_field(fieldname) > module_eval <<-end_eval > def #{fieldname} > send("#{fieldname}_\#{Locale.language.short_name}") > end > > def #{fieldname}=(value) > send("#{fieldname}_\#{Locale.language.short_name}=",value) > end > end_eval > end > end > >Cool. Be warned that the built-in Rails form helpers don''t call the attribute directly, though. They call attribute_before_type_cast. text_field :something, :name will still show the ''raw'' column value, unless you implement the before_type_cast methods.
Anton Kovalyov
2006-Jul-03 17:35 UTC
[Rails] Re: Re: Re: How can I intercept attribute calls?
Wilson Bilkovich wrote:> > Cool. Be warned that the built-in Rails form helpers don''t call the > attribute directly, though. > They call attribute_before_type_cast. > > text_field :something, :name will still show the ''raw'' column value, > unless you implement the before_type_cast methods.Wow, I didn''t know about this. Thanks a lot. -- Posted via http://www.ruby-forum.com/.