A simple instruction to Rspec – 3 in Ruby on Rails -4

This is small introduction about rspec version 3 for ruby on rails developers, and also note that this is not mean for techy geeks 🙂

Initial Setup
add gem 'rspec-rails', '~> 3.0.0'

then bundle install

And run rails generator rspec:install

Herein after spec files are automatically created when ever you create model, controller, view files, if you dont want any spec file to be created automatically then
in your application.rb

config.generators do |g|
g.test_framework :rspec,
view_specs: false,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: false
end

put false wherever you dont want to create spec files
RSpec generators can also be run independently. For instance,

rails generate rspec:model post

All specs are created inside spec folder and to run test just type rspec in terminal

If you want to test only a particular file say products.rb in model then

 rspec ../rails_app/spec/models/products.rb

Thats all

Model specs

Examples
require "rails_helper"

describe Post do
it “checks it saves the data” do
expect(Post.count).to eq 0
@post=Post.create
@post.save
expect(Post.count).to eq 1
end
end

controller spec

require 'rails_helper'

RSpec.describe WelcomeController, :type => :controller do
it ‘has 300 status code’ do
get :index
expect(response.status).to eq(200)
end
end

Matchers

rspec-rails offers a number of custom matchers, most of which are
rspec-compatible wrappers for Rails’ assertions.

redirects
expect(response).to redirect_to(path)
templates
expect(response).to render_template(template_name)
assigned objects
expect(assigns(:widget)).to be_a_new(Widget)

Easy way to deploy Rails 4 application in Unicorn with Nginx

In this post I will show you how to host our rails application with unicorn and nginx

The first step is obviously to install nginx for that go to your control terminal then type

sudo apt-get install “nginx”

After the installation go to /etc/nginx/sites_enabled/ – (or wherever you installed nginx)

in this sites_enabled folder you have default.conf file delete that file and create new file called nginx.conf

server
{
listen 80 default;
#server_name example.com;
root /home/ideology/test_server_nginx/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_pass http://localhost:3000;
}

error_page 500 501 502 503 /404.html;
}

save the file with above code

Now In your rails application folder gemfile.rb add gem “unicorn” and run bundle install

Then start your rails application with unicorn with port 3000

    /application_folder/unicorn -p 3000

Then you can seen your application in browser with nginx server at port 80

Thats all! very simple yes?……

To change environment to production

RAILS_ENV=production rake db:migrate

and then in your boot.rb file add this lines

require ‘securerandom’
ENV[‘SECRET_KEY_BASE’] = SecureRandom.hex

then start your rails application unicorn server

RAILS_ENV=production unicorn -p 3000

And thats all your environment is changed to production now ,

But this is just a fast start up only this is not the correct solution to host your application you need additionally unicorn configuration files for further reference http://unicorn.bogomips.org/

Gemmified version of jquery plug-in stick

Sticky is a jQuery plugin that gives you the ability to make any element on your page always stay visible .This is gemmified version of sticky

jQuery sticky 1.0.0 – http://stickyjs.com/

Installation

Add this line to your application’s Gemfile:

gem 'sticky-rails'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install sticky-rails

Usage

Sticky is a jQuery plugin that gives you the ability to make any element on your page always stay visible. , so please check if you are using the jquery on your rails app

//= require jquery

Your app/assets/javascripts/application.js must appear like:

//= require jquery
//= require jquery_ujs
//= require jquery.sticky.js
//= require_tree .

Basic Structure example from http://stickyjs.com/

<script>
  $(document).ready(function(){
    $("#sticker").sticky({topSpacing:0});
  });
</script> 

REMEMBER TO INITIALIZE IT!

If you are going to use coffee script:

$(document).ready ->
  $("sticker").sticky()

Or just javascript:

 $(document).ready(function(){
    $("#sticker").sticky({topSpacing:0});
  });

For more documentation please checkout the sticky-rails 4 repository here: https://github.com/garand/sticky

Gem Author

http://twitter.com/navinspm