Skip to content
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
9 changes: 7 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class ApplicationController < ActionController::Base


def authenticate_user!
if user_signed_in?
super
else
redirect_to new_user_session_path, :notice => 'Realize login para continuar ou crie uma nova conta'
end
end
end
3 changes: 2 additions & 1 deletion app/controllers/product_categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class ProductCategoriesController < ApplicationController
def create
ProductCategory.create!(name: params.require(:product_category).permit(:name))
ProductCategory.create!(params.require(:product_category).permit(:name))
redirect_to products_path, notice: 'Categoria cadastrada'
end


def search
nome_a_buscar = params[:busca]
ProductCategory.where("name LIKE :param_busca OR outro_campo LIKE :param_busca OR mais_um_campo LIKE :param_busca ",
Expand Down
27 changes: 14 additions & 13 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class ProductsController < ApplicationController
before_action :set_product, only: %i[ show edit update destroy ]
before_action :product_params, only:[:create, :update]
before_action :authenticate_user!

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

# GET /products/1
Expand All @@ -25,11 +24,11 @@ def edit
# POST /products
def create
@product = Product.new(product_params)

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

Expand All @@ -50,12 +49,14 @@ def destroy

private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
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

# Only allow a list of trusted parameters through.
def product_params
params.require(:product).permit(:name, :price, :product_category_id)
end
end

3 changes: 0 additions & 3 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
class WelcomeController < ApplicationController


def index

end


end
3 changes: 2 additions & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Product < ApplicationRecord
belongs_to :product_category, optional: true
belongs_to :product_category
validates :name, :price, :product_category, presence: true
end


Expand Down
1 change: 1 addition & 0 deletions app/models/product_category.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class ProductCategory < ApplicationRecord
has_many :products
validates :name, presence: true
end
39 changes: 3 additions & 36 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,9 @@
</head>

<body>
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<%= form_with(url: root_path, class: 'd-flex') do |f| %>
<%= f.text_field :busca, class: 'form-control me-2' %>
<%= f.submit 'Buscar', class: 'btn btn-outline-success' %>
<% end %>
</div>
</div>
</nav>
<!-- <nav>
<%= link_to 'Produtos', products_path %>
</nav> -->

<div class="container">
<%= notice %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/products/_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% if @product.errors.any? %>
<div style="color: red">
<ul>
<% @product.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
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.

35 changes: 4 additions & 31 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<p style="color: green"><%= notice %></p>

<h1>Products</h1>
<h1>Produtos</h1>
<div id="products">
<% @products.each do |product| %>
<%= render product %>
<p>
<%= link_to "Show this product", product %>
<%= link_to "Ver produto", product %>
</p>
<% end %>
</div>
Expand All @@ -18,33 +17,7 @@
<% end %>
<hr/>

<%= link_to 'Cadastrar produto', new_product_path %>
<%= link_to 'Voltar', root_path %>


<h2>Novo Produto</h2>
<%= form_with(model: @product) do |form| %>
<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :price, style: "display: block" %>
<%= form.text_field :price %>
</div>

<div>
<%= form.label :condition_new, 'Produto Novo' %>
<%= form.radio_button :condition, :new %>

<%= form.label :condition_used, 'Produto Usado' %>
<%= form.radio_button :condition, :used %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>


choose 'Produto Usado'

28 changes: 25 additions & 3 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
<h1>New product</h1>
<%= render 'error' %>

<%= render "form", product: @product %>
<h2>Novo Produto</h2>

<%= form_with(model: @product) do |form| %>
<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :price, style: "display: block" %>
<%= form.text_field :price %>
</div>

<div>
<%= form.label :product_category_id, style: "display: block" %>
<%= form.collection_select :product_category_id, ProductCategory.all, :id, :name, include_blank:true %>
</div>

<div>
<%= form.submit 'Salvar' %>
</div>

<% end %>

<br>

<div>
<%= link_to "Back to products", products_path %>
<%= link_to "Voltar", products_path %>
</div>
17 changes: 11 additions & 6 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<p style="color: green"><%= notice %></p>

<%= render @product %>
<h1><%= @product.name %></h1>

<%= render @product.product_category %>
<dl>
<dt><%= Product.human_attribute_name(:name) %>:</dt>
<dd><%= @product.name %></dd>
<dt><%= Product.human_attribute_name(:price) %>:<dt>
<dd><%= number_to_currency(@product.price, :unit => "R$ ", :separator => ",", :delimiter => ".") %></dd>
<dt><%= Product.human_attribute_name(:product_category) %>:<dt>
<dd><%= @product.product_category.name %></dd>
<dl>

<div>
<%= link_to "Edit this product", edit_product_path(@product) %> |
<%= link_to "Back to products", products_path %>

<%= button_to "Destroy this product", @product, method: :delete %>
<%= link_to "Editar produto", edit_product_path(@product) %> |
<%= link_to "Voltar", products_path %>
</div>
6 changes: 5 additions & 1 deletion app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1><%= translate('.title') %></h1>
<h1>Sistema de Frete e Transportadoras</h1>

<div class="row">
<div class="col-4">
Expand Down Expand Up @@ -40,3 +40,7 @@


<%= link_to 'Início', root_path, class: 'btn btn-primary btn-lg' %>
<%= link_to 'Produtos', products_path, class: 'btn btn-primary btn-lg' %>
<%= link_to 'Sair', destroy_user_session_path, :method => :delete, class: 'btn btn-primary btn-lg' %>


4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ class Application < Rails::Application

# Don't generate system test files.
config.generators.system_tests = nil

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.locale = :'pt-BR'
config.i18n.default_locale = :'pt-BR'
end
end
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Enable server timing
config.server_timing = true

#config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join("tmp/caching-dev.txt").exist?
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
# config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
I18n.available_locales = ['pt-BR']
I18n.default_locale = :'pt-BR'
Loading