Skip to content

Commit

Permalink
[Thejas | Nikhil] #2 Added a validation check for the Cabpool Model
Browse files Browse the repository at this point in the history
  • Loading branch information
thejasbabu committed Dec 1, 2015
1 parent 1a175b4 commit b0ca479
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 8 deletions.
1 change: 0 additions & 1 deletion app/controllers/cabpools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ def new
@cabpool = Cabpool.new
user = User.find_by_email(session[:userid])
@locality = Locality.find(user.locality_id).name
debugger
end
end
4 changes: 4 additions & 0 deletions app/models/cabpool.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Cabpool < ActiveRecord::Base

has_many :users
belongs_to :locality

validates_presence_of :number_of_people, :timein, :timeout, :route
validates_numericality_of :number_of_people, less_than_or_equal_to: 4, greater_than_or_equal_to: 1
end
2 changes: 1 addition & 1 deletion app/views/cabpools/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="row">
<div class="col-md-4 col-md-offset-4">
<%= form_for(@cabpool) do |c| %>

<%= render 'shared/error_messages_cabpool' %>
<%= c.label :number_of_people, "Number of people to travel with" %>
<%= c.text_field :number_of_people, class: 'form-control' %>

Expand Down
17 changes: 17 additions & 0 deletions app/views/shared/_error_messages_cabpool.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<% if @cabpool.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
This form contains <%= pluralize(@cabpool.errors.count, "error") %>.
</div>
<ul>
<% @cabpool.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% unless @locality_errors.nil? %>
<% @locality_errors.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
</ul>
</div>
<% end %>
1 change: 0 additions & 1 deletion spec/controllers/cabpools_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
stub_locality = Locality.new(:name =>"Blah")
allow(Locality).to receive(:find).and_return(stub_locality)
allow(stub_locality).to receive(:name).and_return("Blah")

end

it 'should render create cabpools page' do
Expand Down
33 changes: 28 additions & 5 deletions spec/factories/cabpools.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
FactoryGirl.define do
factory :cabpools do
factory :cabpool do
route "MyString"
number_of_people 1
timein "MyString"
timeout "MyString"
locality nil
number_of_people 1
timein "MyString"
timeout "MyString"
locality nil
end

trait :without_number_of_people do
number_of_people nil
end

trait :without_time_in do
timein ''
end

trait :without_time_out do
timeout ''
end

trait :without_routes do
route ''
end

trait :without_less_than_four_people do
number_of_people 6
end

trait :without_greater_than_one_person do
number_of_people 0
end
end
30 changes: 30 additions & 0 deletions spec/models/cabpool_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
require 'rails_helper'

RSpec.describe Cabpool, type: :model do

it 'Number of people should not be empty' do
cabpool = build(:cabpool, :without_number_of_people)
expect(cabpool.valid?).to be false
end

it 'Time in of the cabpool should not be empty' do
cabpool = build(:cabpool, :without_time_in)
expect(cabpool.valid?).to be false
end

it 'Time out of the cabpool should not be empty' do
cabpool = build(:cabpool, :without_time_out)
expect(cabpool.valid?).to be false
end

it 'Routes of the cabpool should not be empty' do
cabpool = build(:cabpool, :without_routes)
expect(cabpool.valid?).to be false
end

it 'Number of people should be less than or equal to 4' do
cabpool = build(:cabpool, :without_less_than_four_people)
expect(cabpool.valid?).to be false
end

it 'Number of people should be greater than or equal to 1' do
cabpool = build(:cabpool, :without_greater_than_one_person)
expect(cabpool.valid?).to be false
end
end

0 comments on commit b0ca479

Please sign in to comment.