Skip to content

Commit

Permalink
Merge pull request #292 from Purple-Stock/staging
Browse files Browse the repository at this point in the history
update the productions
  • Loading branch information
puppe1990 authored Aug 28, 2024
2 parents 230686c + 4e50f4a commit 869205e
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 65 deletions.
49 changes: 22 additions & 27 deletions app/controllers/productions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# app/controllers/productions_controller.rb

class ProductionsController < ApplicationController
before_action :set_production, only: [:show, :edit, :update, :destroy]
before_action :set_tailor_options, only: [:new, :edit, :create, :update]

def index
@productions = Production.all
@productions = Production.includes(:tailor, production_products: :product).all
end

def show
Expand All @@ -12,53 +13,47 @@ def show
def new
@production = Production.new
@production.production_products.build
end

def edit
@tailors = Tailor.all
end

def create
@production = Production.new(production_params)
if @production.save
redirect_to @production, notice: 'Production was successfully created.'
else
@tailors = Tailor.all
render :new
end
end

def edit
@tailors = Tailor.all
end

def update
if @production.update(production_params)
redirect_to @production, notice: 'Production was successfully updated.'
else
@tailors = Tailor.all
render :edit
end
end

def destroy
begin
ProductionProduct.where(production_id: @production.id).destroy_all
@production.destroy
respond_to do |format|
format.html { redirect_to productions_path, notice: 'Produção deletado.' }
format.turbo_stream { render turbo_stream: turbo_stream.remove(dom_id(@production)) }
end
rescue ActiveRecord::InvalidForeignKey
# Handle invalid foreign key by raising a custom error message
raise "Can't delete production because it has associated records"
end
@production.destroy
redirect_to productions_url, notice: 'Production was successfully destroyed.'
end

private

def set_production
@production = Production.find(params[:id])
end

def production_params
params.require(:production).permit(:cut_date, :deliver_date, :quantity, :tailor_id, :consider, production_products_attributes: [:id, :product_id, :quantity, :_destroy])
end
def set_production
@production = Production.find(params[:id])
end

def set_tailor_options
@tailors = Tailor.all
end
end
def production_params
params.require(:production).permit(
:tailor_id, :cut_date, :delivery_date, :consider,
production_products_attributes: [:id, :product_id, :quantity, :_destroy]
)
end
end
1 change: 1 addition & 0 deletions app/models/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
# fk_rails_... (tailor_id => tailors.id)
#
class Production < ApplicationRecord
Expand Down
89 changes: 82 additions & 7 deletions app/views/productions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,97 @@
<%= form_with(model: production, local: true) do |form| %>
<%# app/views/productions/_form.html.erb %>

<!-- Fields for production -->
<%= form_with(model: [@account, production], local: true) do |form| %>
<% if production.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(production.errors.count, "error") %> prohibited this production from being saved:</h2>
<ul>
<% production.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group">
<%= form.label :number %>
<%= form.number_field :number, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :tailor_id, 'Select Tailor' %>
<%= form.collection_select :tailor_id, Tailor.all, :id, :name, include_blank: true %>
<%= form.collection_select :tailor_id, Tailor.all, :id, :name, { include_blank: true }, { class: 'form-control' } %>
</div>

<div class="form-group">
<%= form.label :cut_date %>
<%= form.date_field :cut_date, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :delivery_date %>
<%= form.date_field :delivery_date, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :pieces_delivered %>
<%= form.number_field :pieces_delivered, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :pieces_missing %>
<%= form.number_field :pieces_missing, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :expected_delivery_date %>
<%= form.date_field :expected_delivery_date, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :confirmed %>
<%= form.check_box :confirmed, class: 'form-check-input' %>
</div>

<div class="form-group">
<%= form.label :paid %>
<%= form.check_box :paid, class: 'form-check-input' %>
</div>

<div class="form-group">
<%= form.label :consider %>
<%= form.check_box :consider, class: 'form-check-input' %>
</div>

<div class="form-group">
<%= form.label :observation %>
<%= form.text_area :observation, class: 'form-control' %>
</div>

<h3>Products</h3>
<div id="products">
<%= form.fields_for :products do |product_form| %>
<%= render 'product_fields', f: product_form %>
<%= form.fields_for :production_products do |production_product_form| %>
<%= render 'production_product_fields', f: production_product_form %>
<% end %>

<div class="links">
<%= link_to_add_association 'Add Product', form, :products %>
<%= link_to_add_association 'Add Product', form, :production_products, partial: 'production_product_fields' %>
</div>
</div>

<%= form.submit %>
<%= form.submit class: 'btn btn-primary' %>
<% end %>

<%# app/views/productions/_production_product_fields.html.erb %>
<div class="nested-fields">
<div class="form-group">
<%= f.label :product_id, 'Select Product' %>
<%= f.collection_select :product_id, Product.all, :id, :name, { include_blank: true }, { class: 'form-control' } %>
</div>

<div class="form-group">
<%= f.label :quantity %>
<%= f.number_field :quantity, class: 'form-control' %>
</div>

<%= link_to_remove_association "Remove Product", f %>
</div>
31 changes: 12 additions & 19 deletions app/views/productions/_production_product_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
<div class="nested-fields">
<div class="col-md-12">
<div class="row mb-3">
<!-- Product Select Dropdown -->
<div class="col-md-6">
<%= f.label :product_id, t('activerecord.attributes.production_product.product_id') %>
<%= f.collection_select :product_id, Product.all, :id, :name, { include_blank: true }, { class: 'form-control' } %>
</div>

<!-- Quantity Input -->
<div class="col-md-2">
<%= f.label :quantity, t('activerecord.attributes.production_product.quantity') %>
<%= f.number_field :quantity, class: 'form-control' %>
</div>

<!-- Remove Button -->
<div class="col-md-2 d-flex align-items-end">
<%= link_to_remove_association t('actions.remove_product'), f, class: 'btn btn-danger' %>
</div>
<div class="row">
<div class="col-md-6">
<%= f.label :product_id, ProductionProduct.human_attribute_name(:product), class: 'form-label' %>
<%= f.collection_select :product_id, Product.all, :id, :name, { include_blank: true }, { class: 'form-control', required: true } %>
</div>
<div class="col-md-4">
<%= f.label :quantity, ProductionProduct.human_attribute_name(:quantity), class: 'form-label' %>
<%= f.number_field :quantity, class: 'form-control', min: 1, required: true %>
</div>
<div class="col-md-2 d-flex align-items-end">
<%= link_to_remove_association t('actions.remove'), f, class: 'btn btn-danger' %>
</div>
</div>
</div>
</div>
42 changes: 30 additions & 12 deletions app/views/productions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
<br><br>
<%= form_with(model: @production, local: true, html: { class: 'needs-validation', novalidate: '' }) do |form| %>
<h1><%= t('productions.new.title') %></h1>

<%= form_with(model: [@account, @production], local: true, html: { class: 'needs-validation', novalidate: '' }) do |form| %>
<% if @production.errors.any? %>
<div class="alert alert-danger">
<h2><%= pluralize(@production.errors.count, "error") %> prohibited this production from being saved:</h2>
<ul>
<% @production.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<!-- Row 1: Tailor Selection -->
<div class="row mb-3">
<div class="col-3">
<%= form.label :tailor_id, Production.human_attribute_name(:tailor), class: 'form-label' %>
<%= form.collection_select :tailor_id, @tailors, :id, :name, { include_blank: true }, { class: 'form-control' } %>
<%= form.collection_select :tailor_id, @tailors, :id, :name, { include_blank: true }, { class: 'form-control', required: true } %>
</div>
</div>

<!-- Row 2: Dates -->
<div class="row mb-3">
<div class="col-md-6">
<%= form.label :cut_date, Production.human_attribute_name(:cut_date), class: 'form-label' %>
<%= form.date_field :cut_date, class: 'form-control' %>
<%= form.date_field :cut_date, class: 'form-control', required: true %>
</div>
<div class="col-md-6">
<%= form.label :deliver_date, Production.human_attribute_name(:deliver_date), class: 'form-label' %>
<%= form.date_field :deliver_date, class: 'form-control' %>
<%= form.label :delivery_date, Production.human_attribute_name(:delivery_date), class: 'form-label' %>
<%= form.date_field :delivery_date, class: 'form-control' %>
</div>
</div>

<!-- Row 3: Production Products -->
<div class="mb-3">
<%= form.fields_for :production_products do |pp_form| %>
<%= render 'production_product_fields', f: pp_form %>
<% end %>
<%= link_to_add_association t('actions.add_product'), form, :production_products, class: 'btn btn-primary' %>
<h3><%= t('productions.products') %></h3>
<div id="production-products">
<%= form.fields_for :production_products do |pp_form| %>
<%= render 'production_product_fields', f: pp_form %>
<% end %>
</div>
<%= link_to_add_association t('actions.add_product'), form, :production_products,
class: 'btn btn-primary',
data: {
association_insertion_node: '#production-products',
association_insertion_method: 'append'
} %>
</div>

<div class="row mb-3 form-check">
Expand All @@ -40,5 +59,4 @@
<%= form.submit t('helpers.submit.create'), class: 'btn btn-success' %>
</div>
</div>

<% end %>
<% end %>
30 changes: 30 additions & 0 deletions db/migrate/20240828195207_add_missing_fields_to_productions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class AddMissingFieldsToProductions < ActiveRecord::Migration[7.0]
def change
change_table :productions, bulk: true do |t|
t.column :number, :integer, if_not_exists: true
t.column :delivery_date, :date, if_not_exists: true
t.column :pieces_delivered, :integer, if_not_exists: true
t.column :pieces_missing, :integer, if_not_exists: true
t.column :expected_delivery_date, :date, if_not_exists: true
t.column :confirmed, :boolean, if_not_exists: true
t.column :paid, :boolean, if_not_exists: true
t.column :observation, :text, if_not_exists: true

# Add indexes
t.index :number, if_not_exists: true
t.index :cut_date, if_not_exists: true
t.index :delivery_date, if_not_exists: true
t.index :expected_delivery_date, if_not_exists: true
end

# Add foreign key for account if it doesn't exist
unless foreign_key_exists?(:productions, :accounts)
add_foreign_key :productions, :accounts
end

# Add foreign key for tailor if it doesn't exist
unless foreign_key_exists?(:productions, :tailors)
add_foreign_key :productions, :tailors
end
end
end

0 comments on commit 869205e

Please sign in to comment.