Skip to content

Commit

Permalink
improve readability by using more helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jul 26, 2024
1 parent 71db98a commit 3a7997a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
7 changes: 3 additions & 4 deletions app/helpers/person_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,18 @@ def fetch_ptime_or_skills_data
return all_skills_people unless ptime_available?

ptime_employees = Ptime::Client.new.request(:get, 'employees', { per_page: 1000 })
ptime_employee_ids = Person.pluck(:ptime_employee_id)
build_dropdown_data(ptime_employees, ptime_employee_ids)
build_dropdown_data(ptime_employees)
rescue CustomExceptions::PTimeClientError
ENV['LAST_PTIME_ERROR'] = DateTime.current.to_s
all_skills_people
end

def build_dropdown_data(ptime_employees, ptime_employee_ids)
def build_dropdown_data(ptime_employees)
ptime_employees.map do |ptime_employee|
ptime_employee_name = append_ptime_employee_name(ptime_employee)
person_id = Person.find_by(ptime_employee_id: ptime_employee[:id])
ptime_employee_id = ptime_employee[:id]
already_exists = ptime_employee_id.in?(ptime_employee_ids)
already_exists = ptime_employee_id.in?(Person.pluck(:ptime_employee_id))
path = new_person_path(ptime_employee_id: ptime_employee_id)
path = person_path(person_id) if already_exists

Expand Down
2 changes: 1 addition & 1 deletion spec/features/people_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe 'People Search', type: :feature, js: true do

before(:each) do
ENV['LAST_PTIME_ERROR'] = DateTime.current.to_s # This is needed to activate the fallback and test for the skills people
use_skills_db
sign_in auth_users(:user), scope: :auth_user
end

Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/person_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
longmax.update!(ptime_employee_id: 33)
alice.update!(ptime_employee_id: 21)

dropdown_data = build_dropdown_data(ptime_employees_data, Person.all.pluck(:ptime_employee_id))
dropdown_data = build_dropdown_data(ptime_employees_data)
expected = [
["Longmax Smith", "/people/169654640"],
["Alice Mante", "/people/663665735"],
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
config.infer_spec_type_from_file_location!

config.before(:each) do
set_env_variables_and_stub_request
stub_env_variables_and_request
end

config.before { allow($stdout).to receive(:puts) }
Expand Down
20 changes: 15 additions & 5 deletions spec/support/ptime_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module PtimeHelpers
def set_env_variables_and_stub_request
ENV["PTIME_BASE_URL"] = "www.ptime.example.com"
ENV["PTIME_API_USERNAME"] = "test username"
ENV["PTIME_API_PASSWORD"] = "test password"

def stub_env_variables_and_request
stub_env_var("PTIME_BASE_URL", "www.ptime.example.com")
stub_env_var("PTIME_API_USERNAME", "test username")
stub_env_var("PTIME_API_PASSWORD", "test password")
stub_ptime_request(ptime_employees.to_json)
end

Expand All @@ -24,4 +23,15 @@ def stub_ptime_request(return_body, path =nil)
.to_return(body: return_body, headers: { 'content-type': content_type }, status: 200)
.with(basic_auth: [ENV["PTIME_API_USERNAME"], ENV["PTIME_API_PASSWORD"]])
end

def use_skills_db
allow_any_instance_of(RestClient::Request)
.to receive(:execute)
.and_raise(RestClient::ExceptionWithResponse)
end

def stub_env_var(name, value)
allow(ENV).to receive(:fetch).with(name).and_return(value)
stub_const('ENV', ENV.to_hash.merge(name => value))
end
end

0 comments on commit 3a7997a

Please sign in to comment.