Showing posts with label gems. Show all posts
Showing posts with label gems. Show all posts

Tuesday, 30 October 2012

free publicly facing url

Hi,

tunnlr provides you public interface url for your web application to access any where which was running in local machine.

If you're running your server locally and have no publicly facing endpoint, you can use a service like tunnlr to create one. This creates a publicly accessible URL with an SSH tunnel back to your local machine.


Website: http://tunnlr.com/

Create account in tunnlr and get a public url for you.


In order to use tunnlr in our rails application we have a gem 'tunnlr_connector'(A plugin for interacting with tunnlr on both unix and windows. Uses the net::ssh gem)

Add gem to your gem file.

gem 'tunnlr_connector', :require => "tunnlr"


run 'bundle install' to install the gems

Now you need to configure local system to tunnlr account. This can be done by running the below command.

rake tunnlr:configure

and enter the email address and password associated with your tunnlr account.

To create a tunnel, run

rake tunnlr:start

The above command will run tunnel server in local machine. You can browse the application with url: http://web1.tunnlr.com:12606/





Thank You,
Uma Mahesh.

Thursday, 13 September 2012

Generating barcode in ruby on rails

Hi,

In ruby we can generate bar code easily with nice wrapper using barby.


BARBY - The Ruby barcode generator. To know more about barby check the http://toretore.eu/barby/


Here are the simple ruby program to generate barcode in ruby file.

require 'barby'
require 'barby/barcode/code_128'
require 'barby/outputter/png_outputter'

barcode = Barby::Code128B.new('The noise of mankind has become too much')
File.open('code128b.png', 'w'){|f|
  f.write barcode.to_png(:height => 20, :margin => 5)
}



Below is the stepwise instructions to implement barcode in rails application.


Add the below gems to your gem file.


gem 'barby'
gem "has_barcode"


Add the below code to your model to which you would like to generate barcode

class Product < ActiveRecord::Base
  include HasBarcode

  has_barcode :barcode,
    :outputter => :svg,
    :type => :code_39,
    :value => Proc.new { |p| "#{p.id}" }

end



Display the barcode in your view as below

<%= @product.barcode_data.html_safe  %>


By using above simple steps you can generate barcode in rails application.

Thank You,
Uma Mahesh.

Tuesday, 11 September 2012

generating pdf in rails application

Hi,

Recently for one of the requirement in my project I was supposed to create the pdf report in the rails application. I have made some research and found the "wicked_pdf" is one of the best gem available in rails.

wicked_pdf makes the life easy for pdf file generation directly from HTML page.

Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.

Wicked PDF works on Ruby 1.8.7 and 1.9.2; Rails 2 and Rails 3.

In order to use the wicked_pdf you need to install wkhtmltopdf from http://code.google.com/p/wkhtmltopdf/


next add this to your Gemfile

gem 'wicked_pdf'

You can get the gem from : https://github.com/umamahesh/wicked_pdf

In your controller:

class HomeController < ApplicationController
  def get_pdf
    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => "pdf_file"
      end
    end
  end
end



There are buch of options avilable to make the pdf file look and feel nicely. You can have the clear information at https://github.com/umamahesh/wicked_pdf



Use it and let me know your comments on this gem.

One more things, if you are going to deploy application in heroku then this gem will not be usefull as heroku will not provide the option to install the wkhtmltopdf :(


You may need to use prawn gem for that.



Thank You,
Uma Mahesh.
 

Sunday, 9 September 2012

Adding ssl for active admin

Hi,

I would like to provide complete admin section with ssl, which provides high level of security for the admin section. I am using active admin for my application. I have searched for available options and finally I have found a perfect match for my requirement. gem 'rack-ssl-enforcer' provides the required functionality for us.

gem 'rack-ssl-enforcer'

add the gem to your gem file and run the command 'bundle install' to install the gem.

You need to add the required configurations settings to make the admin section work with ssl.

Add the below line in the config/application.rb

config.middleware.use Rack::SslEnforcer, :only => %r{^/admin/}

Points to be observed from the above line.

1) We are adding 'Rack::SslEnforcer' as a middleware.
2) We are adding rule based ssl.

The above configuration will make the middleware to force the ssl for the constraint that was passed as condition to it. So it will aforce ssl to be applied for admin section.

To me about the gem 'rack-ssl-enforcer', go through the complete section of github URL: https://github.com/umamahesh/rack-ssl-enforcer

Thank You,
Uma Mahesh.

Monday, 3 September 2012

awesome_print : Ruby printer object

Hi,

awesome_print is the ruby tool to print ruby object with the useful and clear information.

We use 'p' and object.inspect to view the complete object parameters like non-persistent attributes. By using the new 'awesome_print' you can view the complete attributes clearly.

> gem "awesome_print"

In your controller method
> @event = Event.find(params[:id])
> ap @event

 In your console

>   :id => 65,
      :title => "XXXXXXXXXXXXXXXX",
      :description => "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
      :price => 132.0,
      :created_at => XXXXXXXXXXXXXXXXXXXXXXX,
      :updated_at => XXXXXXXXXXXXXXXXXXXXXXX,
      :permalink => "XXXXX".

This will help us on debugging the code.

Thank You,
Uma Mahesh.




Friday, 29 June 2012

creating indexes for all database tables on fly in rails

Hi,

Indexing plays a major role in database query optimisation. So it is suggested to have index for foreign_key columns.

Coming to rails migrations, our migration will not create indexes for the columns with foreign keys. Where you need to create manually those indexes.


Normal migration will be as below

class CreateArticle < ActiveRecord::Migration
  def self.up
    create_table "articles" do |t|
      t.string :content
      t.integer :post_id
      t.integer :user_id
    end
  end

  def self.down
    drop_table "articles"
  end
end


Inorder to add indexes, add the indexes as below:

class CreateArticle < ActiveRecord::Migration
  def self.up
    create_table "articles" do |t|
     t.string :content
     t.integer :post_id
     t.integer :user_id
    end

    add_index :articles, :post_id
    add_index :articles, :user_id
  end

  def self.down
    drop_table "articles"
  end
end


In some cases we may not sure which column need to index, In that case I strongly recommend to use the below gem which creates indexes for all the foreign_key columns on fly by running a simple command.

gem rails_indexes(A rake task to track down missing database indexes.) https://github.com/umamahesh/rails_indexes

usage:

add the gem to your gem file.

gem "rails_indexes"

> bundle install

> rake db:index_migration

Display a migration for adding/removing all necessary indexes based on associations:


Thank You,
Uma Mahesh.