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

create anonymous user in devise

Hi,

I need to craeet the anonymous/guest user at some satuations and it was required to have email as the mandatory column in the DB. I have used the below process for creating the email on fly to make it more unique.

def create_anonymous_user
u = User.new(email: "guest_#{Time.now.to_i}#{rand(99)}
@example.com")
u.confirmed_at = Time.now
u.save(:validate => false, :confirm => false)
u
end



I hope this make the user record creation by eliminating all required validations and making the email unique.

Thank You,
Uma Mahesh.

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.

Friday, 7 September 2012

memory management in ruby

In Ruby memory management is done by Garbage collector. In Ruby every thing is stored in heap memory. So in ruby memory allocation and freezing of memory will be done automatically.




As ruby is dynamic datatype, the default datatype will be taken as string and once the value was assigned to variable, depending on the datatype the memory will be allocated and remaining memory will be deleted in the heap. So it is not required to release the allocated memory by programmer as every thing will be taken care by the garbage collector.

Below is the URL, you can view the clear explanation of how the memory allocation and how garbage collector freed memory is ruby,

URL: http://patshaughnessy.net/2012/3/23/why-you-should-be-excited-about-garbage-collection-in-ruby-2-0

Question:
Q) Does ruby uses stack for memory allocation?
A) Here is the complete explanation of how memory management takes places was explained clearly on bases of ruby version wise.
URL: http://www.engineyard.com/blog/2010/mri-memory-allocation-a-primer-for-developers/

Thank You,
Uma Mahesh.

Wednesday, 5 September 2012

learn git in 15 minutes

Hi,

If you are new to git and git commands you can learn the git easily with try.github.com.

 



It provides a very good interface where you can type git commands and can learn the git easily.

I hope you know the advantage of git and how the web world is knocking it. So lets start learning git today.

URL: http://try.github.com/

Thank You,
Uma Mahesh.

Monday, 3 September 2012

calling an ApplicationController method from console

Hi,

Recently I had a issue with one of the thing and I was supposed to debug it by calling an ApplicationController method from console. Below is the way of doing it.


> object_for_application = ActionController::Base::ApplicationController.new

> object_for_application.my_method


Thank You,
Uma Mahesh.

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.




Metaprogramming in ruby

Hi, 



creating code at runtime that defines new methods. In fact, in a sense all Ruby programming is metaprogramming, since even a class definition is not a declaration as it is in Java but actually code that is executed at runtime. Given that this is true, you might wonder whether you can modify a class at runtime. In fact you can, by adding or changing instance methods or class methods, even for Ruby’s built-in classes.


It is a basic feature of Ruby that you can re-open a class definition 
and add or redefine methods.


Thank You,
Uma Mahesh