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

Exercicio de pull request #3

Open
wants to merge 3 commits into
base: main
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
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.8-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.8-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
popper_js (2.11.6)
public_suffix (5.0.0)
Expand Down Expand Up @@ -206,6 +208,7 @@ GEM
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.5.2-arm64-darwin)
sqlite3 (1.5.2-x86_64-linux)
thor (1.2.1)
tilt (2.0.11)
timeout (0.3.0)
Expand All @@ -222,6 +225,7 @@ GEM

PLATFORMS
arm64-darwin-21
x86_64-linux

DEPENDENCIES
bootstrap (~> 5.2.1)
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class ApplicationController < ActionController::Base


before_action :authenticate_user!
end
21 changes: 0 additions & 21 deletions app/controllers/product_categories_controller.rb

This file was deleted.

56 changes: 4 additions & 52 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,13 @@
class ProductsController < ApplicationController
before_action :set_product, only: %i[ show edit update destroy ]

# GET /products


def index
@products = Product.all
@product = Product.new
@product_category = ProductCategory.new
@product_categories = ProductCategory.all
end

# GET /products/1
def show
end

# GET /products/new
def new
@product = Product.new
@product_categories = ProductCategory.all
end

# GET /products/1/edit
def edit
end

# POST /products
def create
@product = Product.new(product_params)

if @product.save
redirect_to @product, notice: "Product was successfully created."
else
render :new, status: :unprocessable_entity
end
end

# PATCH/PUT /products/1
def update
if @product.update(product_params)
redirect_to @product, notice: "Product was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /products/1
def destroy
@product.destroy
redirect_to products_url, notice: "Product was successfully destroyed."
end

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

# Only allow a list of trusted parameters through.
def product_params
params.require(:product).permit(:name, :price, :product_category_id)
end
end
end
9 changes: 2 additions & 7 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
class WelcomeController < ApplicationController


def index

end


skip_before_action :authenticate_user!
def index; end
end
1 change: 0 additions & 1 deletion app/views/product_categories/_product_category.html.erb

This file was deleted.

27 changes: 0 additions & 27 deletions app/views/products/_form.html.erb

This file was deleted.

12 changes: 0 additions & 12 deletions app/views/products/_product.html.erb

This file was deleted.

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

This file was deleted.

50 changes: 0 additions & 50 deletions app/views/products/index.html.erb

This file was deleted.

17 changes: 11 additions & 6 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<h1>New product</h1>
<%= form_with(model: @product) do |product| %>

<%= product.label :name, 'Nome' %>
<%= product.text_field :name %>

<%= render "form", product: @product %>
<%= product.label :price, 'Preço' %>
<%= product.number_field :price %>

<br>
<%= product.label :product_category_id, 'Categoria do Produto' %>
<%= product.collection_select :product_category_id, @product_categories, :id, :name %>

<div>
<%= link_to "Back to products", products_path %>
</div>
<%= product.submit 'Enviar' %>

<% end %>
12 changes: 0 additions & 12 deletions app/views/products/show.html.erb

This file was deleted.

43 changes: 2 additions & 41 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,42 +1,3 @@
<h1><%= translate('.title') %></h1>
<h1>Produtos</h1>

<div class="row">
<div class="col-4">
<%= image_tag 'delivery.png', class: 'img-fluid' %>
</div>

<div class="col-9">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
</div>


<%= link_to 'Início', root_path, class: 'btn btn-primary btn-lg' %>
<%= link_to "Cadastrar Produto", new_product_path %>
3 changes: 0 additions & 3 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ en:
outro:
outro:
outro:


I18n.translate('transport_mode.error.not_available')
6 changes: 2 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Rails.application.routes.draw do
resources :products
resources :product_categories, only: [:create, :show] do
resources :products, only: [:create]
end
resources :products, only: [:index, :new,:create]
resources :product_categories, only: [:create, :show]
devise_for :users
root 'welcome#index'
end
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
Expand Down
1 change: 1 addition & 0 deletions spec/support/devise_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include Warden::Test::Helpers
24 changes: 24 additions & 0 deletions spec/system/product/user_creates_product_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

describe 'Usuario acessa página de cadastrar produto' do
it 'e não está autenticado' do


visit root_path
click_on 'Cadastrar Produto'

expect(current_path).to eq new_user_session_path
end
it 'com sucesso quando está autenticado' do
user = User.create!(email: "[email protected]", password: "password")
product = ProductCategory.create!(name: 'Categoria_1')

login_as(user)
visit root_path
click_on 'Cadastrar Produto'

expect(page).to have_field 'Nome'
expect(page).to have_field 'Preço'
expect(page).to have_field 'Categoria do Produto'
end
end