public
Description: A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters "http://" or not
Homepage:
Clone URL: git://github.com/paulca/url_field.git
name age message
file .gitignore Sat Jun 06 17:17:04 -0700 2009 ignore the log [paulca]
file README.textile Sun Jun 07 05:01:49 -0700 2009 update readme to reflect dynamic cleaned_ method [paulca]
file Rakefile Mon Oct 05 04:11:31 -0700 2009 gemspec [Paul Campbell]
directory autotest/ Sat Jun 06 13:10:00 -0700 2009 all for one, one for all, one plugin in one commit [paulca]
file init.rb Sat Jun 06 13:11:27 -0700 2009 actually include the plugin [paulca]
directory lib/ Mon Oct 05 04:11:31 -0700 2009 gemspec [Paul Campbell]
directory spec/ Mon Oct 05 04:11:31 -0700 2009 gemspec [Paul Campbell]
file url_field.gemspec Mon Oct 05 04:12:15 -0700 2009 add encoding to gemspec [Paul Campbell]
README.textile

URL field

Sometimes you want to accept a URL field on one of your models. Take for example a Company model with a website field. You want the URL to always have “http://” but sometimes users enter http://, sometimes they dont.

Your app shouldn’t care. Enter the URL field plugin.

This simple plugin allows you to enter either “http://www.example.com” or just “www.example.com” and in either case it will store “http://www.example.com”

Usage

class Company < ActiveRecord::Base
  url_field :website
end

@company = Company.new
@company.website = "www.example.com"
@company.save
@company.website # => "http://www.example.com"

Https? That works too (but one way only)

@company.website = "https://www.example.com"
@company.save
@company.website # => "https://www.example.com"

Extra

If you want access to the correctly formatted URL at any time (for example if you’re passing it to URI.parse, before you save your model), you can prefix your URL field method name with “cleaned_” eg. “cleaned_website” if your field name was website:

class Company < ActiveRecord::Base
  url_field :website
end

@company = Company.new
@company.website = "www.example.com"
@company.cleaned_website # => "http://www.example.com"

Install

Easy!

./script plugin install git://github.com/paulca/url_field.git

About me

I’m Paul Campbell. I’m an avid Ruby on Rails web developer. Follow my ramblings at http://www.pabcas.com

Follow me on Twitter http://twitter.com/paulca

Copyright © 2009 Paul Campbell, released under the MIT license