Posts

Showing posts from January, 2012

RVM : Ruby Version Manager : A Complete Guide

Install RVM   bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head ) Show all rubies and gemsets rvm list gemsets Upgrade to the latest version of rvm from github rvm update --head   Ruby on Rails 3 Tutorial says to follow this with     rvm reload     rvm install 1.8.7     rvm install 1.9.2   and, if you need a patchlevel, do something like       rvm install 1.8.7-p174   Install a given version of ruby     rvm install 1.9.2   If this fails with compiler errors, you may need to install some packages.       rvm package install openssl     rvm package install readline     rvm package install iconv     rvm install 1.9.2 --with-openssl-dir=$HOME/.rvm/usr \       --with-readline-dir=$HOME/.rvm/usr \       --with-iconv-dir=$HOME/.rvm/usr     On Mac, you may need to install some libraries via MacPorts.       sudo port install ncurses     sudo port install libyaml     sudo port install zlib      rvm install 1.9.2 --with-libyaml-dir=/opt/loca

Simulating Multiple Inheritance with Mixins

You want to create a class that derives from two or more sources, but Ruby doesn't support  multiple inheritance in such cases you can use mixins A Ruby class can only have one superclass, but it can include any number of modules. These modules are called  mixins. If you write a chunk of code that can add functionality to classes in general, it should go into a mixin module instead of a class. The only objects that need to be defined as classes are the ones that get instantiated and used on their own (modules can't be instantiated). When a class includes a module with the include keyword, all of the module's methods and constants are made available from within that class. They're not copied, the way a method is when you alias it. Rather, the class becomes aware of the methods of the module. If a module's methods are changed later (even during runtime), so are the methods of all the classes that include that module. Below is the simple example. Suppose

Basics Of Ruby Hashes

Hashes and arrays are the two basic "aggregate" data types supported by most modern programming languages. The basic interface of a hash is similar to that of an array. The difference is that while an array stores items according to a numeric index, the index of a hash can be any object at all. empty = Hash.new # => {} empty ={} # => {} numbers = { 'two' => 2, 'eight' => 8} # => {"two"=>2, "eight"=>8} numbers = Hash['two', 2, 'eight', 8] # => {"two"=>2, "eight"=>8} Once the hash is created, you can do hash lookups and element assignments using the same syntax you would use to view and modify array elements: numbers["two"] # => 2 numbers["ten"] = 10 # => 10 numbers # => {"two"=

Configuring Rails to Send Email

Add the following code to config/environment.rb: ActionMailer::Base.server_settings = {   :address        => "mail.yourhostingcompany.com",   :port           => 25,   :domain         => "www.yourwebsite.com",   :authentication => :login,   :user_name      => "username",   :password       => "password" } Replace each hash value with proper settings for your Simple Mail Transfer Protocol (SMTP) server. You may also change the default email message format. If you prefer to send email in HTML instead of plain text format, add the following line to config/environment.rb as well: ActionMailer::Base.default_content_type = "text/html" Possible values for ActionMailer::Base.default_content_type are "text/plain", "text/html", and "text/enriched". The default value is "text/plain". ActionMailer::Base.server_settings is a hash object containing configuration parameters to connec

Many to Many associations in Rails HABTM and has_many :through

We've noticed there's a bit of confusion about the differences between the two ways to create many-to-many relationships using Rails associations.  Has and Belongs to many (HABTM) or Join Table Associations create_table "dancers_movies", :id => false do |t| t.column "dancer_id", :integer, :null => false t.column "movie_id", :integer, :null => false end class Dancer < ActiveRecord::Base has_and_belongs_to_many :movies end class Movie < ActiveRecord::Base has_and_belongs_to_many :dancers end   has_and_belongs_to_many associations are simple to set up. The join table has only foreign keys for the models being joined - no primary key or other attributes.There is no model class for the join table. has many : through or Join Model associations create_table "appearances", do |t| t.column "dancer_id", :integer, :null => false t.column "movie_id", :integer, :null =>

Prototype, and JQuery inside Rails Application

It has become common practice to use JQuery as the javascript library of choice for Ruby on Rails  applications with the latest versions of rails using JQuery.  But If you have an Rails application that is already using Prototype, it may create problems to replace that with JQuery . And sometimes, you just simply need both to work at the same time. Use Prototype/Scriptaculous with JQuery  First, in our application layout, we’re going to get rid of <% = javascript_include_tag :defaults %> and include each javascript file explicitly just to make things a little easy to understand and a little more straight forward.  So we have replaced defaults with  <%= javascript_include_tag 'prototype', 'scriptaculous', 'application' %> Now, we’ll add in JQuery, by downloading the latest version of JQuery from the JQuery Download page from the JQuery Website <%= javascript_include_tag 'prototype', 'scriptaculous', '

Convert Pdf to Swf with SWFTools and Intallation on Linux Ubuntu

Installation And Usage First you need to download the SWFTools for SWF manipulation and generation , for that you can directly download from here Or Take a Git Clone   git clone git://git.swftools.org/swftools   So these are the steps you need to follow tar -zvxf swftools-0.x.x.tar cd swftools-0.x.x ./configure make make install   You also need to install few libraries, You can directly install these from Ubuntu Software Center , if you are using Ubuntu  freetype jpeglib Now type this to confirm the installation whereis pdf2swf   Once you have the location, you will be ready with the command whereis pdf2swf pdf2swf: /usr/bin/pdf2swf /usr/share/man/man1/pdf2swf.1.gz Now you are on the way to use the command to covert pdf to swf /usr/bin/pdf2swf -zl -p 1-10 -s bitmap -j 75 --viewer /path_to_background.swf /path_to_pdf_file.pdf -o /Output_pdf.swf

Sending Binary Data over the Network with Json

First, you need to open the file. Since the file is binary, you have to open it with an additional “b” flag. Here i am sending a zip file f = File.open("path/myzip.zip", "rb")   Next, you have to do binary-to-text encoding on the binary file because the JSON will complain if you try to feed it a binary string. To overcome this obstacle, you need to apply Base64 encoding. file_content = Base64.b64encode(f.read())   After you have a text representation of the data, you can then use JSON to package it, you can use to_json to convert in to json {:file=>file_content}.to_json In your  Rails application you can do this like render :layout=>false , :json => {:file=> file_content}.to_json

What is the Role of HTML5 in Rails 3

We’ve all heard that Rails 3 uses HTML5. That’s true, but the actual role that HTML5 plays in Rails 3 applications may be simpler than you suspect. Before that you need to know few things What rails.js does It finds remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX. It triggers six javascript events to which you can bind callbacks to work with and manipulate the AJAX response. Remember the first thing rails.js does? Before it can override the click and submit events of all the remote elements, it first needs to find all the remote elements. Rails 3 takes advantage of this new valid markup, by turning all links and forms with :remote => true into HTML elements that have the tag data-remote=true. <%= link_to "home", {:action => "index"}, :remote => true, :class => "button-link" %> # => <a href="index" class="button-link" data-remote