0.0
No commit activity in last 3 years
No release in over 3 years
ActiveModel field serializer for url fields
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.2.0
 Project Readme

UrlAttribute Build Status

ActiveModel attribute serialization for urls.

Usage

Assuming you have a column named url on the users table, you can easily add url handling with:

class User < ActiveRecord::Base
  url_attribute :url
end

user = User.new({
  url: "chingr.com"
})

user.url.class
=> UrlAttribute::NormalizedUrl

user.url.to_s
=> "http://chingr.com"

user.url = nil
user.url
=> nil

You can also specify a column to ignore url normalization.

class User < ActiveRecord::Base
  url_attribute :stem, normalize: false
end

user = User.create({
  name: "Jeff",
  stem: "chingr.com"
})
user.stem.to_s
=> "chingr.com"

Validating

UrlAttribute provides validation to ActiveModel. To use:

class User < ActiveRecord::Base
  url_attribute :url
  validates :url, url: { 
    allow_blank: true,
    message: "is invalid"
  }
end

user = User.new({
  name: "Jeff",
  url: "some invalid url"
})

user.save
=> false

user.errors.full_messages
=> ["Url is invalid"]

License

This project rocks and uses MIT-LICENSE.