Hi Mark

I've just pushed a change for this - although I didn't use your  
version, because I wanted to avoid private methods. Also, there's  
different approaches depending on Rails 2.2 or 2.3.

If you could give this a shot, that'd be great, although I have tested  
it on the sample app you provided.

Cheers

-- 
Pat

On 25/04/2009, at 4:53 PM, DrMark wrote:

>
> Aloha Pat!
>
> I have solved the i18n issue. There were a couple of things to
> consider. First, you have two init.rb files:
>
> thinking-sphinx/init.rb  and
> thinking-sphinx/rails/init.rb
>
> The content in both files is not the same.
>
> In thinking-sphinx/init.rb you have:
>
>  require 'thinking_sphinx'
>
>  if Rails::VERSION::STRING.to_f < 2.1
>    ThinkingSphinx::Configuration.instance.load_models
>  end
>
>  if Rails::VERSION::STRING.to_f > 1.2
>    require 'action_controller/dispatcher'
>    ActionController::Dispatcher.to_prepare :thinking_sphinx do
>      ThinkingSphinx::Configuration.instance.load_models
>    end
>  end
>
>
> in thinking-sphinx/rails/init.rb you have:
>
>  require 'thinking_sphinx'
>  require 'action_controller/dispatcher'
>
>  ActionController::Dispatcher.to_prepare :thinking_sphinx do
>    ThinkingSphinx::Configuration.instance.load_models
>  end
>
>
> I would have expected the files to be the same.
>
> I have identified a couple of ways to solve the problem. According to
> the Rails Guide ( http://guides.rails.info/plugins.html )
>
>  "When rails loads plugins it looks for the
>  file named ‘init.rb’ or ‘rails/init.rb’."
>
> However, if you put a couple of "raise" calls in "thinking-sphinx/
> rails/init.rb" and "thinking-sphinx/init.rb" you will see that Rails
> calls "thinking-sphinx/rails/init.rb" if it is present and then never
> even calls "thinking-sphinx/init.rb".
>
> If you are going to use "thinking-sphinx/rails/init.rb" then you need
> this as its content:
>
>  require 'thinking_sphinx'
>
>  if Rails::VERSION::STRING.to_f < 2.1
>    ThinkingSphinx::Configuration.instance.load_models
>  end
>
>  if Rails::VERSION::STRING.to_f > 1.2
>    require 'action_controller/dispatcher'
>    ActionController::Dispatcher.to_prepare :thinking_sphinx do
>      I18n.backend.send(:init_translations)  if
> Rails::VERSION::STRING.to_f > 2.1
>      ThinkingSphinx::Configuration.instance.load_models
>    end
>  end
>
> Note that your suggested change ( I18n.available_locales ) does not
> work.
>
> If you delete the "thinking-sphinx/rails/init.rb" file then no changes
> are needed to "thinking-sphinx/init.rb" and the error doesn't even
> happen (due to order in which things are loaded). However, the
> convention when plugins can be used with multiple frameworks is to
> include the 'rails/init.rb' file.
>
> Therefore, my proposed fix is to change the content of both "thinking-
> sphinx/rails/init.rb" and "thinking-sphinx/init.rb" to
>
>       require 'thinking_sphinx'
>
>       if Rails::VERSION::STRING.to_f < 2.1
>         ThinkingSphinx::Configuration.instance.load_models
>       end
>
>       if Rails::VERSION::STRING.to_f > 1.2
>         require 'action_controller/dispatcher'
>         ActionController::Dispatcher.to_prepare :thinking_sphinx do
>           I18n.backend.send(:init_translations) if
> Rails::VERSION::STRING.to_f > 2.1
>           ThinkingSphinx::Configuration.instance.load_models
>         end
>       end
>
> I have made these changes in my fork of TS. You are welcome to pull
> from that if you like (1377ee8227dabf8b4917fc16e6586c024337196a).
>
> Thanks to you and Karel for your help!
>
> DrMark
>
>
> On Apr 23, 8:42 pm, Pat Allan <[email protected]> wrote:
>> On re-reading emails from RailsConf, I think I don't need to get  
>> those
>> slides and such done until the actual conference, it's just materials
>> attendees should have before the day. ie: Sphinx source and Thinking
>> Sphinx. Much easier to take care of, so I've put that aside, and dug
>> into the ActiveSupport source...
>>
>> While I18n loads the YAML files before Thinking Sphinx loads, it
>> doesn't process them automatically. Lazy loading if you like, just
>> like how Rails normally behaves with modesl... except then TS loads
>> the model forcibly, without the YAML parsing being considered
>> 'complete'.
>>
>> I guess one quick way of forcing it, in vendor/plugins/ 
>> thinking_sphinx/
>> rails/init.rb, add this line inside the block, before the load_models
>> call:
>>   I18n.available_locales
>>
>> Which *should* force the full loading.
>>
>> It's not ideal, and I'd love some better ideas on how to approach
>> this, but hopefully it'll do the job for the moment.
>>
>> --
>> Pat
>>
>> On 24/04/2009, at 3:42 PM, Pat Allan wrote:
>>
>>> Hi Mark
>>
>>> Definitely sounds like something to do with the order of loading
>>> (and yes, your demo app fails for me too). I'm *supposed* to be
>>> sorting out my RailsConf tutorial content at the moment, so I really
>>> shouldn't spend time digging into this at the moment.
>>
>>> But, a vague description of my approach: go hunting through Rails
>>> source, find out when the environment is prepared (ie: when TS
>>> manually reloads models), and when the I18n YAML file is parsed. I'm
>>> assuming the former happens before the latter, and it really should
>>> be the other way around... so if there's some later event hook we
>>> can tie the TS loading to, that'd hopefully make things better.
>>
>>> --
>>> Pat
>>
>>> On 24/04/2009, at 2:37 PM, DrMark wrote:
>>
>>>> Ok. I have identified an I18N issue caused by ThinkingSphinx and I
>>>> would appreciate thoughts on what is causing the issue. It has  
>>>> driven
>>>> me crazy all day. The problem wasn't obvious due to a large  
>>>> number of
>>>> recent changes in my app, but I have finally tracked the problem to
>>>> ThinkingSphinx.
>>
>>>> I use TS in my app and I use i18n extensively in a large app. I
>>>> upgraded to the latest TS a short while ago (I was running my fork
>>>> but
>>>> switched to the main one). When running my unit tests today, many  
>>>> of
>>>> my validation tests started failing with messages about  
>>>> "translation
>>>> missing". This is odd because all of my tests had been passing.
>>
>>>> After many hours of debugging I have shown that TS is causing the
>>>> problem. I have created a sample application that demonstrates the
>>>> problem. (http://files.me.com/drmarklane/rrxgcm)
>>
>>>> In the sample app I have a simple User model with the following
>>>> sample
>>>> validations:
>>
>>>> validates_presence_of :first_name,
>>>>           :message => I18n.t
>>>> ('users.errors.first_name_cant_be_blank')
>>>> validates_format_of   :last_name, :with=>/[^ \t\n\r\f\v&nbsp;]/,
>>>>           :message=>"#{I18n.t
>>>> ('users.errors.last_name_wrong_format')}"
>>
>>>> if you log in to the console then do:
>>>>>> u = User.new
>>>> => #<User id: nil, first_name: nil, last_name: nil, created_at:  
>>>> nil,
>>>> updated_at: nil>
>>>>>> u.valid?
>>>> => false
>>>>>> u.errors
>>>> => #<ActiveRecord::Errors:0x21d0f2c @errors={"first_name"=>
>>>> ["translation missing: en, users, errors,  
>>>> first_name_cant_be_blank"],
>>>> "last_name"=>["translation missing: en, users, errors,
>>>> last_name_wrong_format"]}, @base=#<User id: nil, first_name: nil,
>>>> last_name: nil, created_at: nil, updated_at: nil>>
>>
>>>> Notice the "translation missing" errors in the errors hash. Any  
>>>> unit
>>>> tests will also fail because of the translation missing errors.
>>>> Adding
>>>> default i18n settings in config.rb doesn't help.
>>
>>>> If you remove ThinkingSphinx, log out of the console, then repeat  
>>>> the
>>>> above steps you will get:
>>
>>>>>> u = User.new
>>>> => #<User id: nil, first_name: nil, last_name: nil, created_at:  
>>>> nil,
>>>> updated_at: nil>
>>>>>> u.valid?
>>>> => false
>>>>>> u.errors
>>>> => #<ActiveRecord::Errors:0x2357da0 @errors={"first_name"=>["Your
>>>> first name can't be blank"], "last_name"=>["Your first name is the
>>>> wrong format"]}, @base=#<User id: nil, first_name: nil, last_name:
>>>> nil, created_at: nil, updated_at: nil>>
>>
>>>> which is correct. Note that TS is not even called in the sample  
>>>> app.
>>>> Simply placing TS in the plugins folder causes the problem.
>>
>>>> Does anyone have any ideas as to the cause of this issue? I think  
>>>> the
>>>> issue is related to the class loading by TS. I would appreciate  
>>>> your
>>>> advice.
>>
>>>> Thanks!
>>
>>>> DrMark
>>
>>>> PS. Pat, your copy of not-a-mock doesn't build unless you set the  
>>>> gem
>>>> version of Rails to 2.2.2
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/thinking-sphinx?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to