Skip to content
accelerando

Tag Archives: Rails

Different day, same shit: MySQL Gem again

11-Dec-08

Again, the MySQL ruby gem totally annyoed me trying to install it on a fresh Mac OS X 10.5.5 install and MySQL 5.0.67.

This time the following command brought it to life:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
  --with-mysql-include=/usr/local/mysql/include

Thanks to a bitter software engineer.

Rails 2.1: send_file :x_sendfile => true

05-Jun-08

The “x_sendfile” argument on the send_file method in Rails 2.1 is not well thought off as it has an impact in development mode also. I guess most Rails coders won’t have Apache proxying their mongrels in dev mode and so they don’t get to see any images or files but the plain path information.

I’ll guess i stay with the x_send_file solution as described here.

PDF::Writer and Ruby on Rails 2.1

04-Jun-08

Some days ago, Ruby On Rails 2.1 saw the light of day and as usual, i eagerly updated my Daily Fratze project.

I had some minor problems due to an old version of will_paginate and some major ones with my use of PDF::Writer.

The PDF::Writer library still works very well but the the instructions here (under PDF::Writer (Austin Ziegler) on how the register an “rpdf” template handler don’t apply anymore due to changes in the Rails template system but can easily be fixed:

In environment.rb change

ActionView::Base.register_template_handler 'rpdf', ActionView::PDFRender

to

ActionView::Template.register_template_handler 'rpdf', ActionView::PDFRender

And also change your ActionView::PDFRender class to:

module ActionView # :nodoc:
  require 'pdf/writer'
  class PDFRender
    PAPER = 'A4'
    include ApplicationHelper
    include ActionView::Helpers::AssetTagHelper
    include ActionView::Helpers::TextHelper      
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::UrlHelper
 
    def initialize(action_view)
      @action_view = action_view
    end
 
    # Render the PDF
    def render(template, local_assigns = {})
      @action_view.controller.headers["Content-Type"] ||= 'application/pdf'
 
      # Retrieve controller variables
      @action_view.controller.instance_variables.each do |v|
        instance_variable_set(v, @action_view.controller.instance_variable_get(v))
      end
 
      pdf = ::PDF::Writer.new( :paper => PAPER )
      pdf.compressed = true if RAILS_ENV != 'development'
      eval template.source, nil, "#{@action_view.base_path}/#{@action_view.first_render}.#{@action_view.finder.pick_template_extension(@action_view.first_render)}" 
 
      pdf.render
    end
 
    def self.compilable?
      false
    end
 
    def compilable?
      self.class.compilable?
    end
  end
end

And enjoy happy PDF generation ;)

Turning off x_send_file in development mode

23-Jan-08

I just discovered the great x_send_file plugin and technique and use it extensivly for daily fratze. I replaced the majority of send_file calls with x_send_file but not all (the in memory thingies i serve cannot be send through apache). This works great in production mode but in development mode, it fails as there is no apache sitting in front the mongrels. Therefore i added the following to my $app_home/config/environments/development.rb:

class ActionController::Base
  def x_send_file(path, options = {})
    send_file(path, options)
  end
end

So all calls to x_send_file in dev mode are delegated to the original send_file.

If anyone can present me a cleaner solution, i.e. with method aliasing, feel free to drop a comment.

Using rubyzip to create zip files on the fly

21-Jan-08

In my Daily Fratze project the users should be able to download their faces as a zip file backup.

Until now they have been able to upload zip files. For that, i used rubyzip which worked quite well.

As a starting point i found a nice article on the joy of rubyzip, but this has a major flaw for me. It uses the Zip::ZipFile interface to create its archives. This interfaces takes a filename as parameter and either creates this file if it doesn’t exists or tries to open it as a zip archive.

I doesn’t want my directories polluted by some random zip files so i tried to use TempFile. Creating a new TempFile leads to an existing file which Zip::ZipFile cannot open.

My solution uses the more basic interface Zip::ZipOutputStream. Further requirements were adding binary files with arbitrary names and not like in the examples of rubyzip, creating new files with some textual content. Here we go:

require 'zip/zip'
require 'zip/zipfilesystem'
 
t = Tempfile.new("some-weird-temp-file-basename-#{request.remote_ip}")
# Give the path of the temp file to the zip outputstream, it won't try to open it as an archive.
Zip::ZipOutputStream.open(t.path) do |zos|
  some_file_list.each do |file|
    # Create a new entry with some arbitrary name
    zos.put_next_entry("some-funny-name.jpg")
    # Add the contents of the file, don't read the stuff linewise if its binary, instead use direct IO
    zos.print IO.read(file.path)
  end
end
# End of the block  automatically closes the file.
# Send it using the right mime type, with a download window and some nice file name.
send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => "some-brilliant-file-name.zip"
# The temp file will be deleted some time...
t.close

Edit: The basename given to Tempfile.new is what the name says: A basename. It doesn’t need to denote a full path. Tempfile creates an arbitrary path for you in the default temporary directory.

Close
E-mail It