From c0eadc7eb5581ae3e2d476c3768f493747564d2c Mon Sep 17 00:00:00 2001 From: Emily Behlmann Date: Sun, 30 Oct 2016 16:34:52 -0500 Subject: [PATCH 1/4] added model validations, tests and factory for question. resolves #4 --- app/models/question.rb | 4 ++++ spec/examples.txt | 9 ++++++--- spec/factories/question_factory.rb | 23 +++++++++++++++++++++++ spec/models/question_spec.rb | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 spec/factories/question_factory.rb diff --git a/app/models/question.rb b/app/models/question.rb index 46c3954..8718e69 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -13,4 +13,8 @@ class Question < ActiveRecord::Base belongs_to :ballot + + # make sure ballot ID and text are present. Allows text, summary to be empty. + validates :ballot_id, presence: true + validates :text, presence: true end diff --git a/spec/examples.txt b/spec/examples.txt index 63dc2f9..6d152d8 100644 --- a/spec/examples.txt +++ b/spec/examples.txt @@ -1,5 +1,8 @@ example_id | status | run_time | --------------------------------------- | ------ | --------------- | -./spec/models/admin_user_spec.rb[1:1:1] | passed | 0.0534 seconds | -./spec/models/ballot_spec.rb[1:1:1] | passed | 0.04902 seconds | -./spec/models/ballot_spec.rb[1:1:2] | passed | 0.0089 seconds | +./spec/models/admin_user_spec.rb[1:1:1] | passed | 0.09046 seconds | +./spec/models/ballot_spec.rb[1:1:1] | failed | 0.00105 seconds | +./spec/models/ballot_spec.rb[1:1:2] | failed | 0.00138 seconds | +./spec/models/question_spec.rb[1:1] | passed | 0.0943 seconds | +./spec/models/question_spec.rb[1:2:1] | passed | 0.01272 seconds | +./spec/models/question_spec.rb[1:2:2] | passed | 0.01343 seconds | diff --git a/spec/factories/question_factory.rb b/spec/factories/question_factory.rb new file mode 100644 index 0000000..06471ce --- /dev/null +++ b/spec/factories/question_factory.rb @@ -0,0 +1,23 @@ +# == Schema Information +# +# Table name: questions +# +# id :integer not null, primary key +# text :text +# summary :text +# friendly_name :string +# ballot_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'faker' + +FactoryGirl.define do + factory :question do + text Faker::Lorem.sentence + + # Associations + ballot + end +end \ No newline at end of file diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 56d8454..197601b 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -14,4 +14,21 @@ require 'spec_helper' RSpec.describe Question do + it "has a valid factory" do + expect(FactoryGirl.create(:question)).to be_valid + end + + describe 'validates' do + before(:each) { @question = build(:question) } + + it "is invalid without an associated ballot id" do + @question.ballot_id = nil + expect(@question).not_to be_valid + end + + it "is invalid without text" do + @question.text = nil + expect(@question).not_to be_valid + end + end end From 848478356cd33c04f648b7ca95bc2b2d15786715 Mon Sep 17 00:00:00 2001 From: Emily Behlmann Date: Sun, 30 Oct 2016 17:03:57 -0500 Subject: [PATCH 2/4] adding model validations, tests and factory for answer, plus question factory bc of dependency. resolves #3 --- app/models/answer.rb | 3 +++ spec/examples.txt | 3 +++ spec/factories/answer_factory.rb | 22 ++++++++++++++++++++++ spec/factories/question_factory.rb | 23 +++++++++++++++++++++++ spec/models/answer_spec.rb | 17 +++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 spec/factories/answer_factory.rb create mode 100644 spec/factories/question_factory.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index e510c44..17775e3 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -12,4 +12,7 @@ class Answer < ActiveRecord::Base belongs_to :question + + validates :question_id, presence: true + validates :text, presence: true end diff --git a/spec/examples.txt b/spec/examples.txt index 63dc2f9..bc372ab 100644 --- a/spec/examples.txt +++ b/spec/examples.txt @@ -1,5 +1,8 @@ example_id | status | run_time | --------------------------------------- | ------ | --------------- | ./spec/models/admin_user_spec.rb[1:1:1] | passed | 0.0534 seconds | +./spec/models/answer_spec.rb[1:1] | passed | 0.10162 seconds | +./spec/models/answer_spec.rb[1:2:1] | passed | 0.01378 seconds | +./spec/models/answer_spec.rb[1:2:2] | passed | 0.01236 seconds | ./spec/models/ballot_spec.rb[1:1:1] | passed | 0.04902 seconds | ./spec/models/ballot_spec.rb[1:1:2] | passed | 0.0089 seconds | diff --git a/spec/factories/answer_factory.rb b/spec/factories/answer_factory.rb new file mode 100644 index 0000000..501743a --- /dev/null +++ b/spec/factories/answer_factory.rb @@ -0,0 +1,22 @@ +# == Schema Information +# +# Table name: answers +# +# id :integer not null, primary key +# text :text +# info :text +# question_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'faker' + +FactoryGirl.define do + factory :answer do + text Faker::Lorem.sentence + + # Associations + question + end +end \ No newline at end of file diff --git a/spec/factories/question_factory.rb b/spec/factories/question_factory.rb new file mode 100644 index 0000000..06471ce --- /dev/null +++ b/spec/factories/question_factory.rb @@ -0,0 +1,23 @@ +# == Schema Information +# +# Table name: questions +# +# id :integer not null, primary key +# text :text +# summary :text +# friendly_name :string +# ballot_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'faker' + +FactoryGirl.define do + factory :question do + text Faker::Lorem.sentence + + # Associations + ballot + end +end \ No newline at end of file diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index 8eeaeb8..bda9e7d 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -13,4 +13,21 @@ require 'spec_helper' RSpec.describe Answer do + it "has a valid factory" do + expect(FactoryGirl.create(:answer)).to be_valid + end + + describe 'validates' do + before(:each) { @answer = build(:answer) } + + it "is invalid without an associated question id" do + @answer.question_id = nil + expect(@answer).not_to be_valid + end + + it "is invalid without text" do + @answer.text = nil + expect(@answer).not_to be_valid + end + end end From 9365aed94c3b4039ab1ca9b29b4c63de9f711475 Mon Sep 17 00:00:00 2001 From: Emily Behlmann Date: Sun, 30 Oct 2016 19:15:54 -0500 Subject: [PATCH 3/4] added params to active admin --- app/admin/answer.rb | 1 + app/admin/ballot.rb | 2 +- app/admin/question.rb | 1 + app/models/ballot.rb | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/admin/answer.rb b/app/admin/answer.rb index 8e932da..0a85cf5 100644 --- a/app/admin/answer.rb +++ b/app/admin/answer.rb @@ -13,5 +13,6 @@ # permitted # end + permit_params :text, :info, :question_id end diff --git a/app/admin/ballot.rb b/app/admin/ballot.rb index a0feb06..d68feb6 100644 --- a/app/admin/ballot.rb +++ b/app/admin/ballot.rb @@ -13,6 +13,6 @@ # permitted # end - permit_params :date + permit_params :date, :admin_id end diff --git a/app/admin/question.rb b/app/admin/question.rb index fb77970..e103c09 100644 --- a/app/admin/question.rb +++ b/app/admin/question.rb @@ -13,5 +13,6 @@ # permitted # end + permit_params :text, :summary, :friendly_name, :ballot_id end diff --git a/app/models/ballot.rb b/app/models/ballot.rb index 286a958..74287f6 100644 --- a/app/models/ballot.rb +++ b/app/models/ballot.rb @@ -13,5 +13,5 @@ class Ballot < ActiveRecord::Base belongs_to :admin_user validates :date, presence: true - validates :admin_user, presence: true + validates :admin_user_id, presence: true end From 79caf5b809b990bfef567c058c946208698154d4 Mon Sep 17 00:00:00 2001 From: Emily Behlmann Date: Sun, 30 Oct 2016 19:31:49 -0500 Subject: [PATCH 4/4] adding friendly display names for models --- app/models/admin_user.rb | 4 ++++ app/models/answer.rb | 4 ++++ app/models/ballot.rb | 4 ++++ app/models/question.rb | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index b19187d..2e11619 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -24,4 +24,8 @@ class AdminUser < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable validates :email, uniqueness: true + + def display_name + "#{email}" + end end diff --git a/app/models/answer.rb b/app/models/answer.rb index 17775e3..72658cb 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -15,4 +15,8 @@ class Answer < ActiveRecord::Base validates :question_id, presence: true validates :text, presence: true + + def display_name + "#{text}" + end end diff --git a/app/models/ballot.rb b/app/models/ballot.rb index 74287f6..a05dc7f 100644 --- a/app/models/ballot.rb +++ b/app/models/ballot.rb @@ -14,4 +14,8 @@ class Ballot < ActiveRecord::Base validates :date, presence: true validates :admin_user_id, presence: true + + def display_name + "#{date}" + end end diff --git a/app/models/question.rb b/app/models/question.rb index 8718e69..eb2dfdd 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -17,4 +17,8 @@ class Question < ActiveRecord::Base # make sure ballot ID and text are present. Allows text, summary to be empty. validates :ballot_id, presence: true validates :text, presence: true + + def display_name + "#{friendly_name}" + end end