candidate creation
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										16
									
								
								app/validators/email_format_validator.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								app/validators/email_format_validator.rb
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
							
								
								
									
										32
									
								
								app/views/recruiter/form.html.erb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								app/views/recruiter/form.html.erb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
<main class="intro_tpl">
 | 
			
		||||
  <h1>New Candidate</h1>
 | 
			
		||||
 | 
			
		||||
  <% if flash[:error].present? %>
 | 
			
		||||
    <div class="error">
 | 
			
		||||
      <%= flash[:error] %>
 | 
			
		||||
      <% @candidate.errors.messages.each do |k,v| %>
 | 
			
		||||
        <p><%= "#{k}: #{v}" %></p>
 | 
			
		||||
      <% end %>
 | 
			
		||||
    </div>
 | 
			
		||||
  <% end %>
 | 
			
		||||
 | 
			
		||||
  <%= form_for @candidate, url: create_candidate_path do |form| %>
 | 
			
		||||
    <div class="form-group">
 | 
			
		||||
      <%= form.label :name %>
 | 
			
		||||
      <%= form.text_field :name %>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div class="form-group">
 | 
			
		||||
      <%= form.label :email %>
 | 
			
		||||
      <%= form.email_field :email %>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div class="form-group">
 | 
			
		||||
      <%= form.label :experience %>
 | 
			
		||||
      <%= form.select :experience, experience_options(@candidate.experience), include_blank: false %>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <%= form.hidden_field :quiz_id, { value: Quiz.first.to_i } %>
 | 
			
		||||
    <%= submit_tag "Login" %>
 | 
			
		||||
  <% end %>
 | 
			
		||||
</main>
 | 
			
		||||
@@ -1,2 +0,0 @@
 | 
			
		||||
<h1>Create new candidate</h1>
 | 
			
		||||
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user