Skip to content

Commit

Permalink
Configuração das tabelas Users e Cargos
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzer0 committed May 18, 2024
1 parent 5728cfd commit 5091a31
Show file tree
Hide file tree
Showing 26 changed files with 245 additions and 109 deletions.
3 changes: 3 additions & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"

# Gem para .env
gem 'dotenv', groups: [:development, :test]

# Use Redis adapter to run Action Cable in production
# gem "redis", ">= 4.0.1"

Expand Down
2 changes: 2 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ GEM
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
dotenv (3.1.2)
drb (2.2.1)
erubi (1.12.0)
globalid (1.2.1)
Expand Down Expand Up @@ -255,6 +256,7 @@ DEPENDENCIES
bootsnap
capybara
debug
dotenv
importmap-rails
jbuilder
pg (~> 1.1)
Expand Down
70 changes: 70 additions & 0 deletions backend/app/controllers/cargos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class CargosController < ApplicationController
before_action :set_cargo, only: %i[ show edit update destroy ]

# GET /cargos or /cargos.json
def index
@cargos = Cargo.all
end

# GET /cargos/1 or /cargos/1.json
def show
end

# GET /cargos/new
def new
@cargo = Cargo.new
end

# GET /cargos/1/edit
def edit
end

# POST /cargos or /cargos.json
def create
@cargo = Cargo.new(cargo_params)

respond_to do |format|
if @cargo.save
format.html { redirect_to cargo_url(@cargo), notice: "Cargo was successfully created." }
format.json { render :show, status: :created, location: @cargo }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @cargo.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /cargos/1 or /cargos/1.json
def update
respond_to do |format|
if @cargo.update(cargo_params)
format.html { redirect_to cargo_url(@cargo), notice: "Cargo was successfully updated." }
format.json { render :show, status: :ok, location: @cargo }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @cargo.errors, status: :unprocessable_entity }
end
end
end

# DELETE /cargos/1 or /cargos/1.json
def destroy
@cargo.destroy!

respond_to do |format|
format.html { redirect_to cargos_url, notice: "Cargo was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_cargo
@cargo = Cargo.find(params[:id])
end

# Only allow a list of trusted parameters through.
def cargo_params
params.require(:cargo).permit(:cargos)
end
end
2 changes: 2 additions & 0 deletions backend/app/helpers/cargos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CargosHelper
end
2 changes: 2 additions & 0 deletions backend/app/models/cargo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Cargo < ApplicationRecord
end
1 change: 1 addition & 0 deletions backend/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class User < ApplicationRecord
belongs_to :cargo
end
32 changes: 0 additions & 32 deletions backend/app/views/users/_form.html.erb

This file was deleted.

17 changes: 0 additions & 17 deletions backend/app/views/users/_user.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions backend/app/views/users/_user.json.jbuilder

This file was deleted.

10 changes: 0 additions & 10 deletions backend/app/views/users/edit.html.erb

This file was deleted.

14 changes: 0 additions & 14 deletions backend/app/views/users/index.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion backend/app/views/users/index.json.jbuilder

This file was deleted.

9 changes: 0 additions & 9 deletions backend/app/views/users/new.html.erb

This file was deleted.

10 changes: 0 additions & 10 deletions backend/app/views/users/show.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion backend/app/views/users/show.json.jbuilder

This file was deleted.

4 changes: 4 additions & 0 deletions backend/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative "boot"

require "rails/all"
require "dotenv"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand All @@ -23,5 +24,8 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")

#dotenv config
Dotenv.load
end
end
11 changes: 5 additions & 6 deletions backend/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ default: &default

development:
<<: *default
database: backend_development

database: <%= ENV['POSTGRES_DB'] %>
# The specified database role being used to connect to PostgreSQL.
# To create additional roles in PostgreSQL see `$ createuser --help`.
# When left blank, PostgreSQL will use the default role. This is
# the same name as the operating system user running Rails.
#username: backend
username: <%= ENV['POSTGRES_USER'] %>

# The password associated with the PostgreSQL role (username).
#password:
password: <%= ENV['POSTGRES_PASSWORD'] %>

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
Expand Down
1 change: 1 addition & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :cargos
resources :users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
9 changes: 9 additions & 0 deletions backend/db/migrate/20240518231634_create_cargos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateCargos < ActiveRecord::Migration[7.1]
def change
create_table :cargos do |t|
t.string :type

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CreateUserJoinTableCargos < ActiveRecord::Migration[7.1]
def change
add_reference :users, :cargos, foreign_key: true
end
end
34 changes: 34 additions & 0 deletions backend/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions backend/test/controllers/cargos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class CargosControllerTest < ActionDispatch::IntegrationTest
setup do
@cargo = cargos(:one)
end

test "should get index" do
get cargos_url
assert_response :success
end

test "should get new" do
get new_cargo_url
assert_response :success
end

test "should create cargo" do
assert_difference("Cargo.count") do
post cargos_url, params: { cargo: { cargos: @cargo.cargos } }
end

assert_redirected_to cargo_url(Cargo.last)
end

test "should show cargo" do
get cargo_url(@cargo)
assert_response :success
end

test "should get edit" do
get edit_cargo_url(@cargo)
assert_response :success
end

test "should update cargo" do
patch cargo_url(@cargo), params: { cargo: { cargos: @cargo.cargos } }
assert_redirected_to cargo_url(@cargo)
end

test "should destroy cargo" do
assert_difference("Cargo.count", -1) do
delete cargo_url(@cargo)
end

assert_redirected_to cargos_url
end
end
7 changes: 7 additions & 0 deletions backend/test/fixtures/cargos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
cargos: MyString

two:
cargos: MyString
7 changes: 7 additions & 0 deletions backend/test/models/cargo_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CargoTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Loading

0 comments on commit 5091a31

Please sign in to comment.