Project

has_url

0.0
No commit activity in last 3 years
No release in over 3 years
Adds validations on URL attributes for ActiveRecord models. URL scheme will be checked and added before updating the database if necessary. A raw_attribute method will also be added to display the url without HTTP(S) scheme or trailing slash.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 3.0.0

Runtime

>= 3.0.0
>= 2.2.0
 Project Readme

HasUrl¶ ↑

Adds validations on URL attributes for ActiveRecord models. URL scheme will be checked and added before updating the database if necessary. A raw_#{attribute} method will also be added to display the url without HTTP(S) scheme or trailing slash.

Usage¶ ↑

In your AR model:

has_url :attribute, options

Options :

  • valid_schemes : Array of schemes or single scheme accepted for the attribute. Default [‘http’, ‘https’]

  • default_scheme : Scheme appended to the attribute if none found (supply the scheme string without ‘://’). Default ‘http’

  • message : Provide a error string or symbol (which will be translated, see ActiveRecord::Errors#generate_message). Default :invalid

Example¶ ↑

class Person < ActiveRecord::Base
  has_url :blog_link
end

person.blog_link = 'example.com'
person.save
person.blog_link #=> 'http://example.com/'
person.raw_blog_link #=> 'example.com'

class Person < ActiveRecord::Base
  has_url [:blog_link, :website_url], :default_scheme => 'https', :valid_schemes => ['http', 'https', 'ftp']
end

Requirements¶ ↑

  • Addressable for URI parsing

TODO¶ ↑

  • Refactor using ActiveModel::Validator

  • More tests

  • Improve the raw_ method stripping, and have it accept an option to specify which schemes to strip (schemes array or boolean to strip all valid schemes)

  • Add a shoulda macro (should_have_url)

  • Check existence of the given attributes ?

  • Optionally test the url validity with an HTTP request ?

Notes and Copyright¶ ↑

This plugin was inspired by the url_validation and validates_url_of plugins.

Copyright © 2012 Pickabee. Released under the MIT licence, see LICENSE for details.