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

Console hooks are broken #74

Open
nhocki opened this issue Jun 3, 2015 · 3 comments
Open

Console hooks are broken #74

nhocki opened this issue Jun 3, 2015 · 3 comments

Comments

@nhocki
Copy link

nhocki commented Jun 3, 2015

Hi,

I'm trying to add default methods to the console like this:

# config/application.rb
# Source http://opensoul.org/2012/11/08/add-helper-methods-to-your-rails-console/
console do
  require 'console_foo'
  Rails::ConsoleMethods.include(ConsoleFoo)
end

Rails includes Rails::ConsoleMethods into the console::ExtendCommandBundle module, in this case that'd be Pry::ExtendCommandBundle which is an empty module so nothing is happening.

I could get this working by doing TOPLEVEL_BINDING.eval('self').extend ConsoleFoo but that seems a lot like a hack, is there a way we could have ExtendCommandBundle work?

The console block gets called before console.start (Pry.start) so maybe there's something to be done there?

Thanks

@mlangenberg
Copy link

Just ran into this as well. Can confirm that switching back to IRB mitigates the problem.
When you run include Rails::ConsoleMethods in the console, the methods are available again.

class Application < Rails::Application
    module ConsoleHelpers
      def bar; :bar; end
    end
    console do
      module Rails::ConsoleMethods
        include ConsoleHelpers
        def foo; :foo; end
      end
    end
end
rails c
Loading development environment (Rails 4.1.6)
[1] pry(main)> foo
=> :foo
[2] pry(main)> bar
NameError: undefined local variable or method `bar' for main:Object
from (pry):2:in `__pry__'

Funny right?

@nhocki
Copy link
Author

nhocki commented Aug 1, 2015

@mlangenberg yes. Going back to plain IRB fixes the problem. I actually ended up extending TOPLEVEL_BINDING. I blogged about this here.

@mlangenberg
Copy link

Thanks for documenting your workaround @nhocki 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants