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

Refactor ProductsController. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.3'
ruby '2.6.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
Expand Down Expand Up @@ -40,7 +40,7 @@ group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'master'
gem lib
end
gem 'factory_bot_rails'
end
Expand All @@ -55,6 +55,8 @@ group :development do
end

group :test do
gem 'database_cleaner'

# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
Expand Down
119 changes: 32 additions & 87 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,50 +1,3 @@
GIT
remote: https://github.com/rspec/rspec-core.git
revision: b4af5c7ef75ee2194059e5dd6a7383718914e91a
branch: master
specs:
rspec-core (3.9.0.pre)
rspec-support (= 3.9.0.pre)

GIT
remote: https://github.com/rspec/rspec-expectations.git
revision: 12bca283fc60fc1da3851de704bb5ce6fe416d85
branch: master
specs:
rspec-expectations (3.9.0.pre)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (= 3.9.0.pre)

GIT
remote: https://github.com/rspec/rspec-mocks.git
revision: 55f0b116dc8c15a7acef9e7619bb0a3925bd18fa
branch: master
specs:
rspec-mocks (3.9.0.pre)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (= 3.9.0.pre)

GIT
remote: https://github.com/rspec/rspec-rails.git
revision: f227983ba2ec8c666d073f40f5a5f517510bd058
branch: master
specs:
rspec-rails (3.9.0.pre)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (= 3.9.0.pre)
rspec-expectations (= 3.9.0.pre)
rspec-mocks (= 3.9.0.pre)
rspec-support (= 3.9.0.pre)

GIT
remote: https://github.com/rspec/rspec-support.git
revision: 4579133922b6fb2a615a0dac0ae8d0b4f6fabfff
branch: master
specs:
rspec-support (3.9.0.pre)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -97,18 +50,15 @@ GEM
bindex (0.7.0)
bootsnap (1.4.4)
msgpack (~> 1.0)
bootsnap (1.4.4-java)
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
capybara (3.20.0)
capybara (3.21.0)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.2)
uglifier
regexp_parser (~> 1.5)
xpath (~> 3.2)
childprocess (1.0.1)
rake (< 13.0)
Expand All @@ -124,6 +74,7 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
database_cleaner (1.7.0)
diff-lcs (1.3)
erubi (1.8.0)
execjs (2.7.0)
Expand All @@ -132,10 +83,7 @@ GEM
factory_bot_rails (5.0.2)
factory_bot (~> 5.0.2)
railties (>= 4.2.0)
ffi (1.10.0)
ffi (1.10.0-java)
ffi (1.10.0-x64-mingw32)
ffi (1.10.0-x86-mingw32)
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.6.0)
Expand All @@ -160,24 +108,12 @@ GEM
mini_portile2 (2.4.0)
minitest (5.11.3)
msgpack (1.2.10)
msgpack (1.2.10-java)
msgpack (1.2.10-x64-mingw32)
msgpack (1.2.10-x86-mingw32)
nio4r (2.3.1)
nio4r (2.3.1-java)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
nokogiri (1.10.3-java)
nokogiri (1.10.3-x64-mingw32)
mini_portile2 (~> 2.4.0)
nokogiri (1.10.3-x86-mingw32)
mini_portile2 (~> 2.4.0)
pg (1.1.4)
pg (1.1.4-x64-mingw32)
pg (1.1.4-x86-mingw32)
public_suffix (3.0.3)
public_suffix (3.1.0)
puma (3.12.1)
puma (3.12.1-java)
rack (2.0.7)
rack-test (1.1.0)
rack (>= 1.0, < 3)
Expand Down Expand Up @@ -213,9 +149,26 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
regexp_parser (1.4.0)
regexp_parser (1.5.1)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-rails (3.8.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
ruby_dep (1.5.0)
rubyzip (1.2.2)
rubyzip (1.2.3)
sass (3.7.4)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand All @@ -227,7 +180,7 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selenium-webdriver (3.142.2)
selenium-webdriver (3.142.3)
childprocess (>= 0.5, < 2.0)
rubyzip (~> 1.2, >= 1.2.2)
shoulda-matchers (4.0.1)
Expand All @@ -246,15 +199,12 @@ GEM
sprockets (>= 3.0.0)
thor (0.20.3)
thread_safe (0.3.6)
thread_safe (0.3.6-java)
tilt (2.0.9)
turbolinks (5.2.0)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo-data (1.2019.1)
tzinfo (>= 1.0.0)
uglifier (4.1.20)
execjs (>= 0.3.0, < 3)
web-console (3.7.0)
Expand All @@ -264,37 +214,32 @@ GEM
railties (>= 5.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-driver (0.7.0-java)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
java
ruby
x64-mingw32
x86-mingw32
x86-mswin32

DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
capybara (>= 2.15)
chromedriver-helper
coffee-rails (~> 4.2)
database_cleaner
factory_bot_rails
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg
puma (~> 3.11)
rails (~> 5.2.3)
rails-controller-testing
rspec-core!
rspec-expectations!
rspec-mocks!
rspec-rails!
rspec-support!
rspec-core
rspec-expectations
rspec-mocks
rspec-rails
rspec-support
sass-rails (~> 5.0)
selenium-webdriver
shoulda-matchers
Expand All @@ -306,7 +251,7 @@ DEPENDENCIES
web-console (>= 3.3.0)

RUBY VERSION
ruby 2.5.3p105
ruby 2.6.3p62

BUNDLED WITH
2.0.1
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# README
### Learning Rails from the book Agile web development with Rails

This README would normally document whatever steps are necessary to get the
application up and running.
* Ruby version: 2.6.3

Things you may want to cover:
* Rails version: 5.2.3

* Ruby version
* Database: PostgreSQL

* System dependencies
* Database Configuration:

* Configuration
These two environment variables must be set:
```bash
export POSTGRES_USERNAME=<your postgres username>
export POSTGRES_PASSWORD=<your postgres password>
```

* Database creation
* Database creation and initialization:
```bash
rake db:create db:migrate
```

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...
* Run spec test:
```bash
rspec
```
42 changes: 42 additions & 0 deletions app/controllers/api/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Api::ProductsController < ::ProductsController
# GET /products.json
def index
@products = Product.all
render json: @products
end

# GET /products/1.json
def show
render json: @product
end

# POST /products.json
def create
perform_action({json_status: :created}) do
@product = ProductService.create_product!(product_params)
end
end

# PATCH/PUT /products/1.json
def update
perform_action({json_status: :ok}) do
@product = ProductService.update_product!(@product, product_params)
end
end

# DELETE /products/1.json
def destroy
@product.destroy
head :no_content
end

private

def perform_action(options)
yield
render json: @product, status: options[:json_status]
rescue ActiveRecord::RecordInvalid => error
Rails.logger.error("#{error.message}\n#{error.backtrace.join("\n")}")
render json: {error: error.message}, status: :unprocessable_entity
end
end
Loading