Skip to content

Commit 55f5c6d

Browse files
committed
Add JSON API flavored JSON response for jobs
1 parent cb1c87e commit 55f5c6d

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

app/controllers/jobs_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ class JobsController < ApplicationController
55
# GET /jobs.json
66
def index
77
@jobs = @jobs.page(params[:page])
8+
9+
respond_to do |format|
10+
format.html
11+
format.json
12+
end
813
end
914

1015
# GET /jobs/1

app/views/jobs/_job.json.jbuilder

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
json.extract! job, :id, :title, :origin, :url, :description, :user, :type, :telecommute, :employer, :location, :contact, :published, :created_at, :updated_at
2-
json.url job_url(job, format: :json)
1+
json.id job.id
2+
json.attributes do
3+
json.extract! job, :title, :origin, :url, :description, :job_type, :telecommute, :location, :contact, :published, :created_at, :updated_at, :published_at
4+
end
5+
6+
json.relationships do
7+
if job.user
8+
json.user do
9+
json.data do
10+
json.extract! job.user, :id
11+
end
12+
end
13+
end
14+
15+
json.employer do
16+
json.links do
17+
json.self employer_url(job.employer)
18+
end
19+
json.data do
20+
json.extract! job.employer, :id, :name
21+
end
22+
end
23+
end
24+
25+
json.links do
26+
json.self job_url(job)
27+
end

app/views/jobs/index.json.jbuilder

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
json.array! @jobs, partial: 'jobs/job', as: :job
1+
json.links do
2+
json.self jobs_url(page: @jobs.current_page)
3+
json.next jobs_url(page: @jobs.next_page) if @jobs.next_page
4+
json.last jobs_url(page: @jobs.total_pages)
5+
end
6+
7+
json.data do
8+
json.array! @jobs, partial: 'jobs/job', as: :job
9+
end

0 commit comments

Comments
 (0)