diff --git a/app/controllers/productions_controller.rb b/app/controllers/productions_controller.rb index 13875a82..e23fa3e8 100644 --- a/app/controllers/productions_controller.rb +++ b/app/controllers/productions_controller.rb @@ -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 @@ -12,9 +13,7 @@ def show def new @production = Production.new @production.production_products.build - end - - def edit + @tailors = Tailor.all end def create @@ -22,43 +21,39 @@ def create 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 \ No newline at end of file diff --git a/app/models/production.rb b/app/models/production.rb index 8f8f278b..2835d131 100644 --- a/app/models/production.rb +++ b/app/models/production.rb @@ -18,6 +18,7 @@ # # Foreign Keys # +# fk_rails_... (account_id => accounts.id) # fk_rails_... (tailor_id => tailors.id) # class Production < ApplicationRecord diff --git a/app/views/productions/_form.html.erb b/app/views/productions/_form.html.erb index ec3f86fa..3ac131a6 100644 --- a/app/views/productions/_form.html.erb +++ b/app/views/productions/_form.html.erb @@ -1,22 +1,97 @@ -<%= form_with(model: production, local: true) do |form| %> +<%# app/views/productions/_form.html.erb %> - +<%= form_with(model: [@account, production], local: true) do |form| %> + <% if production.errors.any? %> +
+

<%= pluralize(production.errors.count, "error") %> prohibited this production from being saved:

+ +
+ <% end %> + +
+ <%= form.label :number %> + <%= form.number_field :number, class: 'form-control' %> +
<%= 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' } %> +
+ +
+ <%= form.label :cut_date %> + <%= form.date_field :cut_date, class: 'form-control' %> +
+ +
+ <%= form.label :delivery_date %> + <%= form.date_field :delivery_date, class: 'form-control' %> +
+ +
+ <%= form.label :pieces_delivered %> + <%= form.number_field :pieces_delivered, class: 'form-control' %> +
+ +
+ <%= form.label :pieces_missing %> + <%= form.number_field :pieces_missing, class: 'form-control' %> +
+ +
+ <%= form.label :expected_delivery_date %> + <%= form.date_field :expected_delivery_date, class: 'form-control' %> +
+ +
+ <%= form.label :confirmed %> + <%= form.check_box :confirmed, class: 'form-check-input' %> +
+ +
+ <%= form.label :paid %> + <%= form.check_box :paid, class: 'form-check-input' %> +
+ +
+ <%= form.label :consider %> + <%= form.check_box :consider, class: 'form-check-input' %> +
+ +
+ <%= form.label :observation %> + <%= form.text_area :observation, class: 'form-control' %>

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 %>
- <%= form.submit %> + <%= form.submit class: 'btn btn-primary' %> <% end %> + +<%# app/views/productions/_production_product_fields.html.erb %> +
+
+ <%= f.label :product_id, 'Select Product' %> + <%= f.collection_select :product_id, Product.all, :id, :name, { include_blank: true }, { class: 'form-control' } %> +
+ +
+ <%= f.label :quantity %> + <%= f.number_field :quantity, class: 'form-control' %> +
+ + <%= link_to_remove_association "Remove Product", f %> +
\ No newline at end of file diff --git a/app/views/productions/_production_product_fields.html.erb b/app/views/productions/_production_product_fields.html.erb index 6e3ea350..d6b86b56 100644 --- a/app/views/productions/_production_product_fields.html.erb +++ b/app/views/productions/_production_product_fields.html.erb @@ -1,22 +1,15 @@
-
-
- -
- <%= 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' } %> -
- - -
- <%= f.label :quantity, t('activerecord.attributes.production_product.quantity') %> - <%= f.number_field :quantity, class: 'form-control' %> -
- - -
- <%= link_to_remove_association t('actions.remove_product'), f, class: 'btn btn-danger' %> -
+
+
+ <%= 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 } %> +
+
+ <%= f.label :quantity, ProductionProduct.human_attribute_name(:quantity), class: 'form-label' %> + <%= f.number_field :quantity, class: 'form-control', min: 1, required: true %> +
+
+ <%= link_to_remove_association t('actions.remove'), f, class: 'btn btn-danger' %>
-
+
\ No newline at end of file diff --git a/app/views/productions/new.html.erb b/app/views/productions/new.html.erb index 4f58538f..caafd5b4 100644 --- a/app/views/productions/new.html.erb +++ b/app/views/productions/new.html.erb @@ -1,11 +1,22 @@ -

-<%= form_with(model: @production, local: true, html: { class: 'needs-validation', novalidate: '' }) do |form| %> +

<%= t('productions.new.title') %>

+ +<%= form_with(model: [@account, @production], local: true, html: { class: 'needs-validation', novalidate: '' }) do |form| %> + <% if @production.errors.any? %> +
+

<%= pluralize(@production.errors.count, "error") %> prohibited this production from being saved:

+ +
+ <% end %>
<%= 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 } %>
@@ -13,20 +24,28 @@
<%= 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 %>
- <%= 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' %>
- <%= 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' %> +

<%= t('productions.products') %>

+
+ <%= 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', + data: { + association_insertion_node: '#production-products', + association_insertion_method: 'append' + } %>
@@ -40,5 +59,4 @@ <%= form.submit t('helpers.submit.create'), class: 'btn btn-success' %>
- -<% end %> +<% end %> \ No newline at end of file diff --git a/db/migrate/20240828195207_add_missing_fields_to_productions.rb b/db/migrate/20240828195207_add_missing_fields_to_productions.rb new file mode 100644 index 00000000..66ceae4a --- /dev/null +++ b/db/migrate/20240828195207_add_missing_fields_to_productions.rb @@ -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 \ No newline at end of file