Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run scratch file in RubyMine or IDEA in Rails environment?

I want to use scratch files as a console replacement for prototyping in my Rails apps. The simplest solution I've found is requiring environment manually like this:

require '/project/path/config/environment.rb'

But it does not use Spring and is terribly slow because of that.

like image 735
Andrew Avatar asked Jun 05 '15 17:06

Andrew


1 Answers

I've found two ways to do it, depending on your workflow one might fit you better than another. Assuming you're using IDEA 14 (it might be different for earlier versions) and Rails 4.1+.

Custom runner

  1. In top menu Run -> Edit Configurations...;
  2. Configuration tab:
    2.1. Ruby script: <Path to your bin/rails file>;
    2.2. Script arguments: runner <Path to your script>;
    2.3. Working directory: <Your project dir>;
    2.4. Environment variables: RAILS_ENV=development;
    2.5. Ruby arguments: -e '$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)';
    2.6. Ruby SDK: <Your project SDK>.
  3. Bundler tab:
    3.1. Run the script in context of the bundle (bundle exec): <check>.

The problem with this approach is that you would have to manually change path to scratch file each time you want to run the different one (please comment if you know workaround).

External tool

Assuming you have Spring installed:

  1. In top menu IntelliJ IDEA -> Preferences;
  2. Tools -> External Tools;
  3. Hit + in the bottom of the menu:
    3.1. Name: Rails Runner;
    3.2. Program: $ProjectFileDir$/bin/spring;
    3.3. Parameters: rails runner $FilePath$;
    3.4. Working directory: $ProjectFileDir$.
  4. Keymap:
    4.1. Search for Rails Runner;
    4.2. Double-click and add custom shortcut (Alt + S is convenient and available).

The only problem with this approach is that you have to have custom shortcut to make it convenient.

like image 183
Andrew Avatar answered Sep 30 '22 19:09

Andrew