Table of Content.

00:10 – Generate a model with Run Anything.
01:30 – Code autocompletion.
02:35 – Go to definition.
04:06 – Quick documentation.
04:36 – Postfix code completion.
05:22 – RuboCop integration & Intention actions (Quick-fixes).
08:20 – Show Recent/Recently Edited Files.
09:04 – RegExp checker.
09:58 – File Structure Popup.
10:39 – Code formatter & Code style options.
12:10 – Go to related symbol (related Rails entity).
12:40 – Navigate between methods in the editor.
12:51 – Refactoring: Extract Method.
14:15 – Refactoring: Rename.
15:50 – Multiple cursors.

Generate a model with Run Anything.

Now we'll create a new model and add some content to it to show you different code editing abilities of RubyMine. To start off, I'll double press Control to open the Run Anything action. This action can be used to run any configurations, script, command, Rake tasks, Rails generators, and even console commands.

I can just start by typing something like “rails g model”, or instead I could just insert “Model”, and RubyMine will understand that I'm looking for a model generator and suggest it in the completion list.

I'll press Enter and see the UI wrapper for the model generator. The same one you will discover for the controller, migration, and other generators.

Here I can provide the name of my model, which will be Post, and its fields: content: text (now, as you can see, RubyMine provides code data completion for field types). The other fields will be “picture: string”. I can also provide additional options. I'll skip and hit OK.

The IDE will run the generator, open the Run tool window, show me the result of my command, and allow me to navigate every created file. It will also open the migration file, and the Post Model in the editor by default.

Let me close the Run tool window and now I'm ready to add some content to my newly created model. Let's now add a couple of associations. First of all, let's make our Post belong to user through the “belongs_to” association.

Code autocompletion.

As RubyMine indexes your application on each start-up, it later provides you with a number of Code Insight features, like code autocompletion for classes, methods, and other entities declared in your application or in Ruby, Rails, and other gems.

So, this is how you get the autocompletion for “belongs_to” in particular. Let's autocomplete it. RubyMine is also smart enough to understand that we are looking for a model here, and indeed we're looking for the user model, so let's autocomplete it as well.

The sample app uses the gem carrierwave to upload pictures. And to associate pictures with our model, we'll need to use a custom method from this gem called “mount_uploader”. And as you can see, as this gem is installed for our application, RubyMine suggests to autocomplete this method as well.

Code autocompletion is not the only Code Insight feature of RubyMine. One of the most important abilities that RubyMine has is the ability to navigate to the definition of any entity declared inside or outside of your application. For example, we can quickly navigate to the definition of the user model. To do that, we'll hover over the name of the model and press Control, or Command depending on your operating system, and click on it.

Okay, now we are moved to the model and we can add an association here as well. Similarly, we can navigate to the definition of the “belongs_to” association. So now we are moved to the active record gem, file associations.rb. Let me show you that in the Project tool window.

So, we're outside of our application and the external libraries, gem active record, file associations.rb. And similarly we can navigate to this method, which is the gem carrierwave, or the file activerecord.rb, and so on and so forth.

Let's now add a method that will check the size of our picture and provide an error if the picture is oversized. We'll call it picture_size: “picture.size > 5.megabytes”. Now it seems that I have two similar looking methods here. What if I'm not sure which one I should use? Instead of googling for each of these methods or checking the documentation, I can press F1 and see quick docs for each of these methods right in the IDE.

So, the method megabytes returns the number of bytes equivalent to the megabytes provided and this is the method I'm looking for. And the second one is just an alias for the first one, so I can use any of these.

Postfix code completion.

Now, it also seems that I forgot to make it an if-statement, but instead of going back in code, I'll just insert dot (.) and type if which will allow me to wrap this line with an if-statement like this: “if picture.size> 5.megabytes”. This feature is called “postfix code completion” and it allows you to supplement, alter, or invert your statements without going back in code.

You can see the list of all postfix code templates in the settings, both with JavaScript and with Ruby. You can also discover what they will look like, and enable and disable the ones you want.

RuboCop integration & Intention actions (Quick-fixes).

Okay, let's now just finish the method. Let's now re-enable RuboCop to see if we have any code style violations in our file. Apparently, we have a couple of issues. To see an error description in every line, you can go to the right part of the editor and navigate to every highlight. You can also hover over the highlighted text and see the error description there.

Finally, if you don't want to use the mouse, you can just press F2 to go to every next error in the file, and then just press Command+F1 or Control+F1 to see the error description.

In this particular case, the problem is that I'm using double-quoted strings instead of single-quoted strings. And to fix that with RuboCop, I can press Alt+Enter and fix all RuboCop offenses in the file or fix them by type of offense or by cop department. Let's fix all RuboCop issues in this file.

Great. This problem disappears but we still have a couple of issues left. First of all, here we have a multi-line statement instead of a one-liner. Unfortunately, RuboCop doesn't provide a quick fix for that, but RubyMine does.

To access it, let's press Alt +Enter again. And as you can see, we can convert the statement to modifier, meaning that we can make this if-statement a one-line statement. Let's press Enter, and as you can see, this error disappears.

Still, we have a couple of irritating issues according to RuboCop. First of all, this line is now too long. Another problem is that we don’t have a documentation comment for our class. To get rid of these issues, let's create an additional RuboCop configuration file.

To do that, we'll go to the root of our project and create a RuboCop configuration file, .rubocop.yml. RubyMine will ask us whether we want to add the new file to Git. We'll press “Yes” for now, but we'll discover VCS features a little bit later.

Let's add additional rules to get rid of the irritating issues. Let's get back to the model. I'll manually save the file to apply the changes faster, and as you can see, RubyMine respects the RuboCop configuration file so these errors disappear.

Let's see what else RubyMine can do for us. You've probably noticed, by the way, that somehow I managed to close all the files but not the one that's currently open in the editor. To do that, hover over the tab, hold Alt, and click the Close button. And now you've got rid of all the files but not the one that is currently open in the editor, but I'll close this file for now as well because I don't need it anymore. And I'll close the Project tool window as well.

Show Recent/Recently Edited Files.

Now, I want to navigate to my user model. To do that, I can use another handy pop-up, which is called Recent Files. That's Command+E or Control+E, depending on your operating system. This one allows you to navigate to recently opened files. As you’d expect, the search works here as well so I can do something like this but the result is a similar pop-up which is called Recently Edited Files, and as you remember, we did edit our user model before, as we added an association there. So, we can open it from here.

RegExp checker.

Let's polish some stuff at first. Here we have some RuboCop issues we can get rid of. And here is another good case for using Intention Actions in RubyMine. We have a regular expression here that checks if a user provides a valid email address. So, to check this regular expression, we can press Alt+Enter again and choose Check RegExp, and provide some samples. This one doesn't match. This one doesn't match either. How about something like name@email.com? Okay. This one matches. Great. So, this is how it works.

Here we can also replace the class name with self and also let's get rid of this ternary operator here that should have been used for multi-line statements. So, let's make it an if/end-statement.

File Structure Popup.

So, we have a somewhat thick model here, and to navigate between its methods faster, we could use the File Structure pop up. This pop-up allows you to navigate between different methods in a file, and the search works here as well. So, we can try and go to some of our private methods like this.

As you can see, we have extra indentation for our private methods, and, by default, RuboCop considers this a code style violation, so I've edited my configuration file to get rid of this offense. The reason I did that is that many developers actually prefer to add extra indentation for their private and protected methods.

Code formatter & Code style options.

As for the code style, RubyMine provides a built-in code formatter which reformats your code according to the Ruby Style Guide and according to some options that you can tweak in the settings.

To reformat your code, you can go to Code | Reformat Code or use a shortcut. And as you can see, RubyMine has reformatted our private methods, and made them in line with the rest of the methods.

By the way, if you only choose this particular method and press the Reformat action again, RubyMine will only reformat this particular selection.

The reason RubyMine indents our private methods is that that's what is supposed to do now, according to the settings. So, if you go to the settings, look for the Code Style. We need one for the Ruby language. However, the result is a number of code style options for different languages and technologies. But here in the Ruby section, as you can see, we can choose whether we want to add extra indentation for our private and protected methods, and the Preview pane also shows you what's going to be changed.

There are other code style options as well, for example, you can add spaces around hash rockets, curly braces in blocks, and hashes, and some other things as well.

Let's hit Apply and OK. Let's reformat our code again, and, as you can see, now RubyMine adds extra indentation for our private methods. So this is how the code formatter works in RubyMine.

Go to related symbol (related Rails entity).

Let's now take a look at some other user-related things. To do that, we can go to Navigate | Related Symbol..., which will open up a pop-up that will show all the user-related entities. So there's a users Controller, a Helper, a number of views, and tests. Let's navigate to the Controller.

Navigate between methods in the editor.

Apparently, we have a number of RuboCop issues here as usual. Let's fix all of them at once. We have a number of Rails actions here. By the way, to navigate between different actions and methods, you're going to use Control+⇧ and Control+⇩ or Alt+⇧ and Alt+⇩ depending on the operating system.

Refactoring: Extract Method.

So, it looks like often we do the same thing for different actions. We find a user in the database by its ID, and we assign it to an instance variable user. Instead of that, why don't we just refactor this piece of code to its own method.

To do that, we'll go to the Refactor menu and choose the Refactor pop-up. As you can see, RubyMine provides a number of different refactorings. For example, you can extract variables, constants, fields. All of these actions have their own shortcuts. We'll go with the Extract Method refactoring. Let's click on it.

We can make the new method a private one, a public one, or a protected one. Let's make it private. Let's also provide a name for it, get_user. Click OK.

RubyMine will find all the actions that use the same piece of code we've just refactored to its own method. If we click “Yes”, we can apply the new method for each action one by one, or for all of them at once.

So, our new method is located right in the private section. Here it is and all the Rails actions use the new method instead of the previous code fragment. However, we still have a small problem here. Apparently, we shouldn't prefix reader methods with get as it's a code style violation, so we'll need to rename it to something else.

Refactoring: Rename.

We don't have to manually do that as RubyMine has a refactoring for that as well, which is called Rename. So, if we press Shift+F6, we can rename this method to something else, for example, take_user. Hit Refactor. RubyMine will find all the usages and if we hit do refactor, it will just rename the method with all of its usages.

And as you can see, this method is now called take_user in all the Rail actions.

Finally, we probably do not want to use the same method in a number of actions. So, obviously it’s a duplication so all we have to do is to make it a filter, a before filter.

Let's go up here and let's duplicate this line like this, Command+D or Control+D, depending on your operating system. Let's make this method a before filter. Now the code completion is available for take_user, as you can see.

Now I do not remember all the actions that use this method so I can manually find all its usages again. To do that, I'll hit Alt+F7. So as you can see, the following actions use this action: edit, followers, following, show, and update. Great. Let's add them.

Multiple cursors.

Finally, we now can get rid of all the occurrences of take_user as we don't need them anymore, because we now have a before filter. We don't have to do that manually though. All we need to do is select one occurrence of take_user and then press Control+G or Alt+J depending on the operating system.

And then press the shortcut again and again to choose all the occurrences of take_user in the file. However, it seems that we also selected the definition of the method take_user, which we probably do not want to erase. So, we need to unselect it.

To do that, we'll use the same shortcut, but also press Shift. And now all we have to do is just press Command+Backspace to get rid of all the unneeded calls of the take_user method.

Finally, let's press escape to escape from multiple cursors.