This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use model scopes and jbuilder (#582)
Adds jbuilder templates and improves logic behind API
- Loading branch information
Showing
10 changed files
with
174 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module PageService | ||
extend self | ||
|
||
def list(language: nil, limit: 30) | ||
Page.language(language). | ||
limit(limit). | ||
order('created_at desc'). | ||
published | ||
end | ||
|
||
def list_featured(language: nil) | ||
Page.language(language). | ||
featured. | ||
order('created_at desc'). | ||
published | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
json.array! @pages do |page| | ||
json.extract! page, :id, :title, :slug, :content, :created_at, :updated_at, :active, :featured, :action_count | ||
json.language page.language.code | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! @page, :id, :title, :slug, :content, :created_at, :updated_at, :active, :featured, :action_count | ||
json.language @page.language.code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,73 @@ | ||
require_relative 'shared_language_pages.rb' | ||
require 'rails_helper' | ||
|
||
describe "api/pages" do | ||
|
||
def json | ||
JSON.parse(response.body) | ||
end | ||
|
||
|
||
before :each do | ||
# I'm rounding the time. Ruby deals with time in nanoseconds whereas the database deals with time in microsecond | ||
# precision. If I don't round the time, the expectation comparing the JSON response expects data from the DB | ||
# with nanosecond precision. | ||
@time_now = Time.at(Time.now.to_i) | ||
allow(Time).to receive(:now).and_return(@time_now) | ||
end | ||
|
||
describe 'GET pages' do | ||
context 'with no specified language' do | ||
let!(:featured_pages) { create_list :page, 5, featured: true } | ||
let!(:mvp_pages) { create_list :page, 5, featured: false } | ||
let!(:last_featured_page) { create :page, title: 'I am the latest featured page', featured: true, slug: 'garden_slug' } | ||
let!(:last_mvp_page) { create :page, title: 'I am the latest test page', featured: false} | ||
|
||
it 'gets a hundred of both featured and unfeatured pages in a reversed order if requested without an id' do | ||
get api_pages_path | ||
expect(response).to be_success | ||
# Includes both featured and unfeatured pages. | ||
expect(json).to include last_featured_page.as_json | ||
expect(json).to include last_mvp_page.as_json | ||
# Limits its reach to the latest hundred pages if there are more than a hundred pages to search through. | ||
expect(json.size).to eq(Page.count) | ||
end | ||
|
||
it 'gets a single page if searched by an id of a page that exists' do | ||
get api_page_path(id: last_mvp_page.id.to_s) | ||
expect(response).to be_success | ||
expect(json['id']).to match last_mvp_page.id | ||
end | ||
|
||
it 'gets a single page if searched by a slug of a page that exists' do | ||
get api_page_path(id: last_featured_page.slug) | ||
expect(response).to be_success | ||
expect(json['id']).to match last_featured_page.id | ||
end | ||
|
||
it 'returns an error if searching for an ID or slug of a page that does not exist' do | ||
get api_page_path(id: last_featured_page.slug + '_epidemic') | ||
expect(response.status).to eq(404) | ||
expect(json).to match({ "errors" => "No record was found with that slug or ID."}) | ||
end | ||
describe 'GET index' do | ||
before do | ||
create(:page, active: true, title: 'Foo', content: 'Bar') | ||
end | ||
|
||
context 'with languages' do | ||
describe 'with language that does not exist' do | ||
it 'returns json with error' do | ||
get api_pages_path, {language: 'klingon'} | ||
expect(json).to match({"errors" => "The language you requested is not supported."}) | ||
expect(response.status).to eq(404) | ||
end | ||
end | ||
end | ||
subject { JSON.parse(response.body) } | ||
|
||
before { get('/api/pages.json') } | ||
|
||
describe 'with languages that exist' do | ||
it 'returns list of pages' do | ||
expect(subject.size).to eq(1) | ||
|
||
include_context "shared language pages" do | ||
[:de,:fr,:en,:es].each do |language_code| | ||
it "in #{language_code}, it gets pages only in that language" do | ||
get api_pages_path, { language: language_code.to_s } | ||
expect(json).to include(@page_hash[language_code][:featured].first.as_json) | ||
expect(json).to include(@page_hash[language_code][:ordinary].first.as_json) | ||
end | ||
end | ||
end | ||
expect(subject.first.keys).to match( | ||
%w{id title slug content created_at updated_at active featured action_count language} | ||
) | ||
|
||
expect(subject.first.symbolize_keys).to include({ | ||
title: 'Foo', | ||
content: 'Bar' | ||
}) | ||
end | ||
end | ||
|
||
describe 'GET featured' do | ||
context 'with no specified language' do | ||
let!(:featured_page) { create(:page, featured: true) } | ||
let!(:mvp_page) { create(:page, featured: false) } | ||
|
||
it 'gets only featured pages' do | ||
get api_pages_featured_path | ||
expect(json.size).to eq(1) | ||
expect(json.first['id']).to eq(featured_page.id) | ||
end | ||
before do | ||
create(:page, featured: true, active: true, title: 'Foo', content: 'Bar') | ||
create(:page, featured: false) | ||
end | ||
|
||
context 'with languages' do | ||
describe 'with language that does not exist' do | ||
it 'returns json with error' do | ||
get api_pages_featured_path, {language: 'klingon'} | ||
expect(json).to match({"errors" => "The language you requested is not supported."}) | ||
expect(response.status).to eq(404) | ||
end | ||
end | ||
|
||
describe 'with languages that exist' do | ||
include_context "shared language pages" do | ||
[:de,:fr,:en,:es].each.each do |language_code| | ||
it "in #{language_code}, it gets pages only in that language" do | ||
get api_pages_featured_path, { language: language_code.to_s } | ||
expect(json.first['language_id']).to match(@page_hash[language_code][:featured].first.language_id) | ||
end | ||
end | ||
end | ||
end | ||
subject { JSON.parse(response.body) } | ||
|
||
before { get( featured_api_pages_path(format: :json)) } | ||
|
||
it 'returns list of pages' do | ||
expect(subject.size).to eq(1) | ||
|
||
expect(subject.first.keys).to match( | ||
%w{id title slug content created_at updated_at active featured action_count language} | ||
) | ||
|
||
expect(subject.first.symbolize_keys).to include({ | ||
title: 'Foo', | ||
content: 'Bar' | ||
}) | ||
end | ||
end | ||
|
||
describe 'GET show' do | ||
let(:page) { create(:page, title: 'Foo', content: 'Bar') } | ||
|
||
subject { JSON.parse(response.body) } | ||
|
||
before { get( api_page_path(page, format: :json) ) } | ||
|
||
it 'returns page' do | ||
expect(subject.keys).to match( | ||
%w{id title slug content created_at updated_at active featured action_count language} | ||
) | ||
|
||
expect(subject.symbolize_keys).to include({ | ||
title: 'Foo', | ||
id: page.id | ||
}) | ||
end | ||
end | ||
end |
Oops, something went wrong.