Setup script to get Ruby and Rails running on Ubuntu with one command using RailsReady

Posted by Bhushan Ahire | Posted in git, Rails, ruby, Subversion | Posted on 05-12-2011

0

How would you like to get a full Ruby on Rails stack up on Ubuntu with one command?

Now you can by running Rails Ready. Rails Ready is a setup script that gets Ruby and Rails running on a fresh install of Ubuntu with one command (Tested on Ubuntu server 10.04 LTS (Long-term Support)).

This is a brand new project by Josh Frye that he uses all the time to setup VMs, but there’s always testing to be done and improvements to be made.

Running the Script

Check out railsready.sh to see everything Rails Ready is doing.

  sudo wget --no-check-certificate https://github.com/joshfng/railsready/raw/master/railsready.sh && bash railsready.sh

The script will then ask if you want to build Ruby from source or install RVM. If you want to watch the magic happen just run tail -f ~/railsready/install.log.

What gets installed?

  • An updated system (Linux only)
  • Homebrew (OSX only)
  • Ruby 1.9.3 latest patch level (installed to /usr/local/bin/ruby) or RVM running 1.9.3 latest patch level
  • Imagemagick
  • libs needed to run Rails (sqlite, mysql, etc)
  • Bundler, Passenger, and Rails gems
  • Git

All you need to do is install NGINX or Apache, run passenger-install-nginx-module or passenger-install-apache-module, upload your app, point your vhost config to your apps public directory and go!

A note about RVM+passenger+nginx: Passenger installed via RVM can’t locate the OpenSSL package installed on Ubuntu. A user contributed fix is as follows:

rvm remove 1.9.2
rvm package install openssl
rvm install 1.9.2 --with-openssl-dir=$HOME/.rvm/usr
rvmsudo passenger-install-nginx-module

Hope this guide will be helpful to you.

Removing “out of sync” error in acts_as_solr

Posted by Bhushan G Ahire | Posted in JRuby, Rails, ruby | Posted on 05-10-2010

0

Solr is an open source enterprise
search server based on the Lucene Java search library, with XML/HTTP and JSON APIs, hit highlighting, faceted search, caching, replication, a web administration interface and many more features. It runs in a Java servlet container such as Tomcat.  -Apache Solr

Solr can be used in different containers and different wrappers. Our application runs on Ruby on Rails, and we used acts_as_solr. Though solr is a powerful, already stable and yet flexible third party solution that we could rely on, we were still not able to maximize its full capacity. We used the bare minimum features of solr for our search modules.

As of now, we’ve used a couple of acts_as_solr enhancments and add ons, some of which we learned from different online resources. We were able to use db_free_solr and explored on the highlighting and faceting capabilities of solr. Its been pretty helpful, but of course nothing is almost always seamless. We encounter few problems with syncing records from the database and onto solr. For sure, you’ve come across this trouble before, if you’ve been using solr:

Out of sync! Found N items in index, but only n were found in database!

It sure was putting down every page wherein there was this glitch in the count of the records retrieved. It therefore gave the negative impression that our site was frequently unstable. Removing a certain indexed element from the solr index is easy as:

ActsAsSolr:: Post.execute(Solr::Request:: Delete.new(:query => %{type_s:Model AND id:"Model:110809"}))
ActsAsSolr:: Post.execute(Solr::Request::Commit.new)

It could’ve been pretty straightforward removing this concerned item from the solr index and then everything would be well.. but its a lot harder than that if you’re looking at over a thousand indexed elements vs their ‘existing’ counterparts in the database! Finding the exact data to remove was really the hardest part! I never knew this until I took the liberty of helping out our kind Infra Team to resolve the problem. I decided to tweak the solr parser method returning the “out of sync” error. I thought that it would actually be brilliant to just display the concerned element’s id so that they could delete it from the index itself. And so, I had something like this: (in acts_solr/lib/parser_methods.rb)

raise "Out of sync! Found #{ids.size} items in index, but only #{things.size} were found in database! Remove #{(ids - (things.collect{|x| x.id})).to_sentence}." unless things.size == ids.size

And yes, viola! I can now see the faulty ids that were causing the “out of sync” problem. I presented this not-so-brilliant solution to our Infra Team, and they came up with a better idea. My colleague thought that it would be nicer if I could just do away with the “out of sync” error altogether. Since I can already pinpoint the cause of the trouble, then why not remove it for good? I came up with half the solution. It was the quicker one to implement and didn’t require much from their end either.

Distinguishing the faulty id from the list of objects from solr vs those that were from the db, it paved the way for me to simply remove these ids from the checking. It was half the solution because (hint, hint.. I may be doing this next time when I have time) I could actually delete the certain indexed element from solr instead of simply removing it from solr’s items on hand. This “full” solution could actually bring forth other complications since you’d have to deal with what models were concerned and what fields will solr need to look at, etc.

And so.. the half solution that I did was to clean up the elements on hand for solr. This snippet is found in acts_as_solr/lib/parser_methods.rb.

 def reorder(things, ids)
    ordered_things = Array.new(things.size)

    unless things.size == ids.size
      (ids - (things.collect{|x| x.id})).collect{|missing| ids[ids.index(missing)] = nil}
      ids = ids.compact
    end

    raise "Out of sync! Found #{ids.size} items in index, but only #{things.size} were found in database! Remove #{(ids - (things.collect{|x| x.id})).to_sentence}." unless things.size == ids.size

    things.each do |thing|
      position = ids.index(thing.id)
      ordered_things[position] = thing
    end

    ordered_things
  end

The first four lines above the “out of sync” message is what is critical. It will attempt to remove the missing object from the items that solr will return. If all else fails, then it will be displaying the “out of sync” error, but would still be displaying the ids that were causing the problem.

Its quick, but not dirty. It works, but will not really guarantee that your problem will go away permanently. I suggest you do a complete reindex of your whole data. Or better yet, whatever was causing it, just make sure that there are no direct database deletion of any data so that solr will always remain in sync with your database.

Also there is an alternative option if you dont like the above.
Why just don’t MySQL decide your ordering.

A small snippet you need to change in

acts_as_solr/lib/parser_methods.rb

 def find_objects(ids, options, configuration)
      result = if configuration[:lazy] && configuration[:format] != :ids
        ids.collect {|id| ActsAsSolr::LazyDocument.new(id, self)}
      elsif configuration[:format] == : objects
        conditions = [ "#{self.table_name}.#{primary_key} in (?)", ids ]
        find_options = {:conditions => conditions}
        find_options[:include] = options[:include] if options[:include]
        if self.connection.adapter_name =~ /mysql/i
          find_options[:order] = "FIELD(#{self.table_name}.#{primary_key}, #{ids.join(',')})"
          result = self.find(:all, find_options)
        else
          result = reorder(self.find(:all, find_options), ids)
        end
      else
        ids
      end

      result
    end

In the above method it will check if the adapter is mysql then it will fetch the latest result list from the DB which will not cause the “Out Of Sync” issue.

Hope this helps.

Run acts_as_solr in JRuby in background mode

Posted by Bhushan G Ahire | Posted in JRuby, Rails, ruby | Posted on 12-07-2010

0

When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process. However in JRuby this is disabled by default due to concurrency issues. And this prevents your solr rake tasks from running under a JRuby environment…

The Problem

When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process.
However in JRuby this is disabled by default due to concurrency issues. There is an option to enabling fork (jruby -J-Djruby.fork.enabled=true)
within JRuby but it is experimental and as the warning says, “WARNING: fork is highly unlikely to be safe or stable on the JVM.” as it can cause all sorts of weird and wonderful side-effects.

The Solution

The solution therefore is to simply alter the solr:start and solr:stop tasks to use Kernel.exec instead.
To do this find the solr rake tasks usually in {RAILS_ROOT}/vendor/acts_as_solr/lib/tasks/solr.rake. In the start task all that is required is to comment out the start of the fork block. so you only have the exec method call,
This will not start the solr process in the background. To do so you just have to add “&” at the end of the exec command, which causes the jar file to run in background mode.

task :start do
      require "#{File.dirname(__FILE__)}/../../config/solr_environment.rb"
      begin
        n = Net::HTTP.new('127.0.0.1', SOLR_PORT)
        n.request_head('/').value

      rescue Net::HTTPServerException #responding
        puts "Port #{SOLR_PORT} in use" and return

      rescue Errno::ECONNREFUSED #not responding
        Dir.chdir(SOLR_PATH) do
            exec "java #{SOLR_JVM_OPTIONS}
-Dsolr.data.dir=#{SOLR_DATA_PATH} -Djetty.logs=#{SOLR_LOGS_PATH}
-Djetty.port=#{SOLR_PORT} -jar start.jar &"
          sleep(5)
          File.open("#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid", "w"){ |f| f << pid}
          puts "#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_PORT}, pid: #{pid}."
        end
      end
    end

and in the end task just comment out the begining of the fork block so you are left with

  task :stop do
    require "#{File.dirname(__FILE__)}/../../config/solr_environment.rb"
      file_path = "#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid"
      if File.exists?(file_path)
        File.open(file_path, "r") do |f|
          pid = f.readline
          Process.kill('TERM', pid.to_i)
        end
        File.unlink(file_path)
        Rake::Task["solr:destroy_index"].invoke if ENV['RAILS_ENV'] == 'test'
        puts "Solr shutdown successfully."
      else
        puts "PID file not found at #{file_path}. Either Solr is not running or no PID file was written."
      end
  end

you should now be able to run your solr rake tasks under jruby with out any problems..

Ruby Mixin Tutorial

Posted by Bhushan G Ahire | Posted in ruby | Posted on 03-06-2010

0

In Java you just have classes (both abstract and concrete) and interfaces. The Ruby language provides classes, modules, and a mix of both. In this post I want to dive into mixins in Ruby.

In the Ruby language a mixin is a class that is mixed with a module. In other words the implementation of the class and module are joined, intertwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class. Before we get started with the mixin examples let me first explain what a module is.

I think of a module as a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but a module can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and seperate classes either at the class or instance level.

Let me define a module, albeit a trivial one, that would convert a numeric integer value to English.

# Convert a integer value to English.
module Stringify
  # Requires an instance variable @value
  def stringify
    if @value == 1
      "One"
    elsif @value == 2
      "Two"
    elsif @value == 3
      "Three"
    end
  end
end

Note that the Stringify module makes use of a @value instance variable. The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it. In addition to instance variables a module could invoke methods defined not in the module itself but in the class that it will be mixed with.

Now let me construct a self contained module that is not dependent on the implementation of any class that it can be mixed with.

# A Math module akin to Java Math class.
module Math
  # Could be called as a class, static, method
  def add(val_one, val_two)
    BigInteger.new(val_one + val_two)
  end
end

The methods in the Math module are intended to be invoked like class methods, also known as static methods. The add method in the Math module accepts two integer values and returns an instance of BigInteger. Let me now define the mixin BigInteger class.

[sourcecode launguage="ruby"]
# Base Number class
class Number
def intValue
@value
end
end

# BigInteger extends Number
class BigInteger < Number

# Add instance methods from Stringify
include Stringify

# Add class methods from Math
extend Math

# Add a constructor with one parameter
def initialize(value)
@value = value
end
end
[/sourcecode]

I loosely modeled the BigInteger and Number classes after the Java versions. The BigInteger class defines one constructor and directly inherits one method from the Number base class. To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.

[sourcecode launguage="ruby"]
# Create a new object
bigint1 = BigInteger.new(10)
# Call a method inherited from the base class
puts bigint1.intValue # –> 10
[/sourcecode]

The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.

[sourcecode launguage="ruby"]
# Call class method extended from Math
bigint2 = BigInteger.add(-2, 4)
puts bigint2.intValue # –> 2
[/sourcecode]

The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods. The method defined in the Stringify module can be used as an instance method.

[sourcecode launguage="ruby"]
# Call a method included from Stringify
puts bigint2.stringify # –> ‘Two’
[/sourcecode]

There is another use of the extend method. You can enhance an object instance by mixing it with a module at run time! This is a powerful feature. Let me create a module that will be used to extend an object, changing it’s responsibilities at runtime.

[sourcecode launguage="ruby"]
# Format a numeric value as a currency
module CurrencyFormatter
def format
"$#{@value}"
end
end
[/sourcecode]

To mix an object instance with a module you can do the following:

[sourcecode launguage="ruby"]
# Add the module methods to
# this object instance, only!
bigint2.extend CurrencyFormatter
puts bigint2.format # –> ‘$2′
[/sourcecode]

Calling the extend method on an an instance will only extend that one object, objects of the same class will not be extended with the new functionality.

[sourcecode launguage="ruby"]
puts bigint1.format # will generate an error
[/sourcecode]

Modules that will be mixed with a class via the include or extend method could define something like a contructor or initializer method to the module. The module initializer method will be invoked at the time the module is mixed with a class. When a class extends a module the module’s self.extended method will be invoked:

[sourcecode launguage="ruby"]
module Math
def self.extended(base)
# Initialize module.
end
end
[/sourcecode]

The self prefix indicates that the method is a static module level method. The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.

When a class includes a module the module’s self.included method will be invoked.

[sourcecode launguage="ruby"]
module Stringify
def self.included(base)
# Initialize module.
end
end
[/sourcecode]

The base parameter will be a class object for the class that includes the module.

It is important to note that inside the included and extended initializer methods you can include and extend other modules, here is an example of that:

[sourcecode launguage="ruby"]
module Stringify
def self.included(base)
base.extend SomeOtherModule
end
end
[/sourcecode]

Paginating multiple models using will_paginate on the same page

Posted by Bhushan G Ahire | Posted in Rails, ruby | Posted on 13-04-2010

0

The will_paginate plugin makes pagination for your models in Ruby on Rails ridiculously simple. However sometimes you’ll find yourself wanting to paginate over two or more models on a single page. For instance, you might want to display a list of users and administrators on a single page along with a pager for each model (assuming that users and administrators are stored in separate tables).

Controller code

The code is pretty simple, except that I am specifying the page to show to be equal to params[:user_page] and params[:administrator_page] respectively. Since we are allowing the ability to page over two models, we need two separate parameters to determine which page of users or administrators to show.

@users = User.paginate(:page =&gt; params[:user_page], :per_page =&gt; 10)&lt;br/&gt;
@administrators = Administrator.paginate(:page =&gt; params[:administrator_page], :per_page =&gt; 10)

View code

In the view all we need to do is make sure to set the param_value to the correct value to indicate to the plugin that we want to use a different parameter name for the page. The default is simply called ‘page’, but we need to make sure to use ‘user_page’ and ‘administrator_page’ instead for the two different models.

&lt;%= will_paginate @users, :param_name =&gt; 'user_page' %&gt;&lt;br/&gt;
&lt;%= will_paginate @administrators, :param_name =&gt; 'administrator_page' %&gt;

That’s it, you should now be able to page through your users and administrators on the same page.

List of useful rake tasks for Rails…

Posted by Bhushan G Ahire | Posted in Rails, ruby | Posted on 25-03-2010

0

rake cache:clear
# Clears all cached pages
rake db:bootstrap
# Loads a schema.rb file into the database and then loads the initial database fixtures.
rake db:bootstrap:copy_default_theme
# Copy default theme to site theme
rake db:migrate
# Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
rake db:schema:dump
# Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load
# Load a schema.rb file into the database
rake db:bootstrap:load
# Load initial database fixtures (in db/bootstrap/*.yml) into the current environment's database.  Load specific fixtures using FIXTURES=x,y
rake db:fixtures:load
# Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y
rake db:sessions:clear
# Clear the sessions table
rake db:sessions:create
# Creates a sessions table for use with CGI::Session::ActiveRecordStore
rake db:structure:dump
# Dump the database structure to a SQL file
rake db:test:clone
# Recreate the test database from the current environment's database schema
rake db:test:clone_structure
# Recreate the test databases from the development structure
rake db:test:prepare
# Prepare the test database and load the schema
rake db:test:purge
# Empty the test database
rake deploy
# Push the latest revision into production using the release manager
rake diff_from_last_deploy
# Describe the differences between HEAD and the last production release
rake doc:app
# Build the app HTML Files
rake doc:clobber_app
# Remove rdoc products
rake doc:clobber_plugins
# Remove plugin documentation
rake doc:clobber_rails
# Remove rdoc products
rake doc:plugins
# Generate documation for all installed plugins
rake doc:rails
# Build the rails HTML Files
rake doc:reapp
# Force a rebuild of the RDOC files
rake doc:rerails
# Force a rebuild of the RDOC files
rake edge
# freeze rails edge
rake log:clear
# Truncates all *.log files in log/ to zero bytes
rake rails:freeze:edge
# Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)
rake rails:freeze:gems
# Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:unfreeze
# Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:update
# Update both configs, scripts and public/javascripts from Rails
rake rails:update:configs
# Update config/boot.rb from your current rails install
rake rails:update:javascripts
# Update your javascripts from your current rails install
rake rails:update:scripts
# Add new scripts to the application script/ directory
rake remote_exec
# Execute a specific action using the release manager
rake rollback
# Rollback to the release before the current release in production
rake show_deploy_tasks
# Enumerate all available deployment tasks
rake stats
# Report code statistics (KLOCs, etc) from the application
rake test
# Test all units and functionals
rake test:functionals                 
# Run tests for functionalsdb:test:prepare
rake test:integration
# Run tests for integrationdb:test:prepare
rake test:plugins                     
# Run tests for pluginsenvironment
rake test:recent
# Run tests for recentdb:test:prepare
rake test:uncommitted                 
# Run tests for uncommitteddb:test:prepare
rake test:units
# Run tests for unitsdb:test:prepare
rake tmp:cache:clear
# Clears all files and directories in tmp/cache
rake tmp:clear                        
# Clear session, cache, and socket files from tmp/
rake tmp:create                       
# Creates tmp directories for sessions, cache, and sockets
rake tmp:pids:clear                   
# Clears all files in tmp/pids
rake tmp:sessions:clear               
# Clears all files in tmp/sessions
rake tmp:sockets:clear
# Clears all files in tmp/sockets
rake update_dialog_helper
# Copies the latest dialog.js to the application's public directory

Setup Capistrano to deploy Rails application on Amazon EC2 with Git

Posted by Bhushan G Ahire | Posted in capistrano, git, Rails, ruby, Security, Subversion | Posted on 17-02-2010

0

1: Create a new Rails app – we’ll call is ‘deploytest’

$ rails deploytest
$ cd deploytest

2: Create a local Git repository for it

$ git init
$ git add *
$ git commit -a -m 'initial commit'
$ git status

3: Create a couple of Capistrano files

$ capify .

4: Edit config/deploy.rb

# The name of your app
set :application, "deploytest"
# The directory on the EC2 node that will be deployed to
set :deploy_to, "/var/www/apps/#{application}"
# The type of Source Code Management system you are using
set :scm, :git
# The location of the LOCAL repository relative to the current app
set :repository,  "."
# The way in which files will be transferred from repository to remote host
# If you were using a hosted github repository this would be slightly different
set :deploy_via, :copy

# The address of the remote host on EC2 (the Public DNS address)
set :location, "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
# setup some Capistrano roles
role :app, location
role :web, location
role :db,  location, :primary => true

# Set up SSH so it can connect to the EC2 node - assumes your SSH key is in ~/.ssh/id_rsa
set :user, "root"
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]

The only account on a default EC2 instance is root. You probably want to create a second user that is responsible for your application.

5: Copy your SSH public key to your EC2 node

$ scp -i ~/my-ec2-keypair ~/.ssh/id_rsa.pub root@ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:/root/.ssh/authorized_keys2

NOTE the filename authorized_keys2 – not authorized_keys!!

6: Setup the EC2 node for Capistrano deployment.
From your LOCAL machine, not the EC2 node:

$ cap deploy:setup

7: Finally, deploy your application

$ cap deploy

You will see lots of output and with this dummy application some of those will report errors/warnings. Don’t worry about that for now.

8: Check that the Deployment was successful
Connect to the EC2 node with SSH the regular way, cd to the app directory and check that everything is there. If that is all working then you are ready to deploy a real application and add custom tasks for managing the database, restarting the server etc.

Bear in mind that Capistrano add new ‘releases’ of your software in separate directories and symlinks the ‘current’ directory to the latest. So the root of your deployed application is the ‘current’ subdirectory.

Hope this will help you setting up your ec2 instance with capistrano.

Send SMS from Ruby On Rails application using web service, SOAP API

Posted by Bhushan G Ahire | Posted in Rails, ruby | Posted on 15-01-2010

0

SOAP4R is a Ruby library for accessing Web Services via SOAP. Recently I had a chance to explore SOAP4R. Here’s how to get started with it.

Installation

Although Ruby 1.8.x comes with SOAP4R in its standard library, it is an old, buggy version. I highly recommend using the latest gem (1.5.8 as of the this update). It has one dependency, httpclient.

gem install soap4r --include-dependencies

Service

There are many services available to send SMS but I prefer to use,

MailServe-SMS

MailServe-SMS, text messaging service, is everything you need for fast, no-frills, no-fuss text messaging. A quick and easy way to send SMS.

Let’s explore these further.

Method 1: Read the WSDL at run-time

 

require "soap/wsdlDriver"
wsdl = "http://sms.qlc.co.in/smsapi.wsdl"
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_drive

 

A single call to a driver factory reads the WSDL file, and creates a driver class for you to use, complete with the methods defined by the service. What if your service requires authentication? The driver inherits methods from httpclient, so you can specify its options as you would for httpclient:

Once driver is get initialised you need to call SMS sending API i.e. SendSMSRequest.

driver.SendSMSRequest("username", "password", "sender_no", "from_no", "message")

 

Once This will return you response 200 SMS sent successfully on success else if the information submitted was wrong then 500 Information submitted was incomplete.

 

Method 2: Generate classes from WSDL

SOAP4R installs a command-line utility called ‘wsdl2ruby’ which can generate a client or server.

Coming soon…..

Get location from IP address in Ruby On Rails for free….

Posted by Bhushan Ahire | Posted in Rails, ruby, Security | Posted on 20-05-2009

0

Find below the code for finding location from IP address using IP location tools.

require 'net/http'
require 'rexml/document'
include REXML

class MapsController < ApplicationController
	def index
		@location = locateIp()

	end

	def locateIp
		ip = request.remote_ip
		ips = ip.to_s
		url = "http://iplocationtools.com/ip_query.php?ip="+ips

		xml_data = Net::HTTP.get_response(URI.parse(url)).body

                xmldoc = REXML::Document.new(xml_data)

		# Now get the root element
		root = xmldoc.root
		city = ""
		regionName = ""
		countryName = ""

		# This will take country name...
		xmldoc.elements.each("Response/CountryName") {
		|e| countryName << e.text
	    }

		# Now get city name...
		xmldoc.elements.each("Response/City") {
   		|e| city << e.text
	    }

		# This will take regionName...
		xmldoc.elements.each("Response/RegionName") {
   		|e| regionName << e.text
	    }

     	ipLocation = city +", "+regionName+", "+countryName

	 return ipLocation
   end #end of method locateIp

end

Generating ZIP files via Ruby on Rails using rubyzip

Posted by Bhushan Ahire | Posted in Rails, ruby | Posted on 03-03-2009

2

gem install rubyzip

Then, in the model that I’m using to generate the zip bundles, I add a couple “require” statements:

require 'zip/zip'
require 'zip/zipfilesystem'

class Album < ActiveRecord::Base
  (...)
end

Next, I added a class method called bundle, which when called will use rubygem to generate the zip file. Note: the “permalink” attributes of Album and Artist are populated when an object of those models is created. I’m using them because it makes for nice filenames, too.

# create a zipped archive file of all the tracks in an album
def bundle(name = self.permalink, set = self.artist.permalink)
   bundle_filename = "#{RAILS_ROOT}/public/uploads/#{set}-#{name}.zip"

   # check to see if the file exists already, and if it does, delete it.
   if File.file?(bundle_filename)
     File.delete(bundle_filename)
   end

   # set the bundle_filename attribute of this object
   self.bundle_filename = "/uploads/#{set}-#{name}.zip"

   # open or create the zip file
   Zip::ZipFile.open(bundle_filename, Zip::ZipFile::CREATE) {
     |zipfile|
     # collect the album's tracks
     self.tracks.collect {
       |track|
         # add each track to the archive, names using the track's attributes
         zipfile.add( "#{set}/#{track.num}-#{track.filename}", "#{RAILS_ROOT}/public#{track.public_filename}")
       }
   }

   # set read permissions on the file
   File.chmod(0644, bundle_filename)

   # save the object
   self.save
end

Next I added a method in my controller:

def create_bundle
   album = Album.find(params[:id])
   album.bundle
   flash[:notice] = 'Album was successfully zipped.'
   redirect_to album_url(album.artist, album)
end

And edit my routes.rb accordingly:

map.create_bundle 'create_bundle/:id', :controller => 'albums', :action => 'create_bundle'

Now it’s just a matter of creating a link in the view for the admin to click whenever he/she wants to generate the zip file:

<%= link_to('Create Album Zip', create_bundle_path(@album)) %>

…and a link for the user to click to download the zip file if it exists:

<% unless @album.bundle_filename.nil? %>
<%= link_to "Download Album Zip", @album.bundle_filename %>
<% end %>

That’s it! Refer to the rubyzip documentation for more info.