Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Remove body from posts index api #1

Merged
merged 7 commits into from Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion Gemfile
Expand Up @@ -12,11 +12,15 @@ gem 'rack-cors'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
end

group :development do
gem 'listen', '~> 3.3'
end

group :test do
gem 'rspec-rails'
gem "factory_bot_rails"
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -69,6 +69,11 @@ GEM
crass (1.0.6)
diff-lcs (1.4.4)
erubi (1.10.0)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faraday (1.3.0)
faraday-net_http (~> 1.0)
multipart-post (>= 1.2, < 3)
Expand Down Expand Up @@ -195,6 +200,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
byebug
factory_bot_rails
listen (~> 3.3)
pg
puma
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/posts_controller.rb
Expand Up @@ -2,12 +2,12 @@ class PostsController < ApplicationController
skip_before_action :require_admin_login, only: [:index, :show]

def index
@posts = Post.order(published_at: :desc)
@posts = Post.list
render json: @posts
end

def show
@post = Post.find(params[:post_id])
@post = Post.find(params[:id])
render json: @post
end

Expand All @@ -17,13 +17,13 @@ def create
end

def update
post = Post.find(params[:post_id])
post = Post.find(params[:id])
post.update!(post_params)
head :ok
end

def destroy
post = Post.find(params[:post_id])
post = Post.find(params[:id])
post.destroy!
head :no_content
end
Expand Down
1 change: 1 addition & 0 deletions app/models/post.rb
@@ -1,2 +1,3 @@
class Post < ApplicationRecord
scope :list, -> { order(published_at: :desc).select(:id, :title, :published_at) }
end
6 changes: 3 additions & 3 deletions config/routes.rb
Expand Up @@ -10,8 +10,8 @@

# posts
get "/posts" => "posts#index"
get "/posts/:post_id" => "posts#show"
get "/posts/:id" => "posts#show"
post "/posts" => "posts#create"
put "/posts/:post_id" => "posts#update"
delete "/posts/:post_id" => "posts#destroy"
put "/posts/:id" => "posts#update"
delete "/posts/:id" => "posts#destroy"
end
7 changes: 7 additions & 0 deletions spec/factories/posts.rb
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :post do
sequence(:title) { |n| "test title #{n}" }
sequence(:body) { |n| "test body #{n}" }
published_at { Time.zone.now }
end
end
19 changes: 19 additions & 0 deletions spec/models/post_spec.rb
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe Post, type: :model do
describe "list scope" do
subject { Post.list }

let!(:posts) do
FactoryBot.create_list(:post, 10)
end

it "loads required fields" do
expect(subject).to all(have_attributes(title: be_a(String), published_at: ActiveSupport::TimeWithZone))
end

it "does not load :body field" do
expect(subject.first).not_to respond_to(:body)
end
end
end
11 changes: 11 additions & 0 deletions spec/requests/posts_spec.rb
@@ -0,0 +1,11 @@
require "rails_helper"

RSpec.describe PostsController, type: :request do
describe "#index" do
it "returns an index of posts" do
get "/posts"
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to be_kind_of(Array)
end
end
end
26 changes: 26 additions & 0 deletions spec/routing/posts_routing_spec.rb
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe PostsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "/posts").to route_to("posts#index")
end

it "routes to #show" do
expect(get: "/posts/1").to route_to("posts#show", id: "1")
end


it "routes to #create" do
expect(post: "/posts").to route_to("posts#create")
end

it "routes to #update via PUT" do
expect(put: "/posts/1").to route_to("posts#update", id: "1")
end

it "routes to #destroy" do
expect(delete: "/posts/1").to route_to("posts#destroy", id: "1")
end
end
end
30 changes: 0 additions & 30 deletions spec/routing/users_routing_spec.rb

This file was deleted.

3 changes: 3 additions & 0 deletions spec/support/factory_bot.rb
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end