How to configure rubymine for use with open3? - ruby-on-rails

Issue
I am using xray-rails gem in a rails app and want it to open rubymine to the correct file when I click it in the browser. It was unclear how to configure this. xray-rails gem uses open3 to open the file in the editor.

Solution
Prerequisite: Follow the instructions for setting up rubymine to be callable from the commandline found in Running RubyMine as a Diff or Merge Command Line Tool
NOTE: This is also a handy use of rubymine, allowing it to perform diffs between files. Nice bonus for researching a solution to my problem.
Create ~/.xrayconfig with content...
:editor: mine
NOTE: mine is the name of the script created in the prerequisite steps. If you named your script something different, use that name in place of mine.
Now when you start xray-rails in your browser and click, the corresponding file will open in rubymine.

Related

How do I setup the ruby SDK in IntelliJ IDEA?

I've been using this guide
Whenever I go to import the module I get this screen:
I used the following file path, maybe this is whats wrong?
/usr/local/Cellar/ruby-build/20160130/share/ruby-build/2.3.0
And got this error:
I'd appreciate any ideas anyone might have, i've been trying get this working all night!
I ran into this problem with Intellij IDEA 2020.2. It took a while to resolve because the Intellij documentation seems to be missing one critical piece.
When you first open a project in IDEA, it assumes all your code is part of a Java module. With that module in place you cannot set a Ruby SDK at the module level. There's no option to do because the existing module is configured as Java.
Here's a screenshot of my example project with the default Java module. Notice the icon is a folder with a blue rectangle in the lower, right corner.
Here's what I had to do.
Open the Project Structure dialog (File | Project Structure).
In the Modules settings, highlight the top-level project module and click the delete button (looks like a minus sign).
Click the plus sign to add a new module.
From the pop-up click "New Module".
In the "New Module" pop-up select Ruby and the correct Module SDK (e.g. rbenv: 2.5.0)
When you are prompted to enter the Module name, Content root, and Module file location, make sure the directories are set to your project's root. When I entered the module name it appended the name to the project's root directory which is probably not what you want.
Once you've done that the module should appear with a Ruby icon on it and the rest should work as documented here: https://www.jetbrains.com/help/idea/configuring-language-interpreter.html
Here's a screenshot of my new Ruby module. You can see the Ruby icon replaced the blue rectangle.
I hope this saves you some time!
In order to set Ruby SDK for your project in IDEA you need to go to File | Project Structure | Project Settings and set project and module SDK.
Olivia is correct, the "Project Structure..." (Mac shortcut Cmd+;) is the dialog for configuring the IDE to use different ruby installations.
The first requirement is that the ruby manager (chruby, rbenv, rvm) is configured properly.
Another concern is exactly where and how jetbrains expects the ruby installation to be organized. I wonder if Intellij is compatible with the way ruby-install lays out ruby? That's the one I used with chruby. I could not make it work.
The posted url to Opening Rails projects in IntelliJ IDEA helped me feel the most confidence that we are on the right track. :)
At the time, I was failing to get Intellij configured due to fact the gem files were not seen by the IDE. All but about 4 gems in my project's Gemfile was being highlighted as having an SDK problem.
To end this, I stopped using chruby and ruby-install. I am not blaming chruby, however I could not make the chruby system work properly with the IDE. It worked fine in the shell. Note, I am a previous user of rvm and rbenv. Switching back to rbenv, now. Note that I've also stopped using ruby-build directly.
Instead, I built the first ruby version directly from rbenv:
rbenv install 2.3.1
Next, I created the .ruby-version file in the root of my rails project directory by running:
rbenv local 2.3.1
To help with any confusion regarding the minimum support needed in the shell startup scripts. Do not alter PATH at all. Place the following in your shell startup script system, whatever that may be:
# rbenv config in my .bash_profile
# --------------------------------
if which rbenv > /dev/null; then
eval "$(rbenv init -)";
fi
With the prerequisites out of the way...
I recommend doing the project import in the same way described in the jetbrains tutorial Opening Rails projects in IntelliJ IDEA.
In the first screenshot from thesowismine, I see two dialogs for different purposes that are simultaneously open. Unless I am mistaken, one of those dialogs should have received its info and closed before going forward, at least during the wizard-like process. Perhaps this was done and that dialog was reloaded later?
In any event, that is not where the Ruby SDK is associated to a Rails project. The screenshot indicates the User is browsing around the brew Cellar, which may indicate two things.
Ruby was installed with a brew install <version> command; I installed ruby by calling the rbenv ecosystem.
That particular dialog is for informing the IDE about the Rails project folder, not the Ruby kit.
Assuming the first dialog is provided with the root directory of the Rails app and next is clicked; then in the second dialog, I change nothing and click next. The 3rd dialog is where I confirm the project directory is correct and I can assign a more elaborate name (which is displayed in the IDE's project menu). Clicking next may prompt you to write over the ".idea/" directory. Say yes. The next dialog confirms that sources were found. Click next. Now Frameworks begin to be detected assuming the Ruby Manager is setup correctly. Click Finish.
Now, goto the "Project Structure..." dialog to set the SDK.
Before or after setting the SDK, go to the the project directory of your Rails project, run:
gem install bundle
bundle install
This will install all the gems your project requires including the rails gem, as presumably it is listed in your Gemfile.
Note that gemsets do not come up, here. In this config, the set of gems are associated to a particular Ruby installation. Bundler is your friend.
Languages & Frameworks > Ruby SDK and Gems
I was able to solve this by doing:
Preferences | Plugins | Install JetBrains plugin

How can I see the source code of a gem installed on my machine?

I installed Devise in my Rails app and I want to read through the source code and learn how it works. I looked through the entire folder structure of my Rails app, but couldn't find any code (except for the method calls).
I know I can see the source from the Github repository but I would like to see it in my editor and on my local machine.
I'm guessing this code must be in some main Ruby directory, but I'm having trouble locating it. Any help is appreciated. Thanks.
Besides Sergio's suggestion, there is another option.
Within your Rails path
$ bundle open devise
This will open the installed gem in editor with the version specified in Gemfile, very handy.
Try gem unpack, it will copy source of a gem to current directory. For example,
gem unpack rails
Documentation: gem unpack.
Simply run bundle show <gem-name>,
it will list the absolute path of gem source code and in next step simply open source code using text editor like this
subl <gem-code-absolute-path>
For Example
Let's assume you want to read kaminari gem code
bundle show kaminari
/home/abdullah/.rvm/gems/ruby-2.3.0#your_gem_name/gems/kaminari-0.16.3
next step (subl is command to open with Sublime Text Editor)
subl /home/abdullah/.rvm/gems/ruby-2.3.0#your_gem_name/gems/kaminari-0.16.3
Run gem environment - this will display you all the information about your gems, including their location.
Additionally I would advise you to install some IDE with go to source feature - RubyMine is just brilliant (and has 30-day-long free trial), if you want to go for absolutely free go with NetBeans together with Ruby plugin. This feature allows you to navigate quickly to source of clicked method, regardless whether it is defined inside your code or inside the gem.
Clone the github repo in your local machine and explore it using your prefered editor:
git clone https://github.com/plataformatec/devise.git

Optimal Way to Browse Gem Source Code

I find that browsing source code of open source libraries being used in projects is invaluable. Unfortunately, I find this difficult when developing with ruby on rails. I am accustomed to working inside IDE's that allow me to jump to the definitions of symbols, regardless of whether they are part of an external library.
I use Aptana Studio 3 on Mac OS X to develop, but would be willing to change IDEs for this feature.
I have explored https://github.com/fnando/gem-open as an option, but have yet to find a good editor to integrate this with. Can anyone recommend one? Anyone else have a good method for browsing the sources of gems?
Aptana Studio 3 has a command line launcher: studio3.
Add the following to your ~/.bash_profile:
export PATH="/Applications/Aptana Studio 3:$PATH"
export GEM_EDITOR="studio3"
Reload your existing shell environment: . ~/.bash_profile and then you can use gem-open with your preferred editor: gem open rails

What is wrong with this rails template?

I am attempting to take a whack at creating my first Rails application template and I am running into a slight issue with the copy_file method.
First some background.... Apparently the Ruby OpenSSL package does not ship with a CA store, so any attempt to connect to an HTTPS service will fail out of the box. The way around this(for Rails 3 apps) is to add the line OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE to the top of your config/environment.rb file. I need to do this on the fly in my template so I can install jQuery.
So I have that all figured out, my general thought is to:
Make a backup of my config/environment.rb file.
Prepend the data to original
Run the jquery:install --ui task
Restore the original config/environment.rb file.
See my template Gist, Lines 25..34 is the relevant section.
So all of that works until step #4 which fails with Error: Could not find "env.orig" in any of your source paths on line #31.
This is VERY perplexing to me because line #28 works, I can see the env.orig file on disk, so why won't the reverse work?
What am I doing wrong?
Update 1:
After looking at the Thor source thor\actions.rb it became clear that Thor uses different paths (not your current project path) for the source and destination. Furthermore my copy was actually not working, it was actually coping the ERB template file, not the already generated file.
After a breather it occurred to me use the right tool for the job so now I have: run 'cp environment.rb environment.~' and run 'mv environment.~ environment.rb' which works just fine. I am fairly certain this would not work on a windows box without the unix tools installed, but I can live with that. Does anyone have a better way?
See my Update for a Why, but the solution was to use the right tool for the job so now I have: run 'cp environment.rb environment.~' and run 'mv environment.~ environment.rb' which works just fine. I am fairly certain this would not work on a windows box without the unix tools installed, but I can live with that.

Ruby on Rails -- new install on windows 7 but cant run ruby commands, problem with path?

I just installed ruby on rails on my new computer ( I was using instantrails before) and I'm trying to get everything setup.
Im running Windows 7. So I followed the instructions from this tutorial.
http://blogupstairs.com/ruby-on-rails/installing-ruby-on-rails-on-windows-7/
The problem i'm guessing is step 3
"Add the newly installed bin directory to your path in your windows system : Open windows explorer-> right click the icon computer-> choose Properties -> in the contol panel Home, Click Advanced system settings and then click Envitonment Variables button->in the system variables click new and add new system variables like this : Variable name : RUBY_HOME , Variable Value : C:\Ruby, after that add it in to the path and add the bin after a semicolon to the “Path” variable like this : C:\Program Files\Common Files\Adobe\AGL;%JAVA_HOME%\bin;%ANDROID_HOME%\tools;%RUBY_HOME%\bin"
I set created the system variables but when I try to run script/generate in the main directory of my app I get the error
"Ruby: no such file or directory --script/generate"
I checked the apps directory and the script folder is in there. How can I run the "ruby script/generate" commands from the control panel?
I was guessing it was something with the path but I dont know any ways to check to find out whats going wrong.
Are you using Rails 3.0+? In this version of rails and up, the script/* folder has been deprecated in favor of rails generate, rails server, etc. See here.
In 99.99999% of all cases, if a computer tells you that it cannot find a file at a specific location, it is because that file is not at that specific location.
So, is there actually a file named generate in a directory named script in the current directory?
I have recently installed RoR on my "clean" Win7 as well.
What IDE are you using?
I suggest trying JRuby with NetBeans/RubyMine:
JRuby installer does all the work (even adds the proper variables to your path in system environment settings...,
IDEs take care of gems, setting up servers etc.
As for the commands, as Nuclearsandwitch mentioned, there is no script/generate or script/server in Rails 3. Just make sure you in the directory with your Rails app and then try running rails server. It should work :-)

Resources