diff --git a/app/controllers/recruiter_controller.rb b/app/controllers/recruiter_controller.rb index 0b51426..a6d05c8 100644 --- a/app/controllers/recruiter_controller.rb +++ b/app/controllers/recruiter_controller.rb @@ -7,9 +7,18 @@ class RecruiterController < ApplicationController def new @candidate = Candidate.new + render :form end def create + @candidate = Candidate.create(candidate_params.merge(recruiter_id: current_recruiter.id)) + + if @candidate.persisted? + redirect_to recruiter_path, flash: { notice: "Sucessfully created candidate #{@candidate.name}" } + else + flash[:error] = "Failed to save Candidate." + render :form + end end def login diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..4c7737c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,12 @@ module ApplicationHelper + def experience_options val + options_for_select([ + ["Please Select", ""], + ["0-3 Years", "0-3"], + ["4-6 Years", "4-6"], + ["7-9 Years", "7-9"], + ["10-14 Years", "10-14"], + ["15+ Years", "15+"] + ], disabled: "-", selected: (val.blank? ? '' : val)) + end end diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 482864a..b219635 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -4,11 +4,14 @@ class Candidate < ApplicationRecord has_many :answers belongs_to :recruiter, class_name: "User" - before_create :generate_test_hash + before_validation(:generate_test_hash, on: :create) + validates_presence_of :name + validates_presence_of :email + validates_presence_of :experience validates_presence_of :recruiter_id - validates_presence_of :test_hash - validates_uniqueness_of :test_hash + validates :email, uniqueness: true, presence: true, email_format: true + validates :test_hash, uniqueness: true, presence: true def submitted_answers answers.where(submitted: true) diff --git a/app/validators/email_format_validator.rb b/app/validators/email_format_validator.rb new file mode 100644 index 0000000..cc2ec71 --- /dev/null +++ b/app/validators/email_format_validator.rb @@ -0,0 +1,16 @@ +class EmailFormatValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + # EMAIL regex test + # (comma seperated) [any word combo] AT [any word combo] DOT [2 or more] + # me@no.yes.x == invalid + # some.thing+two@sub.domain.name == valid + + results = value.to_s.split(',').map do |v| + (v.strip =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i) || v.strip.blank? + end + + if results.include?(false) + record.errors[attribute] << (options[:message] || "is not formatted properly: #{value}") + end + end +end diff --git a/app/views/recruiter/form.html.erb b/app/views/recruiter/form.html.erb new file mode 100644 index 0000000..e0b7029 --- /dev/null +++ b/app/views/recruiter/form.html.erb @@ -0,0 +1,32 @@ +
+

New Candidate

+ + <% if flash[:error].present? %> +
+ <%= flash[:error] %> + <% @candidate.errors.messages.each do |k,v| %> +

<%= "#{k}: #{v}" %>

+ <% end %> +
+ <% end %> + + <%= form_for @candidate, url: create_candidate_path do |form| %> +
+ <%= form.label :name %> + <%= form.text_field :name %> +
+ +
+ <%= form.label :email %> + <%= form.email_field :email %> +
+ +
+ <%= form.label :experience %> + <%= form.select :experience, experience_options(@candidate.experience), include_blank: false %> +
+ + <%= form.hidden_field :quiz_id, { value: Quiz.first.to_i } %> + <%= submit_tag "Login" %> + <% end %> +
diff --git a/app/views/recruiter/new.html.erb b/app/views/recruiter/new.html.erb deleted file mode 100644 index 6afd6ab..0000000 --- a/app/views/recruiter/new.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -

Create new candidate

- diff --git a/test/controllers/recruiter_controller_test.rb b/test/controllers/recruiter_controller_test.rb index e3bfe7f..da5d5f6 100644 --- a/test/controllers/recruiter_controller_test.rb +++ b/test/controllers/recruiter_controller_test.rb @@ -53,4 +53,15 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest get create_candidate_url assert_response :success end + + test "should create new candidate" do + # recruiter = users(:recruiter) + setup_auth + assert_difference("Candidate.count") do + post create_candidate_path, params: { candidate: + { name: 'new name', email: 'test@mailinator.com', experience: '0-3', quiz_id: quizzes(:fed).id } } + end + assert_redirected_to recruiter_path + assert flash[:notice] + end end diff --git a/test/models/candidate_test.rb b/test/models/candidate_test.rb index 85798b8..112f8ef 100644 --- a/test/models/candidate_test.rb +++ b/test/models/candidate_test.rb @@ -1,7 +1,12 @@ require 'test_helper' class CandidateTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "test_hash is auto generated" do + candidate = Candidate.create(name: 'new name', + email: 'test@mailinator.com', + experience: '0-3', + quiz_id: quizzes(:fed).id) + + assert candidate.test_hash.present? + end end