github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

paulca / url_field

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 9
    • 2
  • Source
  • Commits
  • Network (2)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters "http://" or not — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

use before_validation instead 
Paul Campbell (author)
Thu Dec 03 16:07:28 -0800 2009
commit  bc9a65af626538b73a4e471a387c82037d346e59
tree    1cc94034e1d9f195532c43f0267f5d0db4b61b45
parent  9b15adefc9aaeb2b1fba16057bdb3a0f21cfa658
url_field /
name age
history
message
file .gitignore Thu Dec 03 16:02:56 -0800 2009 update gem settings [Paul Campbell]
file CHANGELOG Thu Dec 03 16:02:56 -0800 2009 update gem settings [Paul Campbell]
file README.textile Thu Dec 03 15:56:08 -0800 2009 update readme [Paul Campbell]
file Rakefile Thu Dec 03 16:02:56 -0800 2009 update gem settings [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/ Thu Dec 03 16:07:28 -0800 2009 use before_validation instead [Paul Campbell]
directory rails/ Thu Dec 03 15:53:16 -0800 2009 add support for multiple fields [Paul Campbell]
directory spec/ Thu Dec 03 15:54:40 -0800 2009 update the specs [Paul Campbell]
file url_field.gemspec Thu Dec 03 16:02:56 -0800 2009 update gem settings [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

Multiple fields

If you have multiple url_fields in a single model, just pass them as arguments to the url_field method, eg:

class Company < ActiveRecord::Base
  url_field :website, :support_website, :more_info_website
end

Access the correctly formed URL at any time

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

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server