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

Commit

Permalink
Simplify things by using show for a single page and index for a colle…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
Tuuleh committed Jul 14, 2016
1 parent 421c7a3 commit 5eade11
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
20 changes: 10 additions & 10 deletions app/controllers/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Api::PagesController < ApplicationController

before_action :set_language, only: [:show, :show_featured]
before_action :set_language, only: [:show, :show_featured, :index]

layout false

Expand All @@ -21,26 +21,26 @@ def share_rows
end)
end

def index
pages = @language.present? ? pages_by_language : Page.all
render json: reduce_and_order(pages, 100)
end

def show
if params[:id].present?
render json: page
else
pages = @language.present? ? pages_by_language : Page.all
render json: reduce_and_order(pages, 100)
end
rescue ActiveRecord::RecordNotFound
render json: page
rescue ActiveRecord::RecordNotFound
render json: { errors: "No record was found with that slug or ID." }, status: 404
end

def show_featured
page_scope = @language.present? ? pages_by_language : Page
page_scope = @language.present? ? pages_by_language : Page.all
render json: page_scope.where(featured: true)
end

private

def pages_by_language
pages ||= Page.where(language: @language)
Page.where(language: @language)
end

def reduce_and_order(collection, count)
Expand Down
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@

namespace :pages do
get 'featured/', action: 'show_featured'
get '/', action: 'show'
get '/:page_id', action: 'show'
end

resources :pages do
Expand Down
10 changes: 5 additions & 5 deletions spec/requests/api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def json

describe 'GET pages' do
context 'with no specified language' do
let!(:featured_pages) { create_list :page, 50, featured: true }
let!(:mvp_pages) { create_list :page, 50, featured: false }
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}

Expand All @@ -34,19 +34,19 @@ def json
end

it 'gets a single page if searched by an id of a page that exists' do
get api_pages_path(id: last_mvp_page.id.to_s)
get api_page_path(id: last_mvp_page.id.to_s)
expect(response).to be_success
expect(json).to match last_mvp_page.as_json
end

it 'gets a single page if searched by a slug of a page that exists' do
get api_pages_path(id: last_featured_page.slug)
get api_page_path(id: last_featured_page.slug)
expect(response).to be_success
expect(json).to match last_featured_page.as_json
end

it 'returns an error if searching for an ID or slug of a page that does not exist' do
get api_pages_path(id: last_featured_page.slug + '_epidemic')
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
Expand Down

0 comments on commit 5eade11

Please sign in to comment.