Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hook include of helper to loading of ActionView #275

Merged
merged 2 commits into from
Jul 13, 2016

Conversation

koenpunt
Copy link
Contributor

@koenpunt koenpunt commented Jul 4, 2016

this prevents errors with load order of helpers when using rspec for tests.

I also think this should be the way to go for loading extensions.

This is small part of the changes from my previous PR #269

this prevents errors with load order of helpers when using rspec for
tests
@mattbrictson
Copy link
Contributor

Looks good. Do you think this warrants a mention in the changelog?

@koenpunt
Copy link
Contributor Author

koenpunt commented Jul 5, 2016

I think not, because this helper is only used within ActionView, right? So without ActionView being loaded, there's no use in having these helpers available.

@koenpunt
Copy link
Contributor Author

koenpunt commented Jul 5, 2016

We could consider moving it to an initializer in the railtie/engine:

module BootstrapForm
  module Rails
    class Engine < ::Rails::Engine
      initializer "bootstrap_form.action_view" do
        ActiveSupport.on_load(:action_view) do
          include BootstrapForm::Helper
        end
      end
    end
  end
end

And maybe also require the helper file in there.

@mattbrictson
Copy link
Contributor

I am not entirely familiar with the load order problem you are addressing with this, so please go with whatever you think works best. From a code style standpoint, I prefer the ::Rails::Engine approach, since that seems to be the "Rails way" to do it.

@koenpunt
Copy link
Contributor Author

koenpunt commented Jul 6, 2016

I will try to setup a sample app which demonstrates the issue.

@koenpunt
Copy link
Contributor Author

I've setup a sample app with some instructions in the README: https://github.com/koenpunt/bootstrap-forms-test

@mattbrictson
Copy link
Contributor

Thanks for the sample app. I see exactly the problem you've described.

Would you like me to merge this as-is, or did you want to switch to the ::Rails::Engine initializer?

@koenpunt
Copy link
Contributor Author

Not sure, but I had a similar issue with the SimpleForm gem, so I'll check if that's the case. If so then I will put it up on the Rails mailing list to see if can get some insight in why this happens and what the recommended way of fixing is

@koenpunt
Copy link
Contributor Author

Turns out it is a problem being introduced by rails-controller-testing rails/rails-controller-testing#24 (comment), and using on_load is the recommended way of loading.
Still not sure if it should be in the initializer or not...

@koenpunt
Copy link
Contributor Author

I've got an answer from @rafaelfranca on the matter:

It depends, if action view is not required to the gem work in a railtie. One example of this is the observers initializer we have in activeresource. You can make activeresource work without observers.

If the gem only works if action view is present it can be in the place that do the inclusion, in the case in the lib file.

@mattbrictson
Copy link
Contributor

Great, thanks for getting to the bottom of this. Could you add a mention to the bugfixes section of the changelog? Then I'll merge this in!

@koenpunt
Copy link
Contributor Author

Done!

@mattbrictson
Copy link
Contributor

Thanks! 🙇

@mattbrictson mattbrictson merged commit 3b3130a into bootstrap-ruby:master Jul 13, 2016
@koenpunt koenpunt deleted the lazy-load branch July 13, 2016 15:03
@koenpunt koenpunt restored the lazy-load branch July 18, 2016 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants