Skip to content

Commit

Permalink
Release 2: Add validation support
Browse files Browse the repository at this point in the history
After a challenge from Mike on the last version, I tried to do this
without building a whole, separate validation class.

I couldn't get message to work on the 'presence' validation, so I did a
separate validation function for it. I could have combined all of these
into one large validation function, but I decided it was more
interesting to make a number of simple validation functions instead.
  • Loading branch information
G. Wade Johnson committed Oct 13, 2015
1 parent a7c7a5a commit 91c758e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
3 changes: 3 additions & 0 deletions source/app/assets/stylesheets/urls.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Place all the styles related to the Urls controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.error {
color: red;
}
19 changes: 15 additions & 4 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ def index

def new
# default behavior looks good
@target = ''
@save_errors = nil
end

def create
# Actually create the new shortened url
target= params[:target]
@target = params[:target]
@errors = nil

@url = Url.new(target_link: target)
@url = Url.new(target_link: @target)
if !@url.save
flash[:save_error] = ['Unable to save URL. Please try another.'];
flash[:target] = target
case
when @url.errors[:target_link].any?
@errors = @url.errors[:target_link]
when @url.errors[:linkid].any?
@errors = @url.errors[:linkid]
when @url.errors[:base].any?
@errors = @url.errors[:base]
else
@errors = ['Unable to save URL. Please try another.'];
end
render 'new'
else
redirect_to url_path(@url.linkid)
Expand Down
37 changes: 36 additions & 1 deletion source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
require 'uri'
require 'net/http'

class Url < ActiveRecord::Base
before_save :ensure_short_link
validates :linkid, uniqueness: true
validate :target_link_not_empty, :target_link_must_be_http, :target_link_must_be_reachable

def target_link_not_empty
return unless errors.empty?
if target_link.empty?
errors[:target_link] << 'Target URL cannot be blank';
end
end

def target_link_must_be_http
return unless errors.empty?
unless target_link.match(%r{^https?://})
errors[:target_link] << 'Target URL has an unrecognized scheme';
end
end

def target_link_must_be_reachable
return unless errors.empty?
begin
uri = URI.parse(target_link)
resp = Net::HTTP.get_response(uri)
if resp.code.to_i >= 400
errors[:target_link] << 'Target URL is not found'
end
rescue URI::InvalidURIError
errors[:target_link] << 'Target URL is not valid'
return
rescue
errors[:target_link] << 'Target URL is not reachable'
end
end

def ensure_short_link
if self.linkid.nil? || self.linkid.length == 0
if self.linkid.nil? || self.linkid.empty?
self.linkid = UrlsHelper.shortened_linkid(self.target_link)
end
end
Expand Down
8 changes: 5 additions & 3 deletions source/app/views/urls/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<h1>URL Shortener</h1>
<% if flash[:save_error] %>
<p class="error"><%= flash[:save_error] %></p>
<% if !@errors.nil? && @errors.size > 0 %>
<% @errors.each do |err| %>
<p class="error"><%= err %></p>
<% end %>
<% end %>
<form id="new_url" method="post" action="<%= urls_path %>">
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>"/>
<label for="#target">Target:</label>
<input type="text" name="target" id="target" size="100" placeholder="http://example.com/cool_page.html" value="<%= flash[:target] || '' %>">
<input type="text" name="target" id="target" size="100" placeholder="http://example.com/cool_page.html" value="<%= @target || '' %>">
<button class="btn btn-primary" type="submit">Shorten</button>
</form>
<a href="<%= urls_path %>">Back to list</a>

0 comments on commit 91c758e

Please sign in to comment.