Skip to content

Commit c83bd03

Browse files
Merge pull request #1 from the-bugs/feat/user-scaffold
Feat/user scaffold
2 parents 98430a0 + 9d8ed6f commit c83bd03

File tree

9 files changed

+128
-0
lines changed

9 files changed

+128
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# Ignore bundler config.
88
/.bundle
99

10+
# Ignore bundle files
11+
/vendor/bundle /*
12+
1013
# Ignore the default SQLite database.
1114
/db/*.sqlite3
1215
/db/*.sqlite3-*

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ GEM
107107
net-smtp (0.3.3)
108108
net-protocol
109109
nio4r (2.5.9)
110+
nokogiri (1.15.4-arm64-darwin)
111+
racc (~> 1.4)
110112
nokogiri (1.15.4-x86_64-linux)
111113
racc (~> 1.4)
112114
psych (5.1.0)
@@ -150,6 +152,7 @@ GEM
150152
psych (>= 4.0.0)
151153
reline (0.3.8)
152154
io-console (~> 0.5)
155+
sqlite3 (1.6.4-arm64-darwin)
153156
sqlite3 (1.6.4-x86_64-linux)
154157
stringio (3.0.8)
155158
thor (1.2.2)
@@ -162,6 +165,7 @@ GEM
162165
zeitwerk (2.6.11)
163166

164167
PLATFORMS
168+
arm64-darwin-22
165169
x86_64-linux
166170

167171
DEPENDENCIES

app/controllers/users_controller.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class UsersController < ApplicationController
2+
before_action :set_user, only: %i[ show update destroy ]
3+
4+
# GET /users
5+
def index
6+
@users = User.all
7+
8+
render json: @users
9+
end
10+
11+
# GET /users/1
12+
def show
13+
render json: @user
14+
end
15+
16+
# POST /users
17+
def create
18+
@user = User.new(user_params)
19+
20+
if @user.save
21+
render json: @user, status: :created, location: @user
22+
else
23+
render json: @user.errors, status: :unprocessable_entity
24+
end
25+
end
26+
27+
# PATCH/PUT /users/1
28+
def update
29+
if @user.update(user_params)
30+
render json: @user
31+
else
32+
render json: @user.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /users/1
37+
def destroy
38+
@user.destroy
39+
end
40+
41+
private
42+
# Use callbacks to share common setup or constraints between actions.
43+
def set_user
44+
@user = User.find(params[:id])
45+
end
46+
47+
# Only allow a list of trusted parameters through.
48+
def user_params
49+
params.require(:user).permit(:name, :email, :password)
50+
end
51+
end

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class User < ApplicationRecord
2+
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
resources :users
23
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
34

45
# Defines the root path route ("/")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateUsers < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :users do |t|
4+
t.string :name
5+
t.string :email
6+
t.string :password
7+
8+
t.timestamps
9+
end
10+
end
11+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "test_helper"
2+
3+
class UsersControllerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
@user = users(:one)
6+
end
7+
8+
test "should get index" do
9+
get users_url, as: :json
10+
assert_response :success
11+
end
12+
13+
test "should create user" do
14+
assert_difference("User.count") do
15+
post users_url, params: { user: { email: @user.email, name: @user.name, password: @user.password } }, as: :json
16+
end
17+
18+
assert_response :created
19+
end
20+
21+
test "should show user" do
22+
get user_url(@user), as: :json
23+
assert_response :success
24+
end
25+
26+
test "should update user" do
27+
patch user_url(@user), params: { user: { email: @user.email, name: @user.name, password: @user.password } }, as: :json
28+
assert_response :success
29+
end
30+
31+
test "should destroy user" do
32+
assert_difference("User.count", -1) do
33+
delete user_url(@user), as: :json
34+
end
35+
36+
assert_response :no_content
37+
end
38+
end

test/fixtures/users.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
name: MyString
5+
email: MyString
6+
password: MyString
7+
8+
two:
9+
name: MyString
10+
email: MyString
11+
password: MyString

test/models/user_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class UserTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)