iruby icon indicating copy to clipboard operation
iruby copied to clipboard

Feature request: `rails c`

Open rattrayalex opened this issue 10 years ago • 9 comments

Hi,

Would be awesome to use this within the context that rails c sets up. Is this possible?

Thanks!

rattrayalex avatar Nov 19 '15 22:11 rattrayalex

Came here looking for exactly this. Using this as a Pry replacement is fantastic, using this as a rails console replacement would be a coup.

ianjepperson avatar Dec 11 '15 21:12 ianjepperson

It is probably possible. Maybe someone wants to work on it? We would be happy to accept a patch.

minad avatar Jan 04 '16 20:01 minad

I worked around this by creating a rails-console.rb file in my notebook directory.

# Load the required libraries to mimic the "rails c" command
puts 'Loading environment...'
require '/path/to/rails/config/environment.rb'
require 'rails/console/app'
require 'rails/console/helpers'
puts 'Done.'

Then adding to the start of my notebook require "./rails-console.rb", and it works!

Now my issue is getting the logging output into the notebook (for instance, if I run User.all[0] I'd like to see the SQL SELECT statement). I've tried the following, but it ends up spitting the log info out to the real STDOUT and it shows in the iruby notebook console:

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.connection_pool.clear_reloadable_connections!

So close! I suspect I need to find the object that represents the notebook console output, but I can't seem to locate it. Any pointers?

If I get this 100%, maybe the best pattern would be a magic method to set the Rails instance?

ianjepperson avatar Jan 13 '16 18:01 ianjepperson

+1

stardiviner avatar Jan 14 '16 02:01 stardiviner

Got it! Here's my updated rails-console.rb

# Load the required libraries to mimic the "rails c" command
puts 'Loading environment...'
require '/path/to/rails/config/environment.rb'
require 'rails/console/app'
require 'rails/console/helpers'

ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Base.logger.formatter = proc do |severity, datetime, progname, msg|
   "#{msg}\n"
end
ActiveRecord::Base.connection_pool.clear_reloadable_connections!
puts 'Done.'

ianjepperson avatar Jan 14 '16 03:01 ianjepperson

I think the most reasonable solution is to have a rails command that takes the path to the environment as an argument. Essentially, it does what my workaround file does and would be invoked like so:

In [1]: rails /path/to/rails
Loading environment...
Done

To be more flexible (Windows different path separators, Rails 2 vs. 3) it should be a bit more complex, as described here: http://www.webascender.com/Blog/ID/589/Code-Spelunking-in-Rails-with-Pry#.Vpfs7pMrLBI

It's working for me for now, but if I get some free time I'll see about adding a pull request.

ianjepperson avatar Jan 14 '16 18:01 ianjepperson

It's pretty easy to get working with a few setup commands (see demo notebook ), but it'd be great to get a built in iruby function.

It looks like you might also be able to pass the right flags to irb, but I'm not quite sure what the right ones would be. I'd think the iruby server would be run in the rails root directory, but if it could run from some else that'd be cool too.

wstrinz avatar Jan 20 '16 19:01 wstrinz

I just implemented something like this. I use RVM, so my old solution was a specialized kernel with hardcoded paths in it. This worked but wasn't portable to anyone else's machine without the same setup process. I rearranged things to arrive at this.

Characteristics:

  • Run rake jupyter:notebook to start the server
  • Single require 'notebooks/rails' to load the rails environment
  • Compatible with RVM
  • Portable (no absolute paths)

I ended up creating a rake task to start Jupyter with the right set of environment variables. My lib/tasks/jupyter.rake is simple:

namespace :jupyter do
  task :notebook do
    root = File.absolute_path(__FILE__ + "/../../..")
    env = {
      "RUBYLIB" => ($: + [ root ]).join(":")
    }
    Process.exec(env, "jupyter", "notebook")
  end
end

I also created a quick bootstrap script, which reads like this, saved as notebooks/rails.rb:

Dir.chdir File.dirname(File.dirname(__FILE__))
unless defined? Rails
  APP_PATH = File.expand_path('../../config/application',  __FILE__)
  require File.expand_path('../../config/boot',  __FILE__)
  require APP_PATH
  Rails.application.require_environment!
end
puts "Loaded #{Rails.env} environment"

Now in my notebooks (which I keep in the notebooks folder) just have a single initialization line:

require 'notebooks/rails'

CGamesPlay avatar Apr 01 '16 23:04 CGamesPlay

I made iruby-rails.gem.

Please check it.

mrkn avatar Jun 28 '16 04:06 mrkn