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

Commit 27b6a63

Browse files
refactor scopes on job class
Improve navitation of jobs for users and admins
1 parent 2374d70 commit 27b6a63

File tree

7 files changed

+68
-21
lines changed

7 files changed

+68
-21
lines changed

app/controllers/jobs_controller.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@ def index
1111
@page_description = 'Technical & software development jobs listed on MENAdevs'
1212
@page_keywords = AppSettings.meta_tags_keywords
1313

14-
@jobs = Job.approved.order(:posted_on).reverse_order
14+
@jobs = Job.approved_jobs.order(:posted_on).reverse_order
1515

1616
if (user_signed_in? && params.has_key?(:state))
17-
filter_by_params
17+
filter_by_params(current_user)
1818
end
1919

2020
@jobs = Kaminari.paginate_array(@jobs).page(params[:page]).per(25)
2121
end
2222

2323
# GET /list-jobs-admin
2424
def list_jobs
25-
@admin_jobs = Job.all.order(:updated_at).reverse_order.page(params[:page])
25+
@jobs = Job.all.order(:created_at).reverse_order
26+
27+
if params.has_key?(:state)
28+
filter_by_params
29+
end
30+
31+
@jobs = Kaminari.paginate_array(@jobs).page(params[:page]).per(25)
2632

2733
render :admin_index
2834
end
@@ -130,16 +136,22 @@ def set_job
130136
@job = @job.decorate.first
131137
end
132138

133-
def filter_by_params
134-
user_jobs = Job.user_jobs(current_user)
139+
def filter_by_params(user = nil)
140+
if user.nil?
141+
user_jobs = Job.all
142+
else
143+
user_jobs = Job.user_jobs(user)
144+
end
135145
state_params = params[:state]
136146

137147
if state_params == 'user'
138148
@jobs = user_jobs
139149
elsif state_params == 'draft'
140-
@jobs = user_jobs.user_draft
150+
@jobs = user_jobs.draft_jobs
151+
elsif state_params == 'pending'
152+
@jobs = user_jobs.pending_jobs
141153
elsif state_params == 'expired'
142-
@jobs = user_jobs.user_expired
154+
@jobs = user_jobs.expired_jobs
143155
end
144156
end
145157

app/models/job.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ class Job < ApplicationRecord
110110
friendly_id :title, use: :slugged
111111

112112
scope :user_jobs, -> (user) { where(user_id: user.id) }
113-
scope :approved, -> { where(aasm_state: 'approved') }
114-
scope :user_draft, -> { where(aasm_state: ['draft', 'under_review', 'edited']) }
115-
scope :user_expired, -> { where(aasm_state: 'disabled') }
116-
scope :live, -> { where.not(:posted_on => nil) }
113+
scope :pending_jobs, -> { where(aasm_state: 'under_review') }
114+
scope :approved_jobs, -> { where(aasm_state: 'approved') }
115+
scope :draft_jobs, -> { where(aasm_state: ['draft', 'under_review', 'edited']) }
116+
scope :expired_jobs, -> { where(aasm_state: 'disabled') }
117+
scope :live_jobs, -> { where.not(:posted_on => nil) }
117118

118119
def offline?
119120
(self.draft? || self.under_review? || self.disabled?) ? true : false

app/views/jobs/_admin_job.html.erb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
</td>
2929

3030
<td>
31+
<%= admin_job.aasm.human_state %>
32+
</td>
33+
34+
<td width="5%">
3135
<% if (authorised_admin? && admin_job.offline?) %>
32-
<%= link_to("Edit", edit_job_path(admin_job), class: "button button-3d button-mini button-rounded button-blue") %>
33-
<%= link_to("Delete", job_path(admin_job), class: "button button-3d button-mini button-rounded button-red", method: 'delete') %>
36+
<%= link_to("Edit", edit_job_path(admin_job), class: "button button-3d button-mini button-rounded button-blue fright") %>
3437
<% end %>
3538
</td>
3639
</tr>

app/views/jobs/_admin_jobs_table.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
<th>Created</th>
1111
<th>Views</th>
1212
<th>Hired?</th>
13+
<th>State</th>
1314
<th>&nbsp;</th>
1415
</tr>
1516
</thead>
1617

1718
<tbody>
18-
<%= render(partial: "admin_job", collection: @admin_jobs) %>
19+
<%= render(partial: "admin_job", collection: @jobs) %>
1920
</tbody>
2021

2122
</table>

app/views/jobs/_job.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
<td>
2020
<% if (authorised_job_owner?(job) && job.offline?) %>
21-
<%= link_to("Edit", edit_job_path(job), class: "button button-3d button-mini button-rounded button-blue") %>
22-
<%= link_to("Delete", job_path(job), class: "button button-3d button-mini button-rounded button-red", method: 'delete') %>
21+
<%= link_to("Delete", job_path(job), class: "button button-3d button-mini button-rounded button-red fright", method: 'delete', data: { confirm: "Are you sure that you want to delete this job?" }) %>
22+
<%= link_to("Edit", edit_job_path(job), class: "button button-3d button-mini button-rounded button-blue fright") %>
2323
<% end %>
2424
</td>
2525
</tr>

app/views/jobs/admin_index.html.erb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,50 @@
2222

2323
<div class="container clearfix">
2424

25-
<% if @admin_jobs.blank? %>
25+
<!-- Filter jobs helper: start -->
26+
<div>
27+
<span>Filter jobs</span> &nbsp;
28+
<%= link_to(list_jobs_admin_path) do %>
29+
<%= content_tag(:button, class: display_button_filter_class(params)) do %>
30+
All jobs
31+
<% end %>
32+
<% end %>
33+
34+
<%= link_to(list_jobs_admin_path(state: 'pending')) do %>
35+
<%= content_tag(:button, class: display_button_filter_class(params, 'pending')) do %>
36+
All pending approval jobs
37+
<% end %>
38+
<% end %>
39+
40+
<%= link_to(list_jobs_admin_path(state: 'draft')) do %>
41+
<%= content_tag(:button, class: display_button_filter_class(params, 'draft')) do %>
42+
All draft jobs
43+
<% end %>
44+
<% end %>
45+
46+
<%= link_to(list_jobs_admin_path(state: 'expired')) do %>
47+
<%= content_tag(:button, class: display_button_filter_class(params, 'expired')) do %>
48+
All expired jobs
49+
<% end %>
50+
<% end %>
51+
</div>
52+
<hr/>
53+
<!-- Filter jobs helper: end -->
54+
55+
<% if @jobs.blank? %>
2656
<div class="style-msg alertmsg">
2757
<div class="sb-msg"><i class="icon-warning-sign"></i>There are no jobs on MENAdevs at the moment</div>
2858
</div>
2959
<% else %>
3060

3161
<div class="row">
3262
<div class="col-md-12">
33-
<%= render("admin_jobs_table", locals: { admin_jobs: @admin_jobs }) %>
63+
<%= render("admin_jobs_table", locals: { admin_jobs: @jobs }) %>
3464
</div>
3565
</div>
3666

3767
<div class="text-center">
38-
<%= paginate(@admin_jobs) %>
68+
<%= paginate(@jobs) %>
3969
</div>
4070
<% end %>
4171

app/views/jobs/index.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444

4545
<%= link_to(jobs_path(state: 'draft')) do %>
4646
<%= content_tag(:button, class: display_button_filter_class(params, 'draft')) do %>
47-
Draft
47+
My draft jobs
4848
<% end %>
4949
<% end %>
5050

5151
<%= link_to(jobs_path(state: 'expired')) do %>
5252
<%= content_tag(:button, class: display_button_filter_class(params, 'expired')) do %>
53-
Expired
53+
My expired jobs
5454
<% end %>
5555
<% end %>
5656
</div>

0 commit comments

Comments
 (0)