diff --git a/app/controllers/admin/user_controller.rb b/app/controllers/admin/user_controller.rb index eea95ab..f344909 100644 --- a/app/controllers/admin/user_controller.rb +++ b/app/controllers/admin/user_controller.rb @@ -45,7 +45,7 @@ module Admin private def user_params - params.require(:user).permit(:name, :email, :role, :password) + params.require(:user).permit(:name, :email, :role, :password, quiz_ids: []) end end end diff --git a/app/controllers/recruiter_controller.rb b/app/controllers/recruiter_controller.rb index 4b6731e..8ca72cc 100644 --- a/app/controllers/recruiter_controller.rb +++ b/app/controllers/recruiter_controller.rb @@ -8,10 +8,12 @@ class RecruiterController < ApplicationController def new @candidate = Candidate.new + @quizzes = Quiz.order(:name) render :form end def create + @quizzes = Quiz.order(:name) @candidate = Candidate.create(candidate_params.merge(recruiter_id: current_recruiter.id)) if @candidate.persisted? diff --git a/app/mailers/reviewer_mailer.rb b/app/mailers/reviewer_mailer.rb index b4eee1b..0f06024 100644 --- a/app/mailers/reviewer_mailer.rb +++ b/app/mailers/reviewer_mailer.rb @@ -2,14 +2,7 @@ class ReviewerMailer < ApplicationMailer def candidate_submission candidate @candidate = candidate - - # TODO: candidate.reviewers.map(:email) - if Rails.env.production? - recipients = ["harish.bhavanichikar@perficient.com", "jacob.schulke@perficient.com", - "jennifer.siegfried@perficient.com", "martin.ridgway@perficient.com"] - else - recipients = ["fed.reviewer@mailinator.com"] - end + recipients = candidate.quiz.reviewers.map(&:email) mail to: recipients, subject: "Skills Assessment Results" end diff --git a/app/models/quiz.rb b/app/models/quiz.rb index f8d0b23..c1ed37b 100644 --- a/app/models/quiz.rb +++ b/app/models/quiz.rb @@ -2,6 +2,8 @@ class Quiz < ApplicationRecord has_many :questions, -> { order(:sort) } has_many :candidates + has_many :reviewer_to_quizzes + has_many :reviewers, through: :reviewer_to_quizzes, source: :user validates :name, presence: true, uniqueness: true validates :dept, presence: true diff --git a/app/models/reviewer_to_quiz.rb b/app/models/reviewer_to_quiz.rb new file mode 100644 index 0000000..27634ad --- /dev/null +++ b/app/models/reviewer_to_quiz.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true +class ReviewerToQuiz < ApplicationRecord + belongs_to :user + belongs_to :quiz +end diff --git a/app/models/user.rb b/app/models/user.rb index 00bbdad..f57a07d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true class User < ApplicationRecord has_secure_password - has_many :candidates, foreign_key: "recruiter_id" + has_many :candidates, foreign_key: :recruiter_id + has_many :reviewer_to_quizzes + has_many :quizzes, through: :reviewer_to_quizzes validates :email, presence: true, uniqueness: true validates :name, presence: true diff --git a/app/views/admin/user/_form.html.erb b/app/views/admin/user/_form.html.erb index 7b254c0..c3b5266 100644 --- a/app/views/admin/user/_form.html.erb +++ b/app/views/admin/user/_form.html.erb @@ -15,5 +15,12 @@ <%= form.select :role, admin_role_options(user.role), include_blank: false %> + <%= form.collection_check_boxes(:quiz_ids, Quiz.all, :id, :name, {}, {class: 'checkbox'}) do | quiz | %> +
+ <%= quiz.check_box( checked: user.quizzes.include?(quiz.object)) %> + <%= quiz.label %> +
+ <% end %> + <%= form.submit %> <% end %> diff --git a/app/views/admin/user/view.html.erb b/app/views/admin/user/view.html.erb index 0bd3f52..9c5ec88 100644 --- a/app/views/admin/user/view.html.erb +++ b/app/views/admin/user/view.html.erb @@ -5,4 +5,12 @@

<%= @user.name %>

<%= mail_to(@user.email) %>

<%= @user.role %>

+ +

Quizzes:

+ + <%= link_to('Edit', admin_edit_user_path(@user.to_i), { class: 'btn' }) %> diff --git a/app/views/recruiter/form.html.erb b/app/views/recruiter/form.html.erb index 1a71a96..386de96 100644 --- a/app/views/recruiter/form.html.erb +++ b/app/views/recruiter/form.html.erb @@ -18,7 +18,11 @@ <%= form.select :experience, experience_options(@candidate.experience), include_blank: false %> - <%= form.hidden_field :quiz_id, { value: Quiz.first.to_i } %> +
+ <%= form.label :quiz_id, "Quiz" %> + <%= form.select :quiz_id, options_from_collection_for_select(@quizzes, 'id', 'name'), include_blank: (@quizzes.size > 1) %> +
+ <%= submit_tag "Create" %> <% end %> diff --git a/db/migrate/20160915164450_create_reviewer_to_quizzes.rb b/db/migrate/20160915164450_create_reviewer_to_quizzes.rb new file mode 100644 index 0000000..4f29634 --- /dev/null +++ b/db/migrate/20160915164450_create_reviewer_to_quizzes.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true +class CreateReviewerToQuizzes < ActiveRecord::Migration[5.0] + def change + create_table :reviewer_to_quizzes do |t| + t.integer :user_id, null: false + t.integer :quiz_id, null: false + + t.timestamps + end + add_index :reviewer_to_quizzes, :quiz_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ac3eb57..41a294d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160826200610) do +ActiveRecord::Schema.define(version: 20160915164450) do create_table "answers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.integer "candidate_id" @@ -65,6 +65,14 @@ ActiveRecord::Schema.define(version: 20160826200610) do t.string "name" end + create_table "reviewer_to_quizzes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.integer "user_id", null: false + t.integer "quiz_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["quiz_id"], name: "index_reviewer_to_quizzes_on_quiz_id", using: :btree + end + create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.string "name" t.string "email" diff --git a/erd.pdf b/erd.pdf index 9513737..1d050f2 100644 Binary files a/erd.pdf and b/erd.pdf differ diff --git a/test/fixtures/reviewer_to_quizzes.yml b/test/fixtures/reviewer_to_quizzes.yml new file mode 100644 index 0000000..4e8812f --- /dev/null +++ b/test/fixtures/reviewer_to_quizzes.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user: reviewer + quiz: fed + +two: + user: reviewer2 + quiz: fed diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 6b8ef4d..9c007a3 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -12,6 +12,12 @@ reviewer: password_digest: <%= BCrypt::Password.create("password", cost: 4) %> role: reviewer +reviewer2: + name: David Reviewer + email: david.reviewer@mailinator.com + password_digest: <%= BCrypt::Password.create("password", cost: 4) %> + role: reviewer + admin: name: Alan Admin email: alan.admin@mailinator.com diff --git a/test/mailers/reviewer_mailer_test.rb b/test/mailers/reviewer_mailer_test.rb index 45bb811..6241752 100644 --- a/test/mailers/reviewer_mailer_test.rb +++ b/test/mailers/reviewer_mailer_test.rb @@ -6,7 +6,7 @@ class ReviewerMailerTest < ActionMailer::TestCase candidate = candidates :dawn mail = ReviewerMailer.candidate_submission candidate assert_match "Results", mail.subject - # assert_equal [candidate.recruiter.email], mail.to + assert_equal candidate.quiz.reviewers.map(&:email), mail.to assert_equal [ENV["default_mail_from"]], mail.from assert_match candidate.test_hash, mail.body.encoded end diff --git a/test/models/reviewer_to_quiz_test.rb b/test/models/reviewer_to_quiz_test.rb new file mode 100644 index 0000000..43282af --- /dev/null +++ b/test/models/reviewer_to_quiz_test.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +require 'test_helper' + +class ReviewerToQuizTest < ActiveSupport::TestCase + test "the truth" do + assert ReviewerToQuiz + end +end