From 5eade11e41578722790f8319796bce458fb282f7 Mon Sep 17 00:00:00 2001 From: tuuli Date: Thu, 14 Jul 2016 18:36:09 +0300 Subject: [PATCH] Simplify things by using show for a single page and index for a collection --- app/controllers/api/pages_controller.rb | 20 ++++++++++---------- config/routes.rb | 2 -- spec/requests/api/pages_spec.rb | 10 +++++----- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/pages_controller.rb b/app/controllers/api/pages_controller.rb index bb6d326d0..06ac0453e 100644 --- a/app/controllers/api/pages_controller.rb +++ b/app/controllers/api/pages_controller.rb @@ -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 @@ -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) diff --git a/config/routes.rb b/config/routes.rb index 2c7decdcc..27efe9ebe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -134,8 +134,6 @@ namespace :pages do get 'featured/', action: 'show_featured' - get '/', action: 'show' - get '/:page_id', action: 'show' end resources :pages do diff --git a/spec/requests/api/pages_spec.rb b/spec/requests/api/pages_spec.rb index f197751c4..c72922d71 100644 --- a/spec/requests/api/pages_spec.rb +++ b/spec/requests/api/pages_spec.rb @@ -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} @@ -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