Formatting Text

For text fields one typically wants to allow some HTML tags, add support for link breaks and even auto link URL’s and emails.

This method which I got from this forum thread, Adding a line break … safely, does all of this and is a must add to my application_helper.rb.

def format_content(content)
  # allow only tags specified in tags options, likewise in attributes
  content = sanitize(content, :tags => %w(b strong i em img), :attributes => %w(src))

  # add support for line breaks
  content = simple_format(content)

  # auto link URL's and emails
  content = auto_link(content, :all, :target => '_blank')

  return content
end

rake db:migrate hangs

Recently I tried running my RoR applications on Oracle.

I noticed that when I ran rake db:migrate it would hang. Specifically it would hang during the schema dump phase.

E:\work\sandbox>rake db:migrate --trace -v
(in E:/work/sandbox)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump

I realized later that this was because the schema dump phase is very slow and the database I was using had many other tables from other applications. When I left it running it finally completed after 10 minutes.

You can see the slow progress of the schema dump phase by tailing schema.rb in the db directory.

Ruby on Rails and Oracle

  1. Get the Ruby OCI8 driver.  Download the file that ends with “mswin32.rb” and install like this:
    E:\ruby>ruby ruby-oci8-1.0.3-mswin32.rb
    Copy OCI8.rb to e:/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8
    Copy oci8.rb to e:/ruby/lib/ruby/site_ruby/1.8
    Copy oci8lib.so to e:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt
    OK?
    Enter Yes/No: Yes
    Copying OCI8.rb to e:/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8 ... done
    Copying oci8.rb to e:/ruby/lib/ruby/site_ruby/1.8 ... done
    Copying oci8lib.so to e:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt ... done
    OK

    You can test the driver by running a query using Ruby.

    E:\>ruby -r oci8 -e "OCI8.new('foo','12345','sid').exec(
    'SELECT * from users') do |r| puts r.join(' | ') ; end"
  2. Install the ActiveRecord Oracle adapter.gem
    E:\ruby>install activerecord-oracle-adapter --source http://gems.rubyonrails.org
  3. Update config/database.yml to connect to Oracle
    development:
      adapter: oracle
      database: sid
      username: foo
      password: 12345
      timeout: 5000
  4. Test by doing a rake db:migrate.
  5. Test by running the Ruby on Rails server and making sure there are no errors upon startup.

This article is based on these articles.

Perforce Adding a Directory Tree

This article, Adding a Directory Tree, shows how to add a directory tree to your perforce repository.

  • On UNIX, run:
    find . -type f -print | p4 -x - add

    The above find command will find files, but will not find symlinks. Nevertheless, you can add symlinks to Perforce using a similar method. Be aware, however, that symlinks are read-only entites, and that editing a symlink is not the same as editing the file it references.

    To discover the symlinks that exist in your client workspace and assess whether or not you want to version them, run the following command from your client workspace root:

    find . -type l -print
    
    (That's an "el" as in "l" for link)

    To add symlinks, run:

    find . -type l -print | p4 -x - add -t symlink
  • On Windows, using the MS-DOS command shell, run:
    dir /b /s /a-d | p4 -x - add

smush.it

Image optimization is an art that not many people master. There are many good image editing tools that allow us to get the best visual result for a certain file size but “under the hood” a lot more optimization can be done.

Smushit.com is a service that goes beyond the limitations of Photoshop, Fireworks & Co. It uses image format specific non-lossy image optimization tools to squeeze the last bytes out of your images – without changing their look or visual quality. You’ll get a report of how many bytes you can save by optimizing your images and all the changed images as a single zip for download.

Smush it comes in different flavours:

  • You can upload a bunch of pictures in your browser
  • You can provide us with a list of image urls or
  • You can get a Firefox Extension to optimize the images found on any web page

Saving bytes has never been so easy – you point us in the right direction, and we’ll do the rest for you. A ZIP archive with optimized images will be generated for you.

New from the Yahoo Performance team.

Takes your image(s) and makes their file size as small as possible: converts GIF to PNG8, throws out JPG metadata, etc.  Does not make JPGs more lossy; the results look exactly the same.

Install the Firefox plugin and you can hand it a URL — and when it’s done, it’ll give you a ZIP file of all the images from that URL in reduced form.

Our Travel page, for instance:
Smushed 12.97% or 30.14 KB from the size of your image(s).
I’m actually pretty pleased we are “only” 30KB over.  I’d have thought it’d be more.

Our visitor HIW page:
Smushed 30.48% or 51.16 KB from the size of your image(s).

Browser Security Warnings

This is from my friend, Chris Weekly.

I think it is necessary for us to be precise when we talk about “security popups” as there are many different kinds.

Some of these are always preventable, some are unavoidable in certain scenarios, all vary according to the browser version and its user config.

Anyway here’s a kickstart:

  1. SSL Certificate Warnings (various) – Triggered on HTTPS URL’s on domains with an expired or self-signed certificate. 
  2. Insecure Content Warnings – Triggered on HTTPS URL’s when the page contents embed references to HTTP resources (images, iFrames, stylesheets or scripts).
    This is preventable by proper JSP/taglib usage. Note it is ok for links to use http:// even in https:// pages as they’re not automatically followed.
  3. HTTPS to HTTP Redirection Warnings – Triggered when an HTTPS request triggers a redirect to an HTTP URL.
    This is unavoidable in some scenarios but should be avoided by design whenever possible.
  4. HTTP/HTTPS Switch Alert – Triggered when simply navigating from HTTP to HTTPS or back.
    This is out of our control, but most browsers don’t have this on by default, and users tend to turn this global setting off after seeing it once or twice (on any site) as it’s so common and harmless.
  5. Content not under this site’s control (New) – Apparently resulting from the recent Microsoft security patch.
    I believe this is triggered by scripts which are not on the same domain as the page requested.
    This is most likely to arise w/ 3rd-party tracking pixel-related scripts.  Needs more investigation.

    Update: I may have made an incorrect assumption that it related to recent MS security updates; it might instead be triggered by attempts of javascript on one domain to interact w/ the page on another domain. Which script and whether this is in fact the root cause of #5 is TBD. 

There are others but I think these are the main ones we’ve been dealing with lately.

Thanks,
Chris

Perforce Branching

Perforce branching is pretty simple.

Say you want to create a branch called

//depot/fkim/foo

First you would add it to your client.

//depot/fkim/foo/... //fkim/foo/...

Next you would do an integration from where you wanted to cut the branch.

p4 integrate //depot/work/foo/... //depot/fkim/foo/...

Then you would submit the integration and the branch will be created and updated in your client work space.

p4 submit

NTFS Junctions

Windows NTFS has a nice but relatively unknown feature called NTFS junctions.  It is like hard links in Unix except it is only for directories.  Microsoft has a KB article about it which points to several utilities.  However I have been using Mark Russinovich’s junction tool exclusively.

Using Junction

Usage: [-s] <directory or file name>

-s Recurse subdirectories

If you want to create or delete a junction, use Junction like this:

Usage: [-d] <junction directory> [<junction target>]

To delete a junction specify the -d switch and the junction name.

Here is an example of using junction.  Note that the order of arguments is opposite of the Unix ln command.

> junction foo d:\docs\foo

The above example will create a junction called “foo” in the current directory.  This junction will point to d:\docs\foo.

To do the same in Cygwin:

$ junction foo `cygpath -aw /d/docs/foo`

One tip is to use the DOS dir command.  It will display <JUNCTION> instead of <DIR> in directory listings for junctions.