0.0
No commit activity in last 3 years
No release in over 3 years
Validate and sanitize URLs in ActiveRecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0
>= 3.0.0
~> 1.3.10

Runtime

>= 4.0.0
 Project Readme

URLAttributes

A small and simple gem for validating URL attributes in your ActiveRecord models.

Installation

Add the gem to your Gemfile

gem 'url_attributes'

And run bundle install.

Usage

Add a string column to your ActiveRecord model which will contain a url, e.g. link, then call the class method url_attribute in your model like this:

class MyAwesomeModel < ActiveRecord::Base

  url_attribute :link

end

This does two things:

  1. It adds a validation to link that
  2. Before save, it strips trailing whitespace from the link and adds "http://" to the beginning if it (or "https://") is not already there.

If you want the logic to run somewhere other than before_save, pass a :before option with the name of another before_ callback:

class MyAwesomeModel < ActiveRecord::Base

  url_attribute :link, before: :validation

end

Matcher

The gem also comes with an RSpec matcher. You can use it like this:

describe MyAwesomeModel do

  it { is_expected.to have_url_attribute :link }

end

Contributing

Want to contribute to this gem? You're more than welcome! Just clone the repo from GitHub, upload your changes and open a pull request.

Make sure to add tests for your changes so they don't get accidentally broken by a future update.

(C) 2014 George Millo (Headstock Software)