Category Archives: ruby

This guide contains the general guidelines to follow for making the upgrade easier.

 Introduction

Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade.The best way to be sure that your application still works after upgrading is to have good test coverage before you start the process.

If you don’t have automated tests, you’ll need to spend time manually exercising all the parts that have changed. Do yourself a favour and make sure your test coverage is good before you start an upgrade.

Rails generally stays close to the latest released Ruby version when it’s released:

  • Rails 3 and above require Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially. You should upgrade as early as possible.
  • Rails 3.2.x is the last branch to support Ruby 1.8.7.
  • Rails 4 prefers Ruby 2.0 and requires 1.9.3 or newer.

The official Rails guide for upgrading from Rails 3.2 to 4.0 is very thorough. If your application is currently on any version of Rails older than 3.2.x, you should upgrade to Rails 3.2 before attempting one to Rails 4.0. However, if you’re using any non-standard gems, you’re mostly on your own. Some gems stopped being maintained before Rails 4 was released.

There is a gem “rails4_upgrade” which you can add to your Rails 3 project. It includes a Rake task that will help you with the upgrade. After adding the gem to your Gemfile run “bundle install”.

Step1. In your project root directory run the command “rake rails4:check”. Now instead of going through your currently bundled gems or Gemfile.lock manually, you get a report of what gems you need to upgrade.

Sample Output: 

Step2. Update the Gemfile with the newer version of Rails.

Step3. After resolving all the gem dependencies based on Step1 run “bundle install”. To find outdated gems there’s also “bundle outdated”, which you can run on any project.

Step4. Lastly, there’s the “rails:update” task, which you can use as a guideline as explained very clearly in this post to get rid of unnecessary code or monkey-patches, especially if your Rails 3 app was previously running on Rails 2

Step5. After everything done run the test cases and fix them as well.

Thank you for reading this article!

Here are some of the new feature introduced through Ruby 2.3:

Safe Navigation (&.):

A new operator (&.) has been introduced. It can be very useful in cases where you need to check if an object is nil before calling a method on it. It will return nil if the object equals nil, otherwise it calls the method on the object.

Immutable string:

Strings were mutable by default in Ruby. Immutable strings gives us improved performance because Ruby now has to allocate fewer objects. Ruby 2.3 allows you to optionally make all strings literals frozen by default. You can enable this by adding a comment frozen_string_literal: true at the start of the file.

squiggly heredoc(<<~):

Heredocs let us span strings over multiple lines

ActiveSupport gave us strip_heredoc, but with a seperate library (or Rails)

But now Ruby gives us THE SQUIGGLY HEREDOC

Enumerable#grep_v:

The grep_v method is equivalent to the -v option in the command line grep utility. It returns the list of items that do not match the condition.

Dig:

Dig method simplified accessing nested element in an array or a hash.

Array#dig 

Hashe#dig

Suggestions

When you get a NoMethodError because of a typo in the method name, Ruby now helpfully suggests other method names similar to that one.

Hash “comparison”

Now we can compare 2 hashes. If you see a >= b, it is checking if all the key-value pairs in b are also present in a.

In the first example above, the key-value pair [:x, 1] in the RHS is a subset of those in the LHS – [ [:x, 1], [:y, 2] ], so it returns true.

Hash#to_proc

Hash#to_proc returns a lambda that maps the key with the value. When you call the lambda with a key, it returns the corresponding value from the hash.

Hash#fetch_values:

This method works like Hash#values_at – it fetches the values corresponding to the list of keys we pass in. The difference is that #values_at returns nil when the key doesn’t exist, while #fetch_values raises a KeyError for keys that aren’t present.

 

Overview

We will be setting up a Ruby on Rails development environment on Ubuntu 14.04.

The reason we’re going to be using Ubuntu is because the majority of code you write will run on a Linux server. Ubuntu is one of the easiest Linux distributions to use with lots of documentation so it’s a great one to start with.

Note: Before going to start installation please make sure that you have root privileges or not.

Installing Ruby Dependencies:The first step is to install some dependencies for Ruby.

   sudo apt-get update
   sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev     libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
  

Next we’re going to be installing Ruby using rvm. You can install from source as well.

Install Ruby Using RVM:

    sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    curl -sSL https://get.rvm.io | bash -s stable
    source ~/.rvm/scripts/rvm
    rvm install 2.2.3
    rvm use 2.2.3 --default
    ruby -v
   

Installing Rails Dependencies:Since Rails ships with many dependencies, we’re going to need to install a Javascript runtime like NodeJS. This lets you use Coffeescript and the Asset Pipeline in Rails which combines and minifies your javascript to provide a faster production environment.

    curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
    sudo apt-get install -y nodejs
   

We can use various Rails versions with each Ruby by creating gemsets and then installing Rails within those using the normal gem commands:

    rvm gemset create starterapp
    rvm 2.2.3@starterapp
    gem install rails -v 4.2.4
   
  • “rvm gemset create starterapp” command is to create a gemset name called starterapp.
  • “rvm 2.2.3@starterapp” command is to specify Ruby version and our new gemset.
  • “gem install rails -v 4.2.4” command is to install specific Rails version.

Now that you’ve installed Rails, you can run the rails -v command to make sure you have everything installed correctly:

     rails -v
     # Rails 4.2.4
   

The last step is to install Bundler.

     gem install bundler
    

Configuring Git:(If you already installed git please ignore this step)
We’ll be using Git for our version control system so we’re going to set it up to match our Github account. If you don’t already have a Github account, make sure to register. It will come in handy for the future.

Replace my name and email address in the following steps with the ones you used for your Github account.

    git config --global color.ui true
    git config --global user.name "YOUR NAME"
    git config --global user.email "YOUR@EMAIL.com"
    ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"
  

The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here

    cat ~/.ssh/id_rsa.pub
  

Once you’ve done this, you can check and see if it worked:

    ssh -T git@github.com
  

You should get a message like this:

    Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.
  

Setting Up MySQL:

Rails ships with sqlite3 as the default database. Chances are you won’t want to use it because it’s stored as a simple file on disk. You’ll probably want something more robust like MySQL or PostgreSQL.If you’re coming from PHP, you may already be familiar with MySQL.

You can install MySQL server and client from the packages in the Ubuntu repository. As part of the installation process, you’ll set the password for the root user. This information will go into your Rails app’s database.yml file in the future.

    sudo apt-get install mysql-server mysql-client libmysqlclient-dev
  

Installing the libmysqlclient-dev gives you the necessary files to compile the mysql2 gem which is what Rails will use to connect to MySQL when you setup your Rails app.

Setting Up PostgreSQL:For PostgreSQL, we’re going to add a new repository to easily install a recent version of Postgres.

    sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
    wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
    sudo apt-get update
    sudo apt-get install postgresql-common
    sudo apt-get install postgresql-9.5 libpq-dev
  

The postgres installation doesn’t setup a user for you, so you’ll need to follow these steps to create a user with permission to create databases. Feel free to replace vijay with your username.

    sudo -u postgres createuser vijay -s

    # If you would like to set a password for the user, you can do the following
    sudo -u postgres psql
    postgres=# \password vijay
  

Final Steps: Let’s create your first Rails application:

    #### If you want to use SQLite (not recommended)
    rails new myapp

    #### If you want to use MySQL
    rails new myapp -d mysql

    #### If you want to use Postgres
    # Note that this will expect a postgres user with the same username
    # as your app, you may need to edit config/database.yml to match the
    # user you created earlier
    rails new myapp -d postgresql

    # Move into the application directory
    cd myapp

    # If you setup MySQL or Postgres with a username/password, modify the
    # config/database.yml file to contain the username/password that you specified

    # Create the database
    rake db:create

    rails server
  

Note: Sentence start’s with ‘#’ are comments not commands.

You can now visit http://localhost:3000 to view your new website. Now that you’ve got your machine setup, it’s time to start building some Rails applications.

If you received an error that said Access denied for user ‘root’@’localhost’ (using password: NO) then you need to update your config/database.yml file to match the database username and password.

Reference: gorails

Thanks for reading this Article. If you have any questions, feel free to post your comments and we’ll get back to you.

User registration and social logins are extremely vital for any web application. It is the first step towards building any app, as the most basic functionality on any app would be a ‘user registration’ or logging in using ‘social accounts’. But, building an app with even these features takes a lot of time. I’m afraid, at least a week. So, we found out a solution for this. Our hosted API service lets you build an app with creating and managing user accounts along with a social login registration.

In this blog post, we’ll walk you through a tutorial where you can learn how to build a Ruby on Rails app with our Rest API with basic functionalities like login/registration, edit profile and more. And the best part? It takes just 15 minutes only.

You can click here to know more about our API.

To interact with the NBOS API Server, I have created a Ruby gem called “wavelabs_client_api“. The source code and for an easy reference, the documentation of this gem are available here. This gem is published in rubygems.org under the MIT license.

Prerequisites
1.Install Ruby Version Manager(RVM).
2.Install the Ruby 2.2.3 & Rails 4.2.4 using RVM.

Detailed installation instructions are available here

Note: No database is required because our API server has got it covered.

Now, let us create the web application. Open a console and follow the instructions:.

 $>git clone https://github.com/nbostech/wavelabs-rails-client-api.git
 $>cd wavelabs-rails-client-api
 $>git checkout -b release-1 v0.1
 $>bundle install
 $>rails server

Open the browser and access your application.The home page would look like this.

starter_app_home_page

Now you can use the following functionalities using the above application:
1. Signup
2. Login and Logout
3. Edit Profile Information
4. Change Password
5. Upload Profile Picture
6. Login with Facebook
7. Login with Github
8. Login with LinkedIn
9. Login with Google plus
10. Login with Instagram

The live application is available at Heroku Server

And there, you are all set to go. You haven’t just built a Ruby on Rails with basic login and registration functionality within 15 minutes, you are also all set to launch it into the market.

If you have any questions, feel free to post your comments and we’ll get back to you.