Thursday, 18 December 2014

Meteor basic commands

Meteor (Node.js) Basic commands ::

# Create new application
> meteor create app_name

# Start server
> meteor

# Mongo DB from console
> meteor mongo

# Deploying application
> meteor deploy my_app_name.meteor.com

Wednesday, 17 December 2014

Node.js with Meteor

# Installation guide



# node.js & npm installation:

apt-get install python-software-properties apt-add-repository ppa:chris-lea/node.js apt-get update
apt-get install nodejs
apt-get install npm


# Check node.js & npm version:

node -v
npm -v


# Install Meteor:

curl https://install.meteor.com | /bin/sh

# Create a project:

meteor create myapp

Saturday, 11 October 2014

Crop, zoom and rotate image using carrierwave

Crop, zoom and rotate image using carrierwave


We already have a good reading about cropping in rails using jcrop from ryanbates railscast (http://railscasts.com/episodes/182-cropping-images?view=asciicast). But in my recent rails project I need functionality to rotate and crop zoomed image. So I made following changes that might help others.

## My original image :-



First of all we need carrierwave to upload images and mini_magick to manipulate images. So add following gems in your Gemfile.

##/Gemfile

gem 'carrierwave'
gem 'mini_magick'

To crop, zoom and rotate image we need to add 'cropzoom' file and 'custom.min' file in assets. Find here https://github.com/cropzoom/cropzoom

Following assetes changes required :

##/app/assets/javascripts/application.js

//= require jquery.cropzoom
//= require jquery-ui-1.10.3

##/app/assets/stylesheets/application.css

*= require jquery-ui-1.10.3.custom.min
*= require jquery.cropzoom

Now, to get coordinates of crop, zoom or rotated image we need to set attr_accessor for :
Cropped image coordinates : crop_x, crop_y, crop_w, crop_h
Zoomed image coordinates : zoom_x, zoom_y, zoom_w, zoom_h
Dragged image coordinates : drag_x, drag_y
Rotation angle : rotation_angle

So, my model looks like this :- 

/app/models/user.rb

class User < ActiveRecord::Base

mount_uploader :image, ImageUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :rotation_angle, :zoom_w, :zoom_h, :zoom_x, :zoom_y, :drag_x, :drag_y

Changes required in image crop view.


##/app/views/users/crop.html.erb

# Hide the original image.

<%= image_tag @user.image.url, class: 'img-responsive hide_img' %>

# Container where we will crop, zoom and rotate image
<div id='crop_container'></div>

<% form_for @user do |f| %>
  <% for attribute in [:zoom_w, :zoom_h, :zoom_x, :zoom_y, :drag_x, :drag_y, :rotation_angle, :crop_x, :crop_y, :crop_w, :crop_h] %>
    <%= f.hidden_field attribute, :id => attribute %>
  <% end %>
  <p><%= f.submit "Crop" %></p>
<% end %>

# Initialize cropzoom container

<script>
  $(document).ready(function() {
    var cropzoom = $('#crop_container').cropzoom({ 
      width:300,
      height:300,
      bgColor: '#CCC',
      enableRotation:true,
      enableZoom:true,
      zoomSteps:10,
      rotationSteps:10,
      selector:{
        centered:false,
        startWithOverlay: true,
        borderColor:'blue',
        borderColorHover:'yellow'
      },
      image:{
        source:$('.img-responsive').attr('src'),
        width:768,
        height:768,
        minZoom: 20,
        onRotate: function(imageObject, rotate_angle){
          // Get rotatation angle
          $("#rotation_angle").val(rotate_angle);
        },
        onZoom: function(imageObject, dimensions){
          // Get zoom coordinates
          $('#zoom_w').val(dimensions.w);
          $('#zoom_h').val(dimensions.h);
          $('#zoom_x').val(dimensions.posX);
          $('#zoom_y').val(dimensions.posY);
          // Set drag coordinates to zero when image is zoomed
          $('#drag_x').val(0);
          $('#drag_y').val(0);
        },
        onImageDrag: function(imageObject, position){
          // Get dragged image coordinates
          $('#drag_x').val(position.posX);
          $('#drag_y').val(position.posY);
          // Set zoom x-y coordinates to zero when image is dragged
          $('#zoom_x').val(0);
          $('#zoom_y').val(0);
        }
      }
    });
  });
  
  // Get cropped image coordinates

  $(document).on('click', 'input[type="submit"]', function() {
    var get_html = $('#infoSelector').html()
    var get_array = get_html.split('|')
    var get_x_y_coords_array = get_array[0].split('-')
    var get_w_h_coords_array = get_array[1].split('-')
    var get_x_coord = get_x_y_coords_array[0].split(':')[1]
    var get_y_coord = get_x_y_coords_array[1].split(':')[1]
    var get_w_coord = get_w_h_coords_array[0].split(':')[1]
    var get_h_coord = get_w_h_coords_array[1].split(':')[1]
    $('#crop_x').val(get_x_coord);
    $('#crop_y').val(get_y_coord);
    $('#crop_w').val(get_w_coord);
    $('#crop_h').val(get_h_coord);
  });
</script>

Crop view looks like this :-




Note :- In documentation of cropzoom plugin 'onImageDrag()' shuold have two values, but it have only one. Line no. 182 in my case

So, We need to change in jquery.cropzoom.js.

##/app/assets/javascripts/jquery.cropzoom.js

Change in $($image).draggable() function.

Change :- 
  if ($options.image.onImageDrag != null)
    $options.image.onImageDrag($image);
To :-
  if ($options.image.onImageDrag != null)
  $options.image.onImageDrag($image, getData('image'));

Required controller changes.

##/app/controllers/users_controller.rb

@user = User.find(params[:id])

#If we get image from remotely
@user.remote_image_url = User.first.image

if @user.update_attributes(params[:user])
  if params[:user][:crop_x].present?
    @user.image = @user.image.resize_and_crop
    @user.save!
    @user.image.recreate_versions!
  end
end

##/app/assets/stylesheets/custom.css

// To hide image 

.hide_img {
  display: none;
}

Now, finally changes are required in carrierwave uploader.

##/app/uploaders/image_uploader.rb

include CarrierWave::MiniMagick

version :custom_crop do
  process :resize_and_crop
end

def resize_and_crop
  if model.class.to_s == "User"
    if model.crop_x.present?
      manipulate! do |img| 
        w = model.crop_w.to_i
        h = model.crop_h.to_i
        
        // Set x-y coordinates of cropped image.
        x = model.zoom_x.to_i >= 0 ? (model.crop_x.to_i - model.zoom_x.to_i) : (model.zoom_x.to_i.abs + model.crop_x.to_i)
        y = model.zoom_y.to_i >= 0 ? (model.crop_y.to_i - model.zoom_y.to_i) : (model.zoom_y.to_i.abs + model.crop_y.to_i)
        x = model.drag_x.to_i >= 0 ? (x - model.drag_x.to_i) : (model.drag_x.to_i.to_i.abs + x) 
        y = model.drag_y.to_i >= 0 ? (y - model.drag_y.to_i) : (model.drag_y.to_i.to_i.abs + y) 

        img.combine_options do |i|
          // First we need to resize image with zoomed image. For more details you can find here "https://github.com/minimagick/minimagick"
          i.resize "#{model.zoom_w.to_i}x#{model.zoom_h.to_i}+#{model.zoom_x.to_i}+#{model.zoom_y.to_i}^\!"
          // Rotate zoomed image
          i.rotate(model.rotation_angle.to_i)
          // Crop zoomed and rotated image
          i.crop "#{w}x#{h}+#{x}+#{y}"
        end
        img
      end
    end
  end
end


## Final output is :-


Friday, 10 October 2014

CanCan ActiveModel::ForbiddenAttributesError with Rails 4

1) Replace gem in gem file
Use gem 'cancancan', '~> 1.9', in place of gem 'cancan'

2) In your controller use like this:
load_and_authorize_resource param_method: :my_sanitizer 

my_sanitizer is a example. It is your controller permit params method, similar like:

def my_sanitizer
    params.require(:article).permit(:name)
 end

Thursday, 9 October 2014

Access Control In Ruby

RUBY

What is access control?


Access control provide restriction to access sensible parts of your code from other. This feature enables you to hide the implementation details of your code, and specify a preferred way by which that code can be accessed and used.

Object is only thing with which we can make changes in application. The only way to change an object's state in Ruby is calling one of its methods.

Good rule of thumb in access control:
"never to expose methods that could leave an object in an invalid state"

In Ruby, the inheritance hierarchy or the package/module don't really enter into the equation in context of access control like any other language, it is rather all about which object is the receiver of a particular method call.

Ruby gives you three levels of protection:

Public methods : Public methods can be called by everyone - no access control is enforced. Methods are public by default, anyone can call them. Can be accessed with implicit & explicit receiver. But initialize method is always private.

Protected methods : Protected methods can be only called by objects of the defining class and its subclasses. Can be accessed with implicit & explicit receiver. However, usage of protected is limited.

Private methods : Private methods cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object. We cannot call another object's private methods.

So now implicit & explicit receiver comes in picture.

What is implicit & explicit calling ?
In explicit calling we need to provide reference of object, on which method should be call. We provide this using self or using class instance. But in implicit calling we do not need to provide any reference to object, it will automatically take self as referencing object.

Confused confused confused :(
If we take example from real world then it might be easy to understand. If I ask some one to purchase a book then without any specification that person will go to bookshop not to hospital or medical. This we can consider as implicit calling. Now if I ask that person to purchase book from specific vendor (ABC shop). Then he will go to that specific shop to purchase that book. This we call as explicit calling.

Lets see below code to understand what Implicit and Explicit calling is


class Test
  def method1
    # Implicit calling
    method2
  end

  def method2
  end
end

t = Test.new
# Explicit calling
t.method1



We will study further about private and protected access modifier.

How we can define a method private or protected in ruby?
There are two ways to define a method private or protected.


class MyClass

  def method1          # this method is public
  end

  protected

    def method2        # this method is protected
    end

  private

    def method3        # this method is private
    end

end


class MyClass

  def method1          # this method is public
  end

  def method2
  end

  def method3
  end

  protected :method2   # this method is protected

  private :method3     # this method is private
end

Lets run below example.


class MyClass
  def method1
    self.private1
  end

  private

  def private1
    puts "Hello I am Private"
  end
end

MyClass.new.method1

Running this produces the following noMethodError:


in `method1': private method `private1' called for #<XXXX> (NoMethodError)


Oh no! I missed private method receiver concept. Private method can be called only with implicit receiver. So I have changed my class like below.


class MyClass
  def method1
    private1
  end

  private

  def private1
    puts "Hello I am Private"
  end
end

MyClass.new.method1

Now run again.


Hello I am Private

I get desired output from my code. Now if I call it in subclass then.


class ClassA
  def main_method1
    method1
  end

  private
  def method1
    puts "hello from #{self.class}"
  end
end

class ClassB < ClassA
  def main_method1
    method1
  end
end

ClassA.new.main_method1
ClassB.new.main_method1

If I run this program.


hello from ClassA
hello from ClassB

Get desired result. "This means we can call a private method from within a class it is declared in as well as all subclasses of this class"

We can not call private method with explicit receiver not even with self. So changing previous example accordingly.


class ClassA
  def main_method1
    method1
  end

  private
  def method1
    puts "hello from #{self.class}"
  end
end

class ClassB < ClassA
  def main_method1
    self.method1
  end
end

ClassA.new.main_method1
ClassB.new.main_method1

Output :


hello from classA
`main_method1': private method `method1' called for #<XXXX> (NoMethodError)

Now Moving to protected method.
protected methods can be called by any instance of the defining class or its subclasses.

So changing examples for protected method.


class MyClass
  def method1
    protected1
  end

  def method2
    self.protected2
  end

  protected

  def protected1
    puts "Hello I am Protected1"
  end

  def protected2
    puts "Hello I am Protected2"
  end
end

MyClass.new.method1
MyClass.new.method2

Output:


Hello I am Protected1
Hello I am Protected2

Similarly for subclass:


class ClassA
  def main_method1
    method1
  end

  protected
  def method1
    puts "hello from #{self.class}"
  end
end

class ClassB < ClassA
  def main_method1
    self.method1
  end
end

ClassA.new.main_method1
ClassB.new.main_method1

Output:


hello from ClassA
hello from ClassB

So no change in output.

Now I added another class C


class ClassC < ClassA
  def main_method1
    ClassB.new.method1
  end
end

ClassC.new.main_method1

Run it and output is :


hello from ClassB


Why we need Protected ??

why we need protected if we can call a method explicitely why we need implicit call to a method.
If private method is so secure why we need one more level of access with protected in ruby.

I found my solution when I try to compaire two objects of same class. This can not achive with explicit reciever(Private Method).


class Person
  def initialize(number)
    @number = number
  end

  def number
    @number
  end

  def compare_number(c)
    if c.number > number
      "The second number is bigger."
    else
      "The second number is the same or smaller."
    end
  end

  protected :number

end

first = Person.new(25)
second = Person.new(34)

puts first.compare_number(second)

Output is:


The second number is bigger.

We cannot accomplish  this with private method only and public method are not secure for such methods. So It's clear protected methods are equally important in ruby as private.

Tuesday, 16 September 2014

Configure Nginx with Unicorn on Ubuntu(Linux)

Introduction

So finally you choose to go with NginxUnicorn for your rails application.  Thats great!
We will go through with all the aspect for deploying your app on cloud. For sake of hand we will use AWS's EC2 server for deployment as it is mostly used.


Prepare your instance & Install dependent libraries

When you purchase the instance it is almost blank except OS installed. So we need to prepare it for our deployment. Lets install them first.

$ sudo apt-get update
$ sudo apt-get install build-essential git-core
$ sudo apt-get install zlib1g-dev libssl-dev

Also install vim as you will need it when updating config files on server.

$ sudo apt-get install vim

Install Nginx

Lets install Nginx.

$ sudo apt-get install nginx

It should installed nginx at /etc/nginx . Inside that directory you will find nginx.conf which main config file for Nginx. We will talk more about it later.

Install Ruby

Now its time to install ruby.

Follow the instruction to install RVM at https://rvm.io/rvm/install . There are 2 ways to install rvm
1) Single User installation
2) Multi User installation

I would suggest to go with Multi user installation. You just need to add any user to role "rvm" whom you wanted to give access to use rvm.


Unicorn

Copy below code to unicorn.rb under /config directory of your project. 



# Sample verbose configuration file for Unicorn (not Rack)
#
# This configuration file documents many features of Unicorn
# that may not be needed for some applications. See
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
# for a much simpler configuration file.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.

# WARNING: See config/application.rb under "Relative url support" for the list of
# other files that need to be changed for relative url support
#
# ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"

# Read about unicorn workers here:
# http://doc.gitlab.com/ee/install/requirements.html#unicorn-workers
#
worker_processes 3

# Since Unicorn is never exposed to outside clients, it does not need to
# run on the standard HTTP port (80), there is no reason to start Unicorn
# as root unless it's from system init scripts.
# If running the master process as root and the workers as an unprivileged
# user, do this to switch euid/egid in the workers (also chowns logs):
# user "unprivileged_user", "unprivileged_group"

# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/path/to/your/project" # available in 0.94.0+

# Listen on both a Unix domain socket and a TCP port.
# If you are load-balancing multiple Unicorn masters, lower the backlog
# setting to e.g. 64 for faster failover.
listen "/path/to/your/project/tmp/sockets/unicorb.socket", :backlog => 1024


# nuke workers after 30 seconds instead of 60 seconds (the default)
#
# NOTICE: git push over http depends on this value.
# If you want be able to push huge amount of data to git repository over http
# you will have to increase this value too.
#
# Example of output if you try to push 1GB repo to GitLab over http.
#   -> git push http://gitlab.... master
#
#   error: RPC failed; result=18, HTTP code = 200
#   fatal: The remote end hung up unexpectedly
#   fatal: The remote end hung up unexpectedly
#
# For more information see http://stackoverflow.com/a/21682112/752049
#
timeout 60

# feel free to point this anywhere accessible on the filesystem
pid "/path/to/your/project/tmp/pids/unicorn.pid"

# By default, the Unicorn logger will write to stderr.
# Additionally, some applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path "/path/to/your/project/log/unicorn.stderr.log"
stdout_path "/path/to/your/project/log/unicorn.stdout.log"
# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

# Enable this flag to have unicorn test client connections by writing the
# beginning of the HTTP headers before calling the application.  This
# prevents calling the application for connections that have disconnected
# while queued.  This is only guaranteed to detect clients on the same
# host unicorn runs on, and unlikely to detect disconnects even on a
# fast LAN.
check_client_connection false

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

  # The following is only recommended for memory/DB-constrained
  # installations.  It is not needed if your system can house
  # twice as many worker_processes as you have configured.
  #
  # This allows a new master process to incrementally
  # phase out the old master process with SIGTTOU to avoid a
  # thundering herd (especially in the "preload_app false" case)
  # when doing a transparent upgrade.  The last worker spawned
  # will then kill off the old master process with a SIGQUIT.
  old_pid = "#{server.config[:pid]}.oldbin"
  if old_pid != server.pid
    begin
      sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
      Process.kill(sig, File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
  #
  # Throttle the master from forking too quickly by sleeping.  Due
  # to the implementation of standard Unix signal handlers, this
  # helps (but does not completely) prevent identical, repeated signals
  # from being lost when the receiving process is busy.
  # sleep 1
end

after_fork do |server, worker|
  # per-process listener ports for debugging/admin/migrations
  # addr = "127.0.0.1:#{9293 + worker.nr}"
  # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)

  # the following is *required* for Rails + "preload_app true",
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

  # if preload_app is true, then you may also want to check and
  # restart any other shared sockets/descriptors such as Memcached,
  # and Redis.  TokyoCabinet file handles are safe to reuse
  # between any number of forked children (assuming your kernel
  # correctly implements pread()/pwrite() system calls)
end

You can change "worker_processes" and "timeout" according to your app and sever config.

Configure Nginx to use Unicorn


Now time to configure Nginx. Create a file yoursite.com in "/site-available" directory under nginx's home directory(/etc/nginx) .


upstream your_app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/path/to/your/project/tmp/unicorn.sock fail_timeout=0;
}

server {


    listen 80;
    server_name yoursite.com;

    # Application root, as defined previously
    root /path/to/your/project/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://your_app;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}  


Please make sure to match socket path of unicorn. It should match with what you have defined in unicorn.rb .


Time to start Unicorn

Every thing is setup. Now time to start Unicorn. From root of your application directory run below command. Don't forget to adjust environment if you are running in staging or anyother environment.


bundle exec unicorn_rails -c config/unicorn.rb -E production -D


At this point your site is setup and running now. Go to yoursite.com .

Monday, 15 September 2014

Commands for exporting and importing database from postgresql

To export data:
pg_dump -U username -Fc database_name > db_backup.dump


To import data:
pg_restore --verbose --clean --no-acl --no-owner -d database_name db_backup.dump

Tuesday, 9 September 2014

Setting hosts on ubuntu and windows 7


Setting hosts on ubuntu :


Setting host on ubuntu is pretty straight forward. You just need to have sudo user permission to do so.

Type below command (using gedit as editor):

sudo gedit /etc/hosts

It will open hosts file and you can make your changes there.


Using vim editor :

sudo vi /etc/hosts

Setting hosts on Windows 7:


It's very complex to set hosts in windows 7 although you have all administrator rights. Windows restrict you to make any changes in your hosts file. 

We don't need to make any permission changes in hosts file (Some time we may forgot to change file permission again to it's original permissions). So first of all we will start with finding the hosts file.

The path to the Hosts file in Windows 7 is the same as usual. open start menu and run :

%systemroot%\system32\drivers\etc\

This will open etc folder in which you find hosts file. You can manually find hosts file also.

C => Windows  => System32 => drivers => etc => hosts

Follow below steps :

- Before doing anything create a backup of your hots file.
- Separate copy hosts file to any other location on system (I prefer on desktop).
- Make changes in new file that we have on desktop.
- Replace original hosts file with new one that we have on desktop.
- Restart your system (Not mandatory)

We are done :)


Monday, 8 September 2014

Configure Ubuntu 12.04 for Ruby On Rails development


What is "Ruby On Rails"?


Ruby is a programming language and rails is a software library that extends the Ruby programming language. David Heinemeier Hansson is creator of rails. He gave it the name "Ruby on Rails" though it is often just called "Rails."

Install Ruby with RVM


To make sure our all the package that we are going to install are upto date. We should run a quick update. Open terminal and type command:

sudo apt-get update

Once we have done with update. We will start installing RVM (Ruby Version Manager). RVM is a great tool to manage multiple versions of ruby on single machine.

In this blog we will going to use curl to install RVM.

If you don't have curl on your system, then type command:

sudo apt-get install curl

After you finish with curl. Install rvm using following command:

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

After installation finished, load RVM. You may need to exit out of your terminal and start up a new one. To load RVM :

source ~/.rvm/scripts/rvm

RVM has some of its own dependancies that need to be installed. To automatically install them:

rvm requirements

You are done with rvm. Now installing ruby is easy.

rvm install ruby-2.x.x
rvm install ruby-1.9.x

And so on. Using specific version as default.

rvm install ruby-2.x.x --default

Sometime you may get following error:

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.
 

Answer for error is in error itself. go here and make desired changes and you are done.

Now next we makes sure that we have all the required components of Ruby on Rails. So we continue to install rubygems (By default RVM already installed gems for current version). 

rvm rubygems current

Once everything is set up, it is time to install Rails. 

gem install rails

This process may take a some time, be patient. Now we have Ruby On Rails on our machine.

For different requirement we need different database. So now we start with PostgreSQL setup. By default sqlite used as database in rails.

 

Install PostgreSQL:


To download Postgres and its helpful accompanying dependencies:

sudo apt-get install postgresql postgresql-contrib

You are done with postgres installation.

Create database user(Role):

Postgres uses the concept of roles to distinguish the variety of users that can connect to a database. When it is first installed on a server, the default postgres user is actual named “postgres”. 

To create custom user, first switch into the default user:

sudo su – postgres

Now create new role(user) in PostgreSQL system :

createuser

It will propmpt you as follow :

Enter name of role to add: newuser
Shall the new role be a superuser? (y/n) y

If you want to set password for newly created Role(User) then command should be:

createuser --pwprompt


Install MySQL:


installtion of MySQL on ubuntu is pretty simple. Open command promt and type :

sudo apt-get install mysql-server

During installation process MySQL will ask you to set a root password. Set your root password and you are done.

Now we have fully configured machine with RVM, PostgreSQL and MySQL for rails development.

CREATE EXTENSION hstore getting error

Log:
-------------------------------------------------------------------------------------------------------------------
-- execute("CREATE EXTENSION hstore")
rake aborted!

StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedFile: ERROR:  could not open extension control file "/usr/share/postgresql/9.1/extension/hstore.control": No such file or directory
: CREATE EXTENSION hstore/usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
--------------------------------------------------------------------------------------------------------------------

Solution:

If you have postgresql already installed then run below command

$ sudo apt-get install postgresql-contrib


Friday, 5 September 2014

SETTING UP A CLOUDFRONT CDN FOR RAILS

The Problem:

Even with the asset pipeline, content is transmitted to the end user over their, sometimes slow, connection. Distance plays a role in the speed at which data can be delivered. Because of this, users further away from your servers get a sluggish experience. This decreases both the users’ patience and your ability to effectively engage them.


The Solution:

CDNs are networks of servers that host your content so that when you make a request, the request is served from a server closest to you. This can often reduce transmission time by several seconds (…think users in Asia requesting content that’s served from Virginia – this is the case if you deploy to Heroku).

The use of a CDN also reduces the number of requests to your application servers. Most Ruby applications use Nginx or Apache in front of the Ruby processes. These HTTP servers are really good at serving static content, but no one will deny – the fewer requests, the better.

Option 1: Push your assets to the CDN during deployment

Depending on your method of deployment, pushing assets to a CDN isn’t always trivial. The asset_sync gem has made this relatively straight forward if you choose to host your assets from Amazon S3. Out of the 2 options, this is the most efficient because all requests for assets will be diverted away from your application, leaving its precious processing power to serve more important application requests.

Option 2: Assets are pulled to the CDN on first request

This option won’t change deployment and is simple to setup. The only downside is that upon first request to an asset, the CDN will pull it from your web server and cache it (it’s hardly a downside if you’re currently serving all your assets from your web server). All subsequent requests to that asset will be served straight from the CDN. The simplicity of this option generally makes it my preferred option.

So let’s get to it…




Amazon Cloudfront:

Log in to your Amazon EC2 account and click “Cloudfront”:

"Click Cloudfront in the AWS web console"


Click “Create Distribution”:

"Create a Cloudfront distribution endpoint"

Enter the domain where your assets currently live (ignore Origin ID – it’ll be filled in for you):

"Settings for a typical CDN"


Make note of the Cloudfront distribution URL

"Cloudfront distribution URL"

In Rails application:


Rails provides and easy way to change the host URL of the assets (images, stylesheets, javascripts, fonts…). Enter the Cloudfront distribution URL from above as the Rails asset_host.

# config/environments/production.rb
config.action_controller.asset_host = "d24xjtg100euk4.cloudfront.net"
At this point, the domain of all Rails asset helpers image_tag, stylesheet_link_tag, and javascript_include_tag will be prefaced with the asset host URL that you configured above.

For example:

image_tag("shark_teeth.png")
# http://d24xjtg100euk4.cloudfront.net/assets/images/shark_teeth.png
Note: if you only change config/environments/production.rb, you won’t see any changes in your development environment.

And that’s it!



In AngularJS, how would you cache the data (for example, a list of top 10 news of today) you receive from Java?


I am fetching the list of news from the Backend and want to cache them and display them in HTML5 using the AngularJS.

I am new to Angular and don't know the correct way to do this. is it possible to cache this data or not. Please suggest.


Thursday, 4 September 2014

conditional multiplication of rates as per increase count

What is best approach and coding practice for my below requirements:
As I have user records in my table, and I want to charge user as per conditional basis
Condition is, If user has 20 record then system will charge by 500 USD per record If user has 30 record then System will charge then system will charge 500 USD per record for 20 records and 300 USD for next 10 records also If user has more than 30 records then charges then calculation will be (20 * 500 + 10 * 300 + addition-records * 100 )

Record Table

Post(:id, :content, :user_id)

I have also subscription table where I manage charges
I will appreciate If coders will uses constant for static values, like
$ {1 to 20 => 500, 20 to 30 => 300, 30 to x => 100 } ...etc

Wednesday, 3 September 2014

Rename Database Column in a Rails Migration

rename_column :table_name, :old_column_name, :new_column_name
Rails g migration ChangeColumnName
# creates  db/migrate/xxxxxxxxxx_change_column_name.rb

# db/migrate/xxxxxxxxxx_change_column_name.rbclass ChangeColumnName < ActiveRecord::Migration
  def self.up
    rename_column :table_name, :old_column, :new_column
  end

  def self.down
    # rename back if you need or do something else
  end
end

Tuesday, 2 September 2014

Install postgres on ubuntu 12.04

postgresql is a powerful and reliable object-relational database system. it’s a great alternative for mysql. it is as easy to set up, performs better and offers far more features.

1. make sure you already have install python-software-properties
     $sudo apt-get install python-software-properties

2.add ppa repository to my ubuntu.
     $sudo add-apt-repository ppa:pitti/postgresql

3. after adding ppa, update your system apt:
     $sudo apt-get update

4.finally install postgresql-9.1:
     $sudo apt-get install postgresql

   if you having any error, make sure you already  install libpq-dev.the libpq-dev package is for compiling   wrappers/clients against libpq.
     $sudo apt-get install postgresql-9.1 libpq-dev

5.now check it out installation is successful or…..
     $ locate postgresql

6. if done!!!, cheers …………..   check the install version.
     $psql -v

7. now let’s take look to postgres console. if you are login as root user
     $su postgres

   for other user    
     $ sudo su postgres -c psql
   now you are on postgres console