Skip to content

Commit

Permalink
WTH - Dashboard: Study Level Activity Add Validation to 'Time' Data
Browse files Browse the repository at this point in the history
I added validation for time with regular expression to only allow submissions up to 2 digits after decimal point.
Also, added validation to ensure the numericality and that the number is greater than zero.
Wrote corresponding specs to test for multiple scenarios.
  • Loading branch information
William Holt committed May 26, 2016
1 parent 952899a commit 5f92c9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/fulfillment.rb
Expand Up @@ -35,6 +35,10 @@ class Fulfillment < ActiveRecord::Base
attr_accessible :unit_type
attr_accessible :formatted_date

validates :time, format: { with: /\A\d+(?:\.\d{0,2})?\z/,
message: "must be a decimal with two places after the decimal point, eg. '1.23'" },
numericality: { greater_than: 0, message: 'must be greater than zero'}

default_scope -> { order('fulfillments.id ASC') }

QUANTITY_TYPES = ['Min', 'Hours', 'Days', 'Each']
Expand Down
26 changes: 26 additions & 0 deletions spec/models/fulfillment_spec.rb
Expand Up @@ -71,4 +71,30 @@
end
end
end

describe 'time validation' do
it 'should validate the format of time' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 1.23)

expect(fulfillment).to be_valid
end

it 'should not validate the format of time up to two numbers after decimal point' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 1.23434)

expect(fulfillment).not_to be_valid
end

it 'should not validate the format of time - should be greater than zero' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 0)

expect(fulfillment).not_to be_valid
end

it 'should not validate the format of time - no strings allowed' do
fulfillment = build(:fulfillment, date: Date.new(2001, 1, 2).strftime("%m-%d-%Y"), time: 'ooga booga')

expect(fulfillment).not_to be_valid
end
end
end

0 comments on commit 5f92c9d

Please sign in to comment.