Devise is such a beautiful library.I don’t see any reason to write my own authentication component on rails.Moreover Devise allows you to custom every single feature of the library.

Last week I was tasked with creating a registration page for a startup, to make the sign up process simpler we would allow the user to create a company in the same form.Devise made this task very simple.By default devise creates routes for user sign up,but this route would conflict if you have a user resource route.Here is how you can customize the url to use the right controller and route url.
The path_names property sets the registration url to ‘/users/register’ and since we want to create the company relation we use a custom registration controller.
The registration controller’s new method overrides the Devise::RegistrationController.

build_resource({}) and set_minimum_password_length enforces the minimum password length and also creates the resource which in this case is the user.
We also create a model for the company.

The method create on the other hand just takes the params and generates a user model from it.

The method sign_up_params is overridden to allow the company attributes to be used as rails model attributes More info.

The view on the other hand is also simple.Just add the relations and devise takes care of the rest.

As you can see the company form field is generated similarly to rails form.



Postgres is a really powerful database with a lot of features, One of the most important features which is very rarely found in other databases is “Table inheritance”

Table inheritance is very similar to class inheritance of OOP.In Table inheritance you have a parent table which is inherited by another table with extra columns in it.

By default the pg gem doesnt support table inheritance.

In the current project that I am working on, the business logic required the user to have template using which he could create items.This is where table inheritance shines.It allowed me to create the template model which contained all the generic attributes for the item.So everytime I would update the template the relevant model for the template would also be updated.
Continue reading



error
Most of us have seen this dreaded error page when developing on rails.Launching up irb or rails console wastes a lot of time and brain cycles.
Having said that I came across this neat gem called ‘web-console’.This gem is really handy ,it launches rails console right there is the browser along with the current context.
Installing it is pretty simple Add it to your Gemfile and you are up and running.Make sure you have the version mentioned below or something higher.

gem 'web-console', '2.0.0.beta2'

 

web-console



Continuing with previous post,this post will cover whats new in Rails 4.1 .
Most of my information comes from the blog post on the rails blog.This is my take on the new features and will mostly cover the ones useful for me in my day to day development.

To speed up our development start time and test ,rails preloader has now been replaced with spring, which works just like spork.It creates a separate process which loads the rails environment in memory thereby reducing time for running our test as rake reloads the rails environment every time we run a rake task.

I have been using the env gem to store my secret_keys for apis like facebook,aws..etc.Rails now has the secrets.yml in the config folder.We can now store all the configuration keys in the secret.yml file.We can retrieve the key using the Rails.application.secrets.{secret_name}.

ActiveRecord Enums is another important feature.Even though it is something trivial to implement.Rails makes it easier to store stat of a particular model to a particular integer.Plus it creates many handy methods which is provides to query.

 

There are many more changes ,to learn about them visit this link.

 



You must be wondering why I am writing a blog about Rails 4 even thought it came out nearly 6 months ago.This is mainly because I found some new features and missed some old ones.

One of the most obvious upgrades in rails 4 is that rails now requires ruby 1.9.3+.Which is really great as it helped clean up the core of rails.But as a developer the first change I had to make to my code Strong Parameters.

  • Strong Parameters re-enforces security in by only permitting certain variables through the params field.
  • Turbolinks:Turbolinks is a feature on rails which makes use of javascript to speed up the loading time of the website.The way it works is during the first load of the website.It loads the initial page and takes control over the links.Every time on would click on the link it would make a ajax call in the background and replace the content in the body of the html page.Thereby reducing the requests made to the server and making the website much snappy.This sounds good in theory but in practice this breaks a lot of websites as it has it own onclick and onload events which could break old code.
  • Postgres HStore:Using hstore in postgres has become even easier with the native extension for hstore data.We start with enabling the extenstion in the migration.


    We create the attr table in the images table as hstore datatype.

  • ActiveModel is another important feature in the rails where you can use regular models in rails without the persistence layer.We can even use validations just like a ActiveRecord::Base model.

 



With one of our products in our company we were given the task of building a file uploading system which would take different type of files and upload it to our server.Instead of creating a separate class for each file and having different methods to upload the file, we instead opted for a creating a polymorphic class which can be used with different classes with the has_many relation.

First we begin by creating a migration with the polymorphic relations in the database

rails g migration CreateAssetsTable

We then fill in the migration

class CreateAssetsTable < ActiveRecord::Migration
  def change
    create_table :assets do |t|
      t.string :type
      t.references :attachable, polymorphic: true
      t.timestamps
    end
  end
end

We then run the migration

rake db:migrate

This sets up the assets table with 6 columns.Id,Type,attachable_id,attachable_type,created_at and updated_at.The important columns for the polymorphic relation are the attachable_type and attachable_id.This sets up the relation between other classes and the asset class at the database level.
Next we create the model for this class

rails g model asset

This will create the model class and set us up with an empty model class.We then will in the polymorphic relation.

class Asset < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
end

Just like in the migration the polymorphic relation is the most important feature of this class.

Now that we have the asset class ready we then create other class which will use this relation.
Example:

class Users < ActiveRecord::Base
   has_many :avatars, as: :attachable, class_name: "Asset"
end

The has many sets up the has_many relation with the user model.So now the user has many avatars which refers to the assets model for the asset with type avatar.
Now that we have dealt with the polymorphic nature of the classes,It becomes very easy to add the upload module to the asset class using the paperclip gem by thoughtbot.
We add the gem to the gem file

gem 'paperclip'
bundle install -j4

Next we generate migration for the assets table to hold your new data

class AddAttachmentToAsset < ActiveRecord::Base
  def up
    add_attachment :assets, :file
  end
  def down
    remove_attachment :assets, :file
  end
end
rake db:migrate

We the add the has_attached_file to the Asset class

class Asset < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
  has_attached_file :file
end

Since version 4 of paperclip content validation is required by default.
So we also add a simple image file validation using regex

   validates_attachment_content_type :file, content_type: /Aimage/.*z/

This check if the mime type of the file contains image/ followed by the format.
Now we have a generic file upload class.



Writing a rake task in rails i pretty simple.In this post i will describe creating a rake task to reset the password of every user on the system.Maybe you had a breach and to prevent identity theft you reset the users password and send it to the user through his email address.

To create a rake task we firstly have to namespace it so that it doesnt conflict with existing rake tasks and it also helps us keep our code more modularized.

/// lib/tasks/defcon.rake
require 'securerandom'
namespace :defcon do
  desc 'Reset users emails'
  task :got_hacked do
    begin
      User.all.each do |user|
        password = SecureRandom.hex.slice(0,10)
        user.password = password
        user.save
        UserMailer.reset_password(user,password)
      end
    rescue Exception => e
      puts "#{e.message} occured"
    end
  end
end

We first require the securerandom library.This is required to generate some random password for each user.
We namespace the task with a name,for now i have called defcon.
The task name here is got_hacked which runs once the we type rake defcon.got_hacked so if in case we get hacked we can instantly reset the password for all the users in our system.
Inside the task we have begin and rescue block,this is make sure that we catch any exception that occurs and output it on the screen.
So the regular flow of the program is as such.We loop through all the users in the system (assuming there is a user model).We generate a random password and set that as the password for the current user in the loop.We save the password in the database and then send them a email stating that their password has been reset.
The UserMailer class for sending the email is given below

/// app/mailer/user_mailer.rb
class UserMailer < ActionMailer::Base
  def reset_password(user,password)
    mail(to: user.email, subject: "We got hacked", from: "xxx@xxxx.com")
  end
end

The view for the email

/// app/view/user_mailer/reset_password.html.erb
Dear <%= user.name %>,
We have been hacked.Here is you new password for the account.
password : <%= user.password %>