8 min read

(For more resources related to this topic, see here.)

Managing your implants (Should know)

Now, we will learn how to utilize RubyMine to manage your Gems and external libraries used for your Ruby on Rails programs.

Getting ready

Start RubyMine and open the HelloWorld project that we created earlier. We will be adding an implant to enhance your assimilation process into the collective.

How to do it…

We will now use a Gem in RubyMine by showing the following simple example using the Prawn Gem:

  1. Right-click on the HelloWorld project folder in the left project panel and add a new file to your project. Name it pdf.rb.
  2. Add the following code to this new file:

    require 'prawn' Prawn::Document.generate("hello.pdf") do |pdf| pdf.font "Courier" pdf.stroke_color = "ff0000" pdf.line_width = 5 pdf.stroke do pdf.circle([300, 300], 100); pdf.circle([350, 320], 20); pdf.circle([260, 325], 20); pdf.curve [270, 250], [340, 250], :bounds => [[270, 270],[275, 220]] end pdf.move_down(300) pdf.font_size = 36 pdf.fill_color "000000" pdf.draw_text("You will be assimilated!", :at => [40, 40]) pdf.line_width = 1 pdf.stroke_color = "0000ff" pdf.move_to([0, 0]) grid = 11 num_lines = 51 size = (num_lines - 1) * grid pdf.stroke do 51.times do |idx| pdf.line([0, idx*grid], [size, idx*grid]) pdf.line([idx*grid, 0], [idx*grid, size]) end end end

  3. Now, right-click on the open source file and select Run pdf. You should get an error similar to in ‘require’: cannot load such file — prawn (LoadError). This means that you do not have the required technology to continue the assimilation process. We need to add some Ruby Gems to allow our program to continue.
  4. Open the Settings panel and select the Ruby SDK and Gems panel from the left-hand list. This is where we also changed the version of Ruby that we are using. This panel will allow us to install some specific Ruby Gems that we need.
  5. Hit the button Install Gems… and you will see a screen like the following:

  6. Start typing the name of the Gem you wish to install, which in this case is prawn and the search will begin immediately. Scroll to the right Gem.
  7. Select the Gem in the list at the left-hand side and then hit the button Install. RubyMine will then run the Gem system and install the Gem and all of its appropriate dependencies into your system.
  8. When it is complete, select the prawn gem in the list on the right and you will see the description panel filled with various aspects of the gem for your browsing pleasure.
  9. Once completed, go back and re-run the pdf.rb program.
  10. Since this program actually generates a PDF file, we need to find where it saved the file. In the project window on the left, hit the icon that looks like the following:

    This will synchronize the files inside the folder with the project list. You should now be able to see the file hello.pdf in the project window.

  11. Double-click on this and you will see this file in whichever application you have on your computer that displays PDF files. You should see something like the following:

How it works

The code that we typed in is a simple use of the Prawn Gem. It first requires the Gem code and then starts a block of code that will begin generating the hello.pdf file on the disk. Each command then sets properties of the text, size, colors, circles, and finally draws a grid of lines on the page.

Creating your first progeny (Should know)

Now it is time to create and run a simple Ruby on Rails application using RubyMine exclusively.

Getting ready

Open RubyMine and navigate to File | New Project. From this window, you can now select what type of project to begin with. RubyMine gives you several options that you can use later. Right now, select Rails application, as shown in the following screenshot:

Hit OK and you will see the next settings window which allows you to select which version of Rails you would like to use in your project, along with the JavaScript library and database configurations.

Select the checkbox for Preconfigure for selected database and choose the sqlite3 option, as shown in the following screenshot. Leave the rest as default and hit OK.

How to do it…

Now that you have created a new Rails project, RubyMine takes over and starts using Rails to generate all of the files and configuration necessary for a complete project, including running the command bundle install at the end. This will make sure that you have all the proper Gems installed for your Rails project.

Now, we can run what we have and see that everything is installed correctly, using the following steps:

  1. At the top of the window, select the green arrow to run the Development: Progeny configuration. Running, in this case, means that it will start the default Rails webserver, Webrick, and begin listening on port 3000 for web browser requests.
  2. Once it is running, open your favorite browser and go to the address http://localhost:3000, and you should see the opening Rails Welcome Aboard screen.
  3. Click on the link About your application’s environment, you can see some details about your installation including the various version numbers of Rails and Ruby, and the database that your application will be using.

Now, we can build some features for our app. Let’s begin by creating an interface to our database of species that we have already assimilated. For this, we can use the Rails generators to build the scaffolding of our application that gives us something to build on.

All of the functionalities of Rails can be accessed from within the RubyMine environment, as shown in the following steps, so it is not necessary to go out to a command window at all:

  1. To run the generators, we just need to navigate to Tools | Run Rails Generator.
  2. As you type, the list is filtered to match your selection. Select the scaffold generator and hit Return:

    The following window gives us the opportunity to name our table and the various fields that it will contain. The database table will become the name of our model as well.

  3. Type the following into the top text box:

    Species name:string identification:integer assimilated:Boolean

  4. Leave the other settings at their default and hit OK, as shown in the following screenshot:

  5. Now if you reload your browser window, you should see an error like ActiveRecord:: PendingMigrationError.

    Oops! We forgot to do something after creating the code for the database tables. We need to run the migrations that will create the actual tables.

  6. Navigate to Tools | Run Rake Task… and start typing db:migrate. Just like the generator window, the various rake tasks will begin to be filtered by what you type.
  7. Hit Return and then select the defaults in the next window Latest migration and the migrations will be run for you.
  8. Now that the tables are created, point your browser to a new location, http://localhost:3000/species. You will see the index page for your Species database table, similar to the following screenshot:

    Click on the links and add some species to your database. The scaffolding that we generated, produced all of the CRUD (Create Read Update Delete) forms and screens necessary for managing our Species database table, without typing any commands in our terminal or leaving RubyMine at all.

Of course it has no style, but who cares about style? We are the Borg and only care about technology!

Ok. Maybe we can spruce it up a little bit. Lets add a Gem called Bourbon that helps with the following:

  1. Open the Gemfile from the project window and add the following line to it:

    gem 'bourbon'

    Now we need to install the Gem by running bundle install. We can do this directly from RubyMine by navigating to Tools | Bundler | Install.

  2. Hit the Install button on the window that shows and the Gem will be installed correctly.
  3. Now we can edit the CSS file that is in the App/Assets folder called species.css.scss. Open this file and add the following CSS code to the file:

    @import "bourbon"; p { @include linear-gradient(to top, white, steelblue); }

  4. Reload the page that shows one of the species that you created such as http://localhost:3000/species/1, as shown in the following screenshot. Now isn’t that much better?

There’s more…

Once complete, look at the configuration menu and you will notice that you now have some additional commands in this shortcut menu. RubyMine remembers the tasks and commands that you have executed for you to make it more efficient, as you will likely be running these commands often.

Summary

This is a technology that is worthy of assimilating to its fullest extent. In this article we covered Managing your implants and Creating your first progeny, which shows create and run a simple Ruby on Rails application using RubyMine.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here