Tuesday, November 9, 2010

Internet Explorer empty element headache - a quick fix with jQuery

Internet Explorer 7 doesn't render an empty element right, when this element has some layout features (for example, background). The element is supposed to be rendered as it were non existent on the page, instead we have an annoying box with non-zero dimensions. It's frustrating when you have on your page a message box for potential error and success messages, and when there are no messages, you still see it in IE.

The solution is to hide the box when there is no text in it.

$j(".message-box").each(function() {
if(jQuery.trim($j(this).text()).length == 0) {
$j(this).hide();
}
});


If the page is rendered with a message, then the box is shown. If there is a message rendered over AJAX, then we rerender the whole message box and it's visible originally.

"Show more" - hide higher index items when rendering a collection

While rendering a collection, I wanted to hide some items with higher indexes (and show them when clicking on "Show more"). But there is no way to tell the index when rendering :collection => @items. Then I came across this posting:

Render partial with collection has hidden counter

Now, when rendering
<ul>
<%= render :partial => 'item', :collection => @items %>
</ul>


I can have the partial where only 3 items are shown:
<li style="<%= (item_counter + 1 > 3) ? 'display:none' : '')">item.name</li>

Monday, July 19, 2010

Another incarnation of RFPDF, inserting jpeg with Image Science

I was using a 2006 version of rfpdf with tcpdf support (I don't remember where I got it). It was the only working version I was able to find.

Once I tried to insert images into a pdf document, I discovered that it doesn't work with jpg. The function GetImageSize was called but didn't exist.

I searched the internet and it'd been already fixed. The problem was that those couple of versions of rfpdf either didn't work or didn't exist in the repository.

The solutions were here
and here (specific for ImageScience).

As it translated to my own code, I tuned it for ImageScience and fixed all typos and bugs in the code:

1. Add the following lines to the plugin rfpdf/environment.rb
require 'image_science'
require "#{File.dirname __FILE__}/lib/core/image_science"


2. Add file rfpdf/lib/core/image_science.rb
module RFPDF

# http://uk2.php.net/getimagesize
def getimagesize(filename)
out = Hash.new

ImageScience.with_image(filename) do |img|
out[0] = img.width
out[1] = img.height
out[2] = img.instance_values["file_type"]
out[3] = "height=\"#{img.height}\" width=\"#{img.width}\""
end

out
end

end


3. Fix typos in rfpdf/lib/tcpdf.rb

Thursday, March 25, 2010

Capistrano deploy_via copy from git+windows

I had a problem deploying with Capistrano from a git repository (both local and remote depositories equally didn't work) while on a Windows machine. The git version was probably 1.5 and Capistrano versions used were 2.4.* and 2.5.*

My capistrano settings were the following (besides the usual stuff):

set :deploy_via, :copy
set :copy_dir, "C:/caches/myapp"


I was running:

C:\myapp>cap deploy
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
executing locally: "git ls-remote ssh://git@myhost.com:myport/~/myapp HEAD"
* getting (via checkout) revision b6f590f2d59b3f6ae14e1f5278512f7f1b4edd1a to
C:/caches/myhost.com/20100312233007
executing locally: git clone -q ssh://git@myhost.com:myport/~/myapp C:\caches\myhost.com\20100312233007 &amp;&amp; cd /D C:\caches\myapp.com\20100312233007 &amp;&amp; git checkout -q -b deploy b6f590f2d59b3f6ae14e1f5278512f7f1b4edd1a
warning: Remote branch deploy not found in upstream origin, using HEAD instead
* processing exclusions...
*** [deploy:update_code] rolling back

....

(and a whole bunch of errors)



The command "git clone" failed to clone the code from the repository to the copy folder but copied it to my current folder from which I ran the command instead. After I switched to the latest version of git (1.7), it started to crash with a "Too many arguments" error. When I ran the command manually from the command line, everything was fine.



It seems that in the context of Ruby the "git clone" command is greedy, perceiving the commands chained with && as it's destination folder, and it fails because this "destination folder" doesn't look valid.

I had to put a hack to Capistrano to make "git clone" execute separately.

In \lib\ruby\gems\1.8\gems\capistrano-2.5.18\lib\capistrano\recipes\deploy\strategy\base.rb after the line 53 (cmd = cmd.split...)
add the following lines:

if cmd =~ /\s\&\&\s/ && cmd =~ /^git\s+clone/
cmd1, cmd = cmd.split(" && ", 2)
super(cmd1)
end


I don't know if it would work for all possible scenarios, but it fixed my case. After that the application deployed just fine.