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

box model crud #72

Merged
merged 6 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions app/assets/javascripts/boxes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/purchases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/boxes.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
4 changes: 4 additions & 0 deletions app/assets/stylesheets/purchases.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
74 changes: 74 additions & 0 deletions app/controllers/boxes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class BoxesController < ApplicationController
before_action :set_box, only: [:show, :edit, :update, :destroy]

# GET /boxes
# GET /boxes.json
def index
@boxes = Box.all
end

# GET /boxes/1
# GET /boxes/1.json
def show
end

# GET /boxes/new
def new
@box = Box.new
end

# GET /boxes/1/edit
def edit
end

# POST /boxes
# POST /boxes.json
def create
@box = Box.new(box_params)

respond_to do |format|
if @box.save
format.html { redirect_to @box, notice: 'Box was successfully created.' }
format.json { render :show, status: :created, location: @box }
else
format.html { render :new }
format.json { render json: @box.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /boxes/1
# PATCH/PUT /boxes/1.json
def update
respond_to do |format|
if @box.update(box_params)
format.html { redirect_to @box, notice: 'Box was successfully updated.' }
format.json { render :show, status: :ok, location: @box }
else
format.html { render :edit }
format.json { render json: @box.errors, status: :unprocessable_entity }
end
end
end

# DELETE /boxes/1
# DELETE /boxes/1.json
def destroy
@box.destroy
respond_to do |format|
format.html { redirect_to boxes_url, notice: 'Box was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def box_params
params.require(:box).permit(:box_request_id, :designed_by_id, :design_reviewed_by_id, :assembled_by_id, :shipped_by_id, :shipping_payment_id, :shipped_at, :shipment_tracking_number)
end
end
74 changes: 74 additions & 0 deletions app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class PurchasesController < ApplicationController
before_action :set_purchase, only: [:show, :edit, :update, :destroy]

# GET /purchases
# GET /purchases.json
def index
@purchases = Purchase.all
end

# GET /purchases/1
# GET /purchases/1.json
def show
end

# GET /purchases/new
def new
@purchase = Purchase.new
end

# GET /purchases/1/edit
def edit
end

# POST /purchases
# POST /purchases.json
def create
@purchase = Purchase.new(purchase_params)

respond_to do |format|
if @purchase.save
format.html { redirect_to @purchase, notice: 'Purchase was successfully created.' }
format.json { render :show, status: :created, location: @purchase }
else
format.html { render :new }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
respond_to do |format|
if @purchase.update(purchase_params)
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
format.json { render :show, status: :ok, location: @purchase }
else
format.html { render :edit }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end

# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
@purchase.destroy
respond_to do |format|
format.html { redirect_to purchases_url, notice: 'Purchase was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def purchase_params
params.require(:purchase).permit(:location_id, :total_price, :purchased_by_id, :reimbursed_by_id, :reimbursement_check_number, :reimbursement_status)
end
end
2 changes: 2 additions & 0 deletions app/helpers/boxes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BoxesHelper
end
2 changes: 2 additions & 0 deletions app/helpers/purchases_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PurchasesHelper
end
8 changes: 8 additions & 0 deletions app/models/box.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Box < ApplicationRecord
belongs_to :box_request
belongs_to :designed_by, optional: true, class_name: "User", foreign_key: :designed_by_id, inverse_of: :boxes_as_designer
belongs_to :design_reviewed_by, optional: true, class_name: "User", foreign_key: :design_reviewed_by_id, inverse_of: :boxes_as_design_reviewer
belongs_to :assembled_by, optional: true, class_name: "User", foreign_key: :assembled_by_id, inverse_of: :boxes_as_assembler
belongs_to :shipped_by, optional: true, class_name: "User", foreign_key: :shipped_by_id, inverse_of: :boxes_as_shipper
belongs_to :shipping_payment, optional: true, class_name: "Purchase", foreign_key: :shipping_payment_id, inverse_of: :payment_for_shipment
end
5 changes: 5 additions & 0 deletions app/models/purchase.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Purchase < ApplicationRecord
belongs_to :location
gabezurita marked this conversation as resolved.
Show resolved Hide resolved
belongs_to :purchased_by
belongs_to :reimbursed_by
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many :boxes_as_designer, class_name: "Box", foreign_key: :designed_by_id, inverse_of: :designed_by, dependent: :nullify
has_many :boxes_as_design_reviewer, class_name: "Box", foreign_key: :design_reviewed_by_id, inverse_of: :designed_reviewed_by, dependent: :nullify
has_many :boxes_as_assembler, class_name: "Box", foreign_key: :assembled_by_id, inverse_of: :assembled_by, dependent: :nullify
has_many :boxes_as_shipper, class_name: "Box", foreign_key: :shipped_by_id, inverse_of: :shipped_by, dependent: :nullify
end
2 changes: 2 additions & 0 deletions app/views/boxes/_box.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! box, :id, :box_request_id, :designed_by_id, :design_reviewed_by_id, :assembled_by_id, :shipped_by_id, :shipping_payment_id, :shipped_at, :shipment_tracking_number, :created_at, :updated_at
json.url box_url(box, format: :json)
57 changes: 57 additions & 0 deletions app/views/boxes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%= form_with(model: box, local: true) do |form| %>
<% if box.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(box.errors.count, "error") %> prohibited this box from being saved:</h2>

<ul>
<% box.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :box_request_id %>
<%= form.text_field :box_request_id %>
</div>

<div class="field">
<%= form.label :designed_by_id %>
<%= form.text_field :designed_by_id %>
</div>

<div class="field">
<%= form.label :design_reviewed_by_id %>
<%= form.text_field :design_reviewed_by_id %>
</div>

<div class="field">
<%= form.label :assembled_by_id %>
<%= form.text_field :assembled_by_id %>
</div>

<div class="field">
<%= form.label :shipped_by_id %>
<%= form.text_field :shipped_by_id %>
</div>

<div class="field">
<%= form.label :shipping_payment_id %>
<%= form.text_field :shipping_payment_id %>
</div>

<div class="field">
<%= form.label :shipped_at %>
<%= form.datetime_select :shipped_at %>
</div>

<div class="field">
<%= form.label :shipment_tracking_number %>
<%= form.text_field :shipment_tracking_number %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/boxes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Box</h1>

<%= render 'form', box: @box %>

<%= link_to 'Show', @box %> |
<%= link_to 'Back', boxes_path %>
41 changes: 41 additions & 0 deletions app/views/boxes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<p id="notice"><%= notice %></p>

<h1>Boxes</h1>

<table>
<thead>
<tr>
<th>Box request</th>
<th>Designed by</th>
<th>Design reviewed by</th>
<th>Assembled by</th>
<th>Shipped by</th>
<th>Shipping payment</th>
<th>Shipped at</th>
<th>Shipment tracking number</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @boxes.each do |box| %>
<tr>
<td><%= box.box_request %></td>
<td><%= box.designed_by %></td>
<td><%= box.design_reviewed_by %></td>
<td><%= box.assembled_by %></td>
<td><%= box.shipped_by %></td>
<td><%= box.shipping_payment %></td>
<td><%= box.shipped_at %></td>
<td><%= box.shipment_tracking_number %></td>
<td><%= link_to 'Show', box %></td>
<td><%= link_to 'Edit', edit_box_path(box) %></td>
<td><%= link_to 'Destroy', box, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Box', new_box_path %>
1 change: 1 addition & 0 deletions app/views/boxes/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @boxes, partial: "boxes/box", as: :box
5 changes: 5 additions & 0 deletions app/views/boxes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Box</h1>

<%= render 'form', box: @box %>

<%= link_to 'Back', boxes_path %>
44 changes: 44 additions & 0 deletions app/views/boxes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Box request:</strong>
<%= @box.box_request %>
</p>

<p>
<strong>Designed by:</strong>
<%= @box.designed_by %>
</p>

<p>
<strong>Design reviewed by:</strong>
<%= @box.design_reviewed_by %>
</p>

<p>
<strong>Assembled by:</strong>
<%= @box.assembled_by %>
</p>

<p>
<strong>Shipped by:</strong>
<%= @box.shipped_by %>
</p>

<p>
<strong>Shipping payment:</strong>
<%= @box.shipping_payment %>
</p>

<p>
<strong>Shipped at:</strong>
<%= @box.shipped_at %>
</p>

<p>
<strong>Shipment tracking number:</strong>
<%= @box.shipment_tracking_number %>
</p>

<%= link_to 'Edit', edit_box_path(@box) %> |
<%= link_to 'Back', boxes_path %>
1 change: 1 addition & 0 deletions app/views/boxes/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "boxes/box", box: @box
Loading