diff --git a/app/controllers/cabpools_controller.rb b/app/controllers/cabpools_controller.rb index 7cb60fd..183704a 100644 --- a/app/controllers/cabpools_controller.rb +++ b/app/controllers/cabpools_controller.rb @@ -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 diff --git a/app/models/cabpool.rb b/app/models/cabpool.rb index 6d5abce..ae3694f 100644 --- a/app/models/cabpool.rb +++ b/app/models/cabpool.rb @@ -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 diff --git a/app/views/cabpools/new.html.erb b/app/views/cabpools/new.html.erb index 80263d2..c4e9d6b 100644 --- a/app/views/cabpools/new.html.erb +++ b/app/views/cabpools/new.html.erb @@ -4,7 +4,7 @@
<%= 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' %> diff --git a/app/views/shared/_error_messages_cabpool.html.erb b/app/views/shared/_error_messages_cabpool.html.erb new file mode 100644 index 0000000..f96d4ae --- /dev/null +++ b/app/views/shared/_error_messages_cabpool.html.erb @@ -0,0 +1,17 @@ +<% if @cabpool.errors.any? %> +
+
+ This form contains <%= pluralize(@cabpool.errors.count, "error") %>. +
+
    + <% @cabpool.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> + <% unless @locality_errors.nil? %> + <% @locality_errors.each do |msg| %> +
  • <%= msg %>
  • + <% end %> + <% end %> +
+
+<% end %> \ No newline at end of file diff --git a/spec/controllers/cabpools_controller_spec.rb b/spec/controllers/cabpools_controller_spec.rb index 4f080b1..c2cb4ff 100644 --- a/spec/controllers/cabpools_controller_spec.rb +++ b/spec/controllers/cabpools_controller_spec.rb @@ -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 diff --git a/spec/factories/cabpools.rb b/spec/factories/cabpools.rb index 7309886..24bff3b 100644 --- a/spec/factories/cabpools.rb +++ b/spec/factories/cabpools.rb @@ -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 diff --git a/spec/models/cabpool_spec.rb b/spec/models/cabpool_spec.rb index 628949e..2546b31 100644 --- a/spec/models/cabpool_spec.rb +++ b/spec/models/cabpool_spec.rb @@ -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