add user scaffold

This commit is contained in:
João Victor Geonizeli
2022-02-27 14:46:30 -03:00
parent cec57e0ad2
commit 011a8cdb77
16 changed files with 163 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ ruby "3.1.1"
gem "rails", "~> 7.0.2", ">= 7.0.2.2"
gem "pg", "~> 1.1"
gem "puma", "~> 5.0"
gem "jbuilder"
gem "bootsnap", require: false
@@ -13,5 +14,4 @@ group :development, :test do
gem "rspec-rails", "~> 5.1"
gem "factory_bot_rails", "~> 6.2"
gem "shoulda-matchers", "~> 5.1"
end

View File

@@ -90,6 +90,9 @@ GEM
io-wait (0.2.1)
irb (1.4.1)
reline (>= 0.3.0)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
loofah (2.14.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
@@ -190,6 +193,7 @@ DEPENDENCIES
bootsnap
debug
factory_bot_rails (~> 6.2)
jbuilder
pg (~> 1.1)
puma (~> 5.0)
rails (~> 7.0.2, >= 7.0.2.2)

View File

@@ -0,0 +1,53 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show update destroy ]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
if @user.save
render :show, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
if @user.update(user_params)
render :show, status: :ok, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:username)
end
end

10
app/models/user.rb Normal file
View File

@@ -0,0 +1,10 @@
class User < ApplicationRecord
validates :username,
presence: true,
uniqueness: true,
length: { maximum: 14 },
allow_nil: false,
format: {
with: /\A[a-zA-Z0-9]+\z/,
}
end

View File

@@ -0,0 +1,2 @@
json.extract! user, :id, :username, :created_at, :updated_at
json.url user_url(user, format: :json)

View File

@@ -0,0 +1 @@
json.array! @users, partial: "users/user", as: :user

View File

@@ -0,0 +1 @@
json.partial! "users/user", user: @user

View File

@@ -1,6 +1,5 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
resources :users, only: [:show]
# Defines the root path route ("/")
# root "articles#index"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
end

View File

@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :username, null: false, limit: 14, index: true
t.timestamps
end
end
end

9
db/schema.rb generated
View File

@@ -10,8 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 0) do
ActiveRecord::Schema[7.0].define(version: 2022_02_27_172543) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: :cascade do |t|
t.string "username", limit: 14, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["username"], name: "index_users_on_username"
end
end

View File

@@ -1,7 +1,3 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
# Character.create(name: "Luke", movie: movies.first)
User.find_or_create_by(username: 'xpto')
User.find_or_create_by(username: 'admin')
User.find_or_create_by(username: 'geonizeli')

5
spec/factories/users.rb Normal file
View File

@@ -0,0 +1,5 @@
FactoryBot.define do
factory :user do
username { "xpto" }
end
end

36
spec/models/user_spec.rb Normal file
View File

@@ -0,0 +1,36 @@
require 'rails_helper'
RSpec.describe User, type: :model do
describe 'validations' do
describe 'username' do
it 'presence' do
user = build(:user, username: nil)
expect(user).to_not be_valid
end
it 'uniqueness' do
create(:user, username: 'xpto')
user = build(:user, username: 'xpto')
expect(user).to_not be_valid
end
it 'max length 14' do
with_15_caracters = build(:user, username: 'asdfghjklzxcvhn')
with_14_caracters = build(:user, username: 'asdfghjklzxcvh')
expect(with_15_caracters).to_not be_valid
expect(with_14_caracters).to be_valid
end
it 'format' do
with_invalid_format = build(:user, username: 'asdfghjklzxcvh!')
with_valid_format = build(:user, username: 'asdfghjklzxcvh')
expect(with_invalid_format).to_not be_valid
expect(with_valid_format).to be_valid
end
end
end
end

View File

@@ -34,6 +34,8 @@ RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryBot::Syntax::Methods
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
@@ -62,10 +64,3 @@ RSpec.configure do |config|
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

View File

@@ -0,0 +1,17 @@
require 'rails_helper'
RSpec.describe "/users", type: :request do
let(:valid_attributes) {
{
username: 'geonizeli'
}
}
describe "GET /show" do
it "renders a successful response" do
user = User.create! valid_attributes
get user_url(user), as: :json
expect(response).to be_successful
end
end
end

View File

@@ -0,0 +1,9 @@
require "rails_helper"
RSpec.describe UsersController, type: :routing do
describe "routing" do
it "routes to #show" do
expect(get: "/users/1").to route_to("users#show", id: "1")
end
end
end