Ruby on Rails gem inclusion and initialisation order for reopening a model declared in a gem from another gem
Originally, I have posted Stack Overflow question Ruby on Rails gems... Re-open models (now deleted). But I believe this question is way too confusing... I'll try to ask differently based on what I have discovered.
Let's say...
- Gem A has an engine (lib/a/engine.rb) and declares a model M (ActiveRecord based, 'app/models/m.rb').
- In gem A, there is an entry point (lib/a.rb) which is parsed when rails loads the gem.
- In gem B, there is an entry point (lib/b.rb) which is parsed when rails loads the gem.
- Gem B depends on gem A (specified in gemspec) and needs to re-open the A::M model.
- I have checked that 'a.rb' is loaded before开发者_Go百科 'b.rb', and it is.
Now my problem is that while being in 'b.rb', if I try to do a class_eval on the M model (From gem A, remember?) to re-open it and add 'B specific logic' to it, the M class is not yet defined... I have put messages in 'm.rb', and I figured that it is being loaded a long time after 'a.rb' and 'b.rb' are loaded...
From within gem B, how can I get a callback after ActiveRecord
have loaded my model A::M? Do I need to do something in my engines? Or in other words, what files are loaded after ActiveRecord has loaded models in A?
Just in case, make sure to load gem A manually in gem B as well.
精彩评论