Skip to content

Commit

Permalink
Merge pull request #3 from monade/default-actions-with-options
Browse files Browse the repository at this point in the history
Default actions with options
  • Loading branch information
ProGM authored Oct 20, 2023
2 parents 288cf04 + 32b7afa commit 0a757aa
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 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
5 changes: 3 additions & 2 deletions lib/solipsist/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def default!(model, options = {}, &block)
when 'create'
_implicit_create_action(model, options, &block)
when 'index', 'show'
render_default!(model, options = {}, &block)
render_default!(model, options, &block)
else
raise "No implicit action for #{params[:action]}"
end
Expand All @@ -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
21 changes: 20 additions & 1 deletion spec/solipsist/mass_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 ArticlesController, type: :controller do
let(:json_body) { JSON.parse(response.body) }

Expand All @@ -25,7 +28,7 @@
it 'creates an item' do
expect do
post :create, params: { title: '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 { Article.count }.by(1)
end
Expand All @@ -40,6 +43,21 @@
expect(json_body.dig('data', 1, 'attributes', 'title')).to eq('123123')
end.to change { Article.count }.by(2)
end

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

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

context 'PUT /articles/:id' do
Expand Down Expand Up @@ -83,3 +101,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 0a757aa

Please sign in to comment.