Xavier, yes, you're right, usually config.to_prepare would be wrapped
around the MyClass.configure to ensure it is run again when the constant is
removed and autoloading brings in a new one.
Here's an actual example from an application i'm working on now. We have a
class which handles outages, think feature flips but for services going
down. There's a model which records the list of outages OutageSet. in an
initializer we have a list of outages which are setup, so like this:
# config/initializers/outages.rb
OutageSet.configure do |config|
config.outage :search, { some: 'options' }
end
Many gems follow this same pattern, but the we don't run into the same
issues as the constants are usually eagerly loaded. It only become a
problem when the constants in our initialiazer are using autoloading. I
think autoloading is GOOD here, as it reduces application boot time, but it
would be nice to be able to ensure the configuration runs as soon as the
constant is available rather than negating the benefits we get from
autoloading.
Also, yes, the code could be re-written so that internally OutageSet knows
about how to load its configuration, but I think the seperation of class
and configuration is a very good pattern, which many gems already employ,
it just needs a small addition to get it working well with autoloading.
On Saturday, January 23, 2016 at 6:49:17 AM UTC+11, Xavier Noria wrote:
>
> Let me understand the use case.
>
> Autoloading constants in initializers in general is not a good idea
> because the initializer only runs when the application boots.
>
> If MyClass.configure stores anything in the class object autoloaded at
> boot time, in development mode[*] when code changes all autoloaded
> constants are wiped. In particular next time you hit MyClass the constant
> is going to store a different class object that has no relationship with
> the other one.
>
> Why is the application autoloading in initializers?
>
> Xavier
>
> [*] Technically depends on the configuration.
>
--
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.