Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class WebSite < ActiveRecord::Base
end
```

If you would like to automatically update url's with missing protocols, use the `ensure_protocol` option:

```ruby
validates :url, :url => {:ensure_protocol => true}
```

This will update your attribute, prepending the default "https://" protocol. This way, urls without a protocol still pass validation, but don't get into your database without a valid protocol.

## Features

* Allow using *http*, *https* and *schema-less* urls. (will be configurable in future)
Expand Down
2 changes: 2 additions & 0 deletions lib/valid_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def validate_each(record, attribute, value)
begin
url = ensure_protocol(value)
uri = Addressable::URI.parse(url)

record.send("#{attribute}=", url) if options[:ensure_protocol]
rescue
invalid = true
end
Expand Down
Loading