Setting up RubyOnRails environment on Ubuntu 14.04

This tutorial is about setting up environment for RubyOnRails on Ubuntu 14.04.

First of all, update & upgrade your system:

sudo apt-get update

^
sudo apt-get upgrade
^

Install git and curl:

sudo apt-get install curl

^
sudo apt-get install git-core
^

Configure git:

git config --global user.name "Your Name"

^
git config --global user.email email@example.com
^

Install RVM:

curl -sSL https://get.rvm.io | bash -s stable

^
source ~/.rvm/scripts/rvm
^
Test RVM installation correctness:

type rv...

ActiveRecord models localization

Suppose we have some model and we want to localize it, first of all we need to now i18n_key for that model. Open rails console rails c, and run:

OurModelName.model_name.i18n_key

For example let's do that for model, defined in public_activity gem:

PublicActivity::Activity.model_name.i18n_key
#=> :"public_activity/activity"

Now, we cat easily localize it, by adding this in .yaml file:

activerecord:
  models:
    public_activity/activity: 'Our localized model name'

In the same way we can locali...

Capistrano deploy.rb examples

All examples here for deploy RoR application with unicorn, staging machine (where we deploys) available via ssh by ssh_key.

  1. Deploy via remote_cache:

     require 'rvm/capistrano'
     require 'bundler/capistrano'
    
     set :application, '_proj_'
     set :rails_env, 'production'
     set :domain, '_user@your_deploy_domain_'
     set :deploy_to, "_path_to_#{application}"
     set :use_sudo, false
     set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
     set :unicorn_pid, "#{deploy_to}/shared/pids/u...
    

Setup postgresql for RoR

Assume your database.yml file looks like this:

{: .yaml}
development:
adapter: postgresql
host: localhost
encoding: unicode
database: proj_development
pool: 5
username: proj
password:
template: template0

test:
  adapter:  postgresql
  host:     localhost
  encoding: unicode
  database: proj_test
  pool:     5
  username: proj
  password:
  template: template0

production:
  adapter:  postgresql
  host:     localhost
  encoding:...

Ubuntu postgresql 9.3 installation for RoR

Add repository and install Postgresql:

^
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.3 libpq-dev
^

Then make yourself a Postgresql superuser:

sudo su postgres

^
createuser your_system_login
^
psql
^
ALTER ROLE your_system_login WITH SUPERUS...

Rails migration add float point field with scale and precision

class CreateFakes < ActiveRecord::Migration
  def change
    create_table :fakes do |t|
      t.decimal :float_value, :precision => 4, :scale => 3
    end
  end
end

This will allow you to have 3 digits after the decimal point and 4 digits max.

Rspec + Capybara + Rails4 + Spork (intergation tests setup with selenium and poltergeist)

Add in Gemfile:

group :development, :test do
  gem 'rspec-rails'
  gem 'spork-rails'
end

group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'email_spec'
  gem 'poltergeist'
  gem 'launchy'
  gem 'selenium-webdriver'
end

Run bundle install

Ensure your spec_helper.rb looks similar with this:

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
# require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to ...

Javascript with Turbolinks in Rails 4

If some of your scripts don't work with turbolinks, you should do the following:

ready = ->
  #your code here
  
$(document).ready(ready)
$(document).on('page:load', ready)