I encounter loading problems when developing a gem, which adds cache_at 
method to active_model. 

In the following example, the cache_at method needs to add after_commit 
callback to Profile, but Profile may has not been loaded yet.

class User < ActiveRecord::Base has_one :profile cache_at :profile end

It will be useful if there is a way to know when the constant gets 
autoloaded.

ActiveSupport::Dependencies.onload('Profile') do
after_commit ->{ clean_the_cache }
end

Mario Visic於 2016年1月23日星期六 UTC+8上午2時10分32秒寫道:
>
> Hi all
>
> I work on some fairly large ruby on rails applications and boot up time in 
> development always starts to become an issue, using a preloader like zeus 
> or spring helps but reducing the first boot time is always useful. One of 
> the big slowdowns for booting these large apps usually turns out to be 
> initializers touching constants which then triggers an autoload. There is a 
> popular pattern used where the class definition and configuration are split 
> up into two, you then end up with a file in config/initializers which will 
> run something like: MyClass.configure, which then triggers MyClass to be 
> defined and slows down the boot process.
>
> It would be nice instead to be able to run the configuration code when the 
> constant gets autoloaded, something like this:
>
> ActiveSupport::Dependencies.on_load('MyClass') do
>   MyClass.configure do |config|
>     # config here
>   end
> end
>
> That way we still have the benefit of splitting up the class definition 
> and the configuration, but we aren't eagerly loading the class in the 
> initializer, only later when it's needed.
>
> I've written a small patch which implements this behaviour: 
> https://github.com/mariovisic/rails/commit/b5ecbf36fa087c59e716e1e85eb05a5bcb658e29
> As you can see the logic is only a few lines.
>
> Does anyone have any thoughts on if this patch would be useful to others?
>
> Cheers
> Mario
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to