diff --git a/Gemfile b/Gemfile index 91d17e8..b7b6f37 100644 --- a/Gemfile +++ b/Gemfile @@ -49,3 +49,8 @@ group :development, :test do gem 'spring' end + +group :test do + gem "shoulda-matchers", '2.6.0' + gem "minitest" +end diff --git a/Gemfile.lock b/Gemfile.lock index c53d616..171a721 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -137,6 +137,8 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + shoulda-matchers (2.6.0) + activesupport (>= 3.0.0) skyblue-rails (0.0.0.2) railties (>= 3.1.0) spring (1.6.2) @@ -172,11 +174,13 @@ DEPENDENCIES factory_girl_rails jbuilder (~> 2.0) jquery-rails + minitest pg rails (= 4.2.3) rspec-rails sass-rails (~> 5.0) sdoc (~> 0.4.0) + shoulda-matchers (= 2.6.0) skyblue-rails spring turbolinks diff --git a/app/models/answer.rb b/app/models/answer.rb new file mode 100644 index 0000000..0348afe --- /dev/null +++ b/app/models/answer.rb @@ -0,0 +1,13 @@ +class Answer < ActiveRecord::Base + belongs_to :quiz + + validates :answer_text, presence: true, length: { in: 1..30 } + validates :quiz_id, presence: true + validate :right_answer_is_only_one_by_one_quiz + + def right_answer_is_only_one_by_one_quiz + if quiz.present? && right_or_wrong && quiz.answers.where(right_or_wrong: true) > 0 + error.add(:right_answer_number, "1つの問題に対して正解は1つだけです") + end + end +end diff --git a/db/migrate/20160317000000_create_answers.rb b/db/migrate/20160317000000_create_answers.rb new file mode 100644 index 0000000..ce5eca8 --- /dev/null +++ b/db/migrate/20160317000000_create_answers.rb @@ -0,0 +1,11 @@ +class CreateAnswers < ActiveRecord::Migration + def change + create_table :answers do |t| + t.string :answer_text, :null => false + t.boolean :right_or_wrong, :null => false + t.belongs_to :quiz, index: true, foreign_key: true + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c8bc635..a97318c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160311000000) do +ActiveRecord::Schema.define(version: 20160317000000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "answers", force: :cascade do |t| + t.string "answer_text", null: false + t.boolean "right_or_wrong", null: false + t.integer "quiz_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "answers", ["quiz_id"], name: "index_answers_on_quiz_id", using: :btree + create_table "quiz_titles", force: :cascade do |t| t.string "title", null: false t.text "url", null: false @@ -39,5 +49,6 @@ t.datetime "updated_at", null: false end + add_foreign_key "answers", "quizzes" add_foreign_key "quizzes", "quiz_titles" end diff --git a/spec/factories/answers.rb b/spec/factories/answers.rb new file mode 100644 index 0000000..95737f9 --- /dev/null +++ b/spec/factories/answers.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :answer do + answer_text "MyString" + right_or_wrong false + quiz_id nil + end +end diff --git a/spec/factories/quiz_titles.rb b/spec/factories/quiz_titles.rb index a1bed9a..07497d4 100644 --- a/spec/factories/quiz_titles.rb +++ b/spec/factories/quiz_titles.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :quiz_title do - + title "MyString" + url "MyString" end - end diff --git a/spec/factories/quizzes.rb b/spec/factories/quizzes.rb index 3714c2a..77afde0 100644 --- a/spec/factories/quizzes.rb +++ b/spec/factories/quizzes.rb @@ -1,7 +1,6 @@ FactoryGirl.define do factory :quiz do quiz_statement "MyString" -quiz_title nil + quiz_title nil end - end diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb new file mode 100644 index 0000000..440b5aa --- /dev/null +++ b/spec/models/answer_spec.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +RSpec.describe Answer, type: :model do + before do + @quiz_title1 = FactoryGirl.create(:quiz_title, id: 1) + @quiz1 = FactoryGirl.create(:quiz, id: 1 , quiz_title_id: 1) + @answer1 = FactoryGirl.create(:answer, id: 1, quiz_id: 1, right_or_wrong: false) + end + + subject { @answer1 } + + describe "belongs_to quiz model" do + it { should belong_to(:quiz)} + end + + describe "answer_text validate" do + it { should validate_presence_of(:answer_text) } + it { should ensure_length_of(:answer_text).is_at_least(1) } + it { should ensure_length_of(:answer_text).is_at_most(30) } + end + + describe "answer_text validate" do + it { should validate_presence_of(:quiz_id) } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 88ff2d0..fdd0b5f 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -5,6 +5,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' require 'rspec/rails' +require 'minitest/autorun' +require 'shoulda/matchers' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in