add user scaffold
This commit is contained in:
2
Gemfile
2
Gemfile
@@ -5,6 +5,7 @@ ruby "3.1.1"
|
|||||||
gem "rails", "~> 7.0.2", ">= 7.0.2.2"
|
gem "rails", "~> 7.0.2", ">= 7.0.2.2"
|
||||||
gem "pg", "~> 1.1"
|
gem "pg", "~> 1.1"
|
||||||
gem "puma", "~> 5.0"
|
gem "puma", "~> 5.0"
|
||||||
|
gem "jbuilder"
|
||||||
|
|
||||||
gem "bootsnap", require: false
|
gem "bootsnap", require: false
|
||||||
|
|
||||||
@@ -13,5 +14,4 @@ group :development, :test do
|
|||||||
|
|
||||||
gem "rspec-rails", "~> 5.1"
|
gem "rspec-rails", "~> 5.1"
|
||||||
gem "factory_bot_rails", "~> 6.2"
|
gem "factory_bot_rails", "~> 6.2"
|
||||||
gem "shoulda-matchers", "~> 5.1"
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -90,6 +90,9 @@ GEM
|
|||||||
io-wait (0.2.1)
|
io-wait (0.2.1)
|
||||||
irb (1.4.1)
|
irb (1.4.1)
|
||||||
reline (>= 0.3.0)
|
reline (>= 0.3.0)
|
||||||
|
jbuilder (2.11.5)
|
||||||
|
actionview (>= 5.0.0)
|
||||||
|
activesupport (>= 5.0.0)
|
||||||
loofah (2.14.0)
|
loofah (2.14.0)
|
||||||
crass (~> 1.0.2)
|
crass (~> 1.0.2)
|
||||||
nokogiri (>= 1.5.9)
|
nokogiri (>= 1.5.9)
|
||||||
@@ -190,6 +193,7 @@ DEPENDENCIES
|
|||||||
bootsnap
|
bootsnap
|
||||||
debug
|
debug
|
||||||
factory_bot_rails (~> 6.2)
|
factory_bot_rails (~> 6.2)
|
||||||
|
jbuilder
|
||||||
pg (~> 1.1)
|
pg (~> 1.1)
|
||||||
puma (~> 5.0)
|
puma (~> 5.0)
|
||||||
rails (~> 7.0.2, >= 7.0.2.2)
|
rails (~> 7.0.2, >= 7.0.2.2)
|
||||||
|
|||||||
53
app/controllers/users_controller.rb
Normal file
53
app/controllers/users_controller.rb
Normal 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
10
app/models/user.rb
Normal 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
|
||||||
2
app/views/users/_user.json.jbuilder
Normal file
2
app/views/users/_user.json.jbuilder
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
json.extract! user, :id, :username, :created_at, :updated_at
|
||||||
|
json.url user_url(user, format: :json)
|
||||||
1
app/views/users/index.json.jbuilder
Normal file
1
app/views/users/index.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
|||||||
|
json.array! @users, partial: "users/user", as: :user
|
||||||
1
app/views/users/show.json.jbuilder
Normal file
1
app/views/users/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
|||||||
|
json.partial! "users/user", user: @user
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
Rails.application.routes.draw do
|
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 ("/")
|
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||||
# root "articles#index"
|
|
||||||
end
|
end
|
||||||
|
|||||||
9
db/migrate/20220227172543_create_users.rb
Normal file
9
db/migrate/20220227172543_create_users.rb
Normal 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
9
db/schema.rb
generated
@@ -10,8 +10,15 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
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
|
end
|
||||||
|
|||||||
10
db/seeds.rb
10
db/seeds.rb
@@ -1,7 +1,3 @@
|
|||||||
# This file should contain all the record creation needed to seed the database with its default values.
|
User.find_or_create_by(username: 'xpto')
|
||||||
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
|
User.find_or_create_by(username: 'admin')
|
||||||
#
|
User.find_or_create_by(username: 'geonizeli')
|
||||||
# Examples:
|
|
||||||
#
|
|
||||||
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
|
|
||||||
# Character.create(name: "Luke", movie: movies.first)
|
|
||||||
5
spec/factories/users.rb
Normal file
5
spec/factories/users.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :user do
|
||||||
|
username { "xpto" }
|
||||||
|
end
|
||||||
|
end
|
||||||
36
spec/models/user_spec.rb
Normal file
36
spec/models/user_spec.rb
Normal 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
|
||||||
@@ -34,6 +34,8 @@ RSpec.configure do |config|
|
|||||||
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
||||||
config.fixture_path = "#{::Rails.root}/spec/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
|
# 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
|
# examples within a transaction, remove the following line or assign false
|
||||||
# instead of true.
|
# instead of true.
|
||||||
@@ -62,10 +64,3 @@ RSpec.configure do |config|
|
|||||||
# arbitrary gems may also be filtered via:
|
# arbitrary gems may also be filtered via:
|
||||||
# config.filter_gems_from_backtrace("gem name")
|
# config.filter_gems_from_backtrace("gem name")
|
||||||
end
|
end
|
||||||
|
|
||||||
Shoulda::Matchers.configure do |config|
|
|
||||||
config.integrate do |with|
|
|
||||||
with.test_framework :rspec
|
|
||||||
with.library :rails
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|||||||
17
spec/requests/users_spec.rb
Normal file
17
spec/requests/users_spec.rb
Normal 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
|
||||||
9
spec/routing/users_routing_spec.rb
Normal file
9
spec/routing/users_routing_spec.rb
Normal 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
|
||||||
Reference in New Issue
Block a user