Skip to content

Commit

Permalink
feat: default status for POST is 201
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Lecchi committed Oct 6, 2023
1 parent 288cf04 commit c4ab323
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/Gemfile.lock
/log
/tmp
.tool-versions
.vscode
16 changes: 16 additions & 0 deletions .rubocop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require:
- rubocop-rails

Rails/SaveBang:
Enabled: true
Severity: error

Style/ClassAndModuleChildren:
Enabled: false

Metrics/BlockLength:
Exclude:
- "spec/**/*"

Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ gem 'solargraph'
gem 'yard'
gem 'sqlite3'
gem 'rails'
gem 'rubocop-rails', require: false
3 changes: 2 additions & 1 deletion lib/solipsist/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def _implicit_destroy_action(model, options)
# @param [Hash] options
def _implicit_create_action(model, options)
model.save!
block_given? ? yield : _implicit_render(model, options)
merged_options = options.include?(:status) ? options : options.merge(status: :created)
block_given? ? yield : _implicit_render(model, merged_options)
end

# @param [Object] model
Expand Down
26 changes: 25 additions & 1 deletion spec/solipsist/actions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require 'spec_helper'

# rubocop:disable Metrics/BlockLength
describe PeopleController, type: :controller do
let(:json_body) { JSON.parse(response.body) }

Expand All @@ -22,13 +25,33 @@
end

context 'POST /people' do
it 'default http status is 201' do
post :create, params: { name: 'aaaaaaa' }
expect(response).to have_http_status(:created)
end

it 'creates an item' do
expect do
post :create, params: { name: 'aaaaaaa' }
expect(response).to have_http_status(:ok)
expect(response).to have_http_status(:created)
expect(json_body.dig('data', 'id')).to be_a(String)
end.to change { Person.count }.by(1)
end

context 'custom status' do
before do
class PeopleController < ApplicationController
def create
default! @person, { status: :ok }
end
end
end

it 'can use custom status' do
post :create, params: { name: 'aaaaaaa' }
expect(response).to have_http_status(:ok)
end
end
end

context 'PUT /people/:id' do
Expand All @@ -54,3 +77,4 @@
end
end
end
# rubocop:enable Metrics/BlockLength
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Application < Rails::Application
RSpec::Expectations.configuration.warn_about_potential_false_positives = false

Rails.application.config.eager_load = false
Rails.application.config.active_record.legacy_connection_handling = false

Dir[File.expand_path('../support/*.rb', __FILE__)].each { |f| require f }

Expand Down
1 change: 1 addition & 0 deletions spec/support/controllers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
include ActionController::MimeResponds
Expand Down

0 comments on commit c4ab323

Please sign in to comment.