Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 6ade2da

Browse files
Allow no salary setting for freelance jobs - EXCEPTIONALLY -
1 parent 119217f commit 6ade2da

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

app/controllers/application_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def api_request
2121
end
2222

2323
def check_user_profile_complete
24-
if user_signed_in? && current_user.profile && !current_user.profile.complete?
25-
redirect_to edit_user_profile_path(current_user), alert: 'Please complete your profile before proceeding'
24+
if (user_signed_in? && current_user.profile && !current_user.profile.complete?)
25+
redirect_to(edit_user_profile_path(current_user), notice: 'Please complete your profile before proceeding')
2626
end
2727
end
2828

app/models/job.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ class Job < ApplicationRecord
9595
validates :title, presence: true
9696
validates :employment_type, presence: true
9797
validates :experience, presence: true
98-
validates :from_salary, presence: true
98+
validates :from_salary, presence: true, :if => Proc.new { |j| j.job_is_not_freelance? }
99+
validates :from_salary, :numericality => { :greater_than_or_equal_to => 0 }, :if => Proc.new { |j| j.job_is_not_freelance? }
100+
validates :to_salary, :numericality => { :greater_than => 0, allow_nil: true }, :if => Proc.new { |j| (j.job_is_not_freelance? && j.salary_is_set?) }
99101
validates :currency, presence: true, :if => Proc.new { |j| j.salary_is_set? }
100102
validates :payment_term, presence: true, :if => Proc.new { |j| j.salary_is_set? }
101103

@@ -128,6 +130,10 @@ def online?
128130
self.approved?
129131
end
130132

133+
def job_is_not_freelance?
134+
self.employment_type != "freelance"
135+
end
136+
131137
def salary_is_set?
132138
self.from_salary.blank? ? false : true
133139
end

app/validators/salary_validator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ def validate_each(record, attribute, value)
44
end
55

66
def valid_salary_fields?(record)
7-
return true if record.to_salary.blank?
7+
return true if (record.to_salary.blank? || record.employment_type == "freelance")
88

99
record.from_salary < record.to_salary
1010
end
11-
end
11+
end

spec/models/job_spec.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
end
5151

5252
let(:job) { create(:job) }
53+
let(:freelance_job) { create(:job, employment_type: :freelance) }
5354

5455
it "valid object" do
5556
expect(job).to be_valid
@@ -59,18 +60,42 @@
5960
expect(job.custom_identifier).to_not be_empty
6061
end
6162

62-
it "validate from salary to be less than to salary range" do
63+
it "should not have starting salary as a negative value" do
64+
job.from_salary = -100
65+
job.to_salary = 500
66+
expect(job).to_not be_valid
67+
expect(job.errors[:from_salary]).to include('must be greater than or equal to 0')
68+
end
69+
70+
it "should not have upper limit salary as a negative value" do
71+
job.from_salary = 100
72+
job.to_salary = -1
73+
expect(job).to_not be_valid
74+
expect(job.errors[:to_salary]).to include('must be greater than 0')
75+
end
76+
77+
it "should validate to salary to be less than from salary range" do
6378
job.from_salary = 1500
6479
job.to_salary = 500
6580
expect(job).to_not be_valid
6681
expect(job.errors[:to_salary]).to eq(['cannot be less than starting salary'])
82+
end
6783

84+
it "should validate to salary to be equal to from salary" do
6885
job.from_salary = 10
6986
job.to_salary = 10
7087
expect(job).to_not be_valid
7188
expect(job.errors[:to_salary]).to eq(['cannot be less than starting salary'])
7289
end
7390

91+
it "should not validate salary if job is a freelance contract" do
92+
job.from_salary = 0
93+
job.to_salary = 0
94+
95+
expect(freelance_job).to be_valid
96+
expect(freelance_job.errors).to be_empty
97+
end
98+
7499
it { should validate_presence_of :company_name }
75100
it { should validate_presence_of :title }
76101
it { should validate_presence_of :employment_type }
@@ -155,6 +180,20 @@
155180
end
156181
end
157182

183+
describe "Job#remove_expired_jobs" do
184+
let!(:approved_job_00) { create(:job, aasm_state: :approved, posted_on: (Date.today - 3.months)) }
185+
let!(:approved_job_01) { create(:job, aasm_state: :approved, posted_on: (Date.today - 45.days)) }
186+
let!(:approved_job_02) { create(:job, aasm_state: :approved) }
187+
188+
it "should unpublish live jobs that are older than 1 month" do
189+
expect(Job.approved.count).to eq(3)
190+
191+
Job.remove_expired_jobs
192+
193+
expect(Job.approved.count).to eq(1)
194+
end
195+
end
196+
158197
describe "#text_for_twitter" do
159198
let(:new_job) { create(:job, aasm_state: :approved, twitter_handle: 'menadevs', title: 'Engineering Manager') }
160199
let(:twitter_text) { new_job.text_for_twitter }

spec/requests/jobs_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
select('Part time', :from => 'Employment type')
115115
select('Associate', :from => 'Experience')
116116
fill_in 'Starting salary', with: '1000'
117+
# fill_in 'To salary', with: '1500'
117118
select('Per month', :from => 'Payment term')
118119
select('United States Dollar (USD)', :from => 'Currency')
119120

0 commit comments

Comments
 (0)