From 902639d9dc9038095d6ba0194d5f1a0d9aa5d114 Mon Sep 17 00:00:00 2001 From: Svetlana Vileshina Date: Fri, 26 Jul 2019 16:11:49 -0400 Subject: [PATCH] box model crud Co-authored-by: Robb Kidd Co-authored-by: Mae Beale --- app/assets/javascripts/boxes.js | 2 + app/assets/stylesheets/boxes.css | 4 ++ app/controllers/boxes_controller.rb | 74 +++++++++++++++++++++++ app/helpers/boxes_helper.rb | 2 + app/models/box.rb | 8 +++ app/views/boxes/_box.json.jbuilder | 2 + app/views/boxes/_form.html.erb | 57 +++++++++++++++++ app/views/boxes/edit.html.erb | 6 ++ app/views/boxes/index.html.erb | 41 +++++++++++++ app/views/boxes/index.json.jbuilder | 1 + app/views/boxes/new.html.erb | 5 ++ app/views/boxes/show.html.erb | 44 ++++++++++++++ app/views/boxes/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20190726200525_create_boxes.rb | 16 +++++ test/controllers/boxes_controller_test.rb | 48 +++++++++++++++ test/fixtures/boxes.yml | 21 +++++++ test/models/box_test.rb | 7 +++ test/system/boxes_test.rb | 57 +++++++++++++++++ 19 files changed, 397 insertions(+) create mode 100644 app/assets/javascripts/boxes.js create mode 100644 app/assets/stylesheets/boxes.css create mode 100644 app/controllers/boxes_controller.rb create mode 100644 app/helpers/boxes_helper.rb create mode 100644 app/models/box.rb create mode 100644 app/views/boxes/_box.json.jbuilder create mode 100644 app/views/boxes/_form.html.erb create mode 100644 app/views/boxes/edit.html.erb create mode 100644 app/views/boxes/index.html.erb create mode 100644 app/views/boxes/index.json.jbuilder create mode 100644 app/views/boxes/new.html.erb create mode 100644 app/views/boxes/show.html.erb create mode 100644 app/views/boxes/show.json.jbuilder create mode 100644 db/migrate/20190726200525_create_boxes.rb create mode 100644 test/controllers/boxes_controller_test.rb create mode 100644 test/fixtures/boxes.yml create mode 100644 test/models/box_test.rb create mode 100644 test/system/boxes_test.rb diff --git a/app/assets/javascripts/boxes.js b/app/assets/javascripts/boxes.js new file mode 100644 index 00000000..dee720fa --- /dev/null +++ b/app/assets/javascripts/boxes.js @@ -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. diff --git a/app/assets/stylesheets/boxes.css b/app/assets/stylesheets/boxes.css new file mode 100644 index 00000000..afad32db --- /dev/null +++ b/app/assets/stylesheets/boxes.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/boxes_controller.rb b/app/controllers/boxes_controller.rb new file mode 100644 index 00000000..ada2b541 --- /dev/null +++ b/app/controllers/boxes_controller.rb @@ -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 diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb new file mode 100644 index 00000000..17fa1bf2 --- /dev/null +++ b/app/helpers/boxes_helper.rb @@ -0,0 +1,2 @@ +module BoxesHelper +end diff --git a/app/models/box.rb b/app/models/box.rb new file mode 100644 index 00000000..938bc0ef --- /dev/null +++ b/app/models/box.rb @@ -0,0 +1,8 @@ +class Box < ApplicationRecord + belongs_to :box_request + belongs_to :designed_by + belongs_to :design_reviewed_by + belongs_to :assembled_by + belongs_to :shipped_by + belongs_to :shipping_payment +end diff --git a/app/views/boxes/_box.json.jbuilder b/app/views/boxes/_box.json.jbuilder new file mode 100644 index 00000000..b1ed9df4 --- /dev/null +++ b/app/views/boxes/_box.json.jbuilder @@ -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) diff --git a/app/views/boxes/_form.html.erb b/app/views/boxes/_form.html.erb new file mode 100644 index 00000000..17757707 --- /dev/null +++ b/app/views/boxes/_form.html.erb @@ -0,0 +1,57 @@ +<%= form_with(model: box, local: true) do |form| %> + <% if box.errors.any? %> +
+

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

+ +
    + <% box.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :box_request_id %> + <%= form.text_field :box_request_id %> +
+ +
+ <%= form.label :designed_by_id %> + <%= form.text_field :designed_by_id %> +
+ +
+ <%= form.label :design_reviewed_by_id %> + <%= form.text_field :design_reviewed_by_id %> +
+ +
+ <%= form.label :assembled_by_id %> + <%= form.text_field :assembled_by_id %> +
+ +
+ <%= form.label :shipped_by_id %> + <%= form.text_field :shipped_by_id %> +
+ +
+ <%= form.label :shipping_payment_id %> + <%= form.text_field :shipping_payment_id %> +
+ +
+ <%= form.label :shipped_at %> + <%= form.datetime_select :shipped_at %> +
+ +
+ <%= form.label :shipment_tracking_number %> + <%= form.text_field :shipment_tracking_number %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/boxes/edit.html.erb b/app/views/boxes/edit.html.erb new file mode 100644 index 00000000..d7e85ab3 --- /dev/null +++ b/app/views/boxes/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Box

+ +<%= render 'form', box: @box %> + +<%= link_to 'Show', @box %> | +<%= link_to 'Back', boxes_path %> diff --git a/app/views/boxes/index.html.erb b/app/views/boxes/index.html.erb new file mode 100644 index 00000000..3813b008 --- /dev/null +++ b/app/views/boxes/index.html.erb @@ -0,0 +1,41 @@ +

<%= notice %>

+ +

Boxes

+ + + + + + + + + + + + + + + + + + <% @boxes.each do |box| %> + + + + + + + + + + + + + + <% end %> + +
Box requestDesigned byDesign reviewed byAssembled byShipped byShipping paymentShipped atShipment tracking number
<%= box.box_request %><%= box.designed_by %><%= box.design_reviewed_by %><%= box.assembled_by %><%= box.shipped_by %><%= box.shipping_payment %><%= box.shipped_at %><%= box.shipment_tracking_number %><%= link_to 'Show', box %><%= link_to 'Edit', edit_box_path(box) %><%= link_to 'Destroy', box, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Box', new_box_path %> diff --git a/app/views/boxes/index.json.jbuilder b/app/views/boxes/index.json.jbuilder new file mode 100644 index 00000000..7b1d4c21 --- /dev/null +++ b/app/views/boxes/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @boxes, partial: "boxes/box", as: :box diff --git a/app/views/boxes/new.html.erb b/app/views/boxes/new.html.erb new file mode 100644 index 00000000..0aeb5e7d --- /dev/null +++ b/app/views/boxes/new.html.erb @@ -0,0 +1,5 @@ +

New Box

+ +<%= render 'form', box: @box %> + +<%= link_to 'Back', boxes_path %> diff --git a/app/views/boxes/show.html.erb b/app/views/boxes/show.html.erb new file mode 100644 index 00000000..ae7d5120 --- /dev/null +++ b/app/views/boxes/show.html.erb @@ -0,0 +1,44 @@ +

<%= notice %>

+ +

+ Box request: + <%= @box.box_request %> +

+ +

+ Designed by: + <%= @box.designed_by %> +

+ +

+ Design reviewed by: + <%= @box.design_reviewed_by %> +

+ +

+ Assembled by: + <%= @box.assembled_by %> +

+ +

+ Shipped by: + <%= @box.shipped_by %> +

+ +

+ Shipping payment: + <%= @box.shipping_payment %> +

+ +

+ Shipped at: + <%= @box.shipped_at %> +

+ +

+ Shipment tracking number: + <%= @box.shipment_tracking_number %> +

+ +<%= link_to 'Edit', edit_box_path(@box) %> | +<%= link_to 'Back', boxes_path %> diff --git a/app/views/boxes/show.json.jbuilder b/app/views/boxes/show.json.jbuilder new file mode 100644 index 00000000..70fea382 --- /dev/null +++ b/app/views/boxes/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "boxes/box", box: @box diff --git a/config/routes.rb b/config/routes.rb index e109e994..5e34181c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :boxes resources :volunteers resources :box_requests resources :requesters diff --git a/db/migrate/20190726200525_create_boxes.rb b/db/migrate/20190726200525_create_boxes.rb new file mode 100644 index 00000000..b2b44134 --- /dev/null +++ b/db/migrate/20190726200525_create_boxes.rb @@ -0,0 +1,16 @@ +class CreateBoxes < ActiveRecord::Migration[5.2] + def change + create_table :boxes do |t| + t.references :box_request, foreign_key: true, null: false, index: true + t.references :designed_by, foreign_table_name: :user, null: true, index: true + t.references :design_reviewed_by, foreign_table_name: :user, null: true, index: true + t.references :assembled_by, foreign_table_name: :user, null: true, index: true + t.references :shipped_by, foreign_table_name: :user, null: true, index: true + t.references :shipping_payment, foreign_table_name: :purchase, null: true, index: true + t.datetime :shipped_at + t.string :shipment_tracking_number + + t.timestamps + end + end +end diff --git a/test/controllers/boxes_controller_test.rb b/test/controllers/boxes_controller_test.rb new file mode 100644 index 00000000..f2550797 --- /dev/null +++ b/test/controllers/boxes_controller_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class BoxesControllerTest < ActionDispatch::IntegrationTest + setup do + @box = boxes(:one) + end + + test "should get index" do + get boxes_url + assert_response :success + end + + test "should get new" do + get new_box_url + assert_response :success + end + + test "should create box" do + assert_difference('Box.count') do + post boxes_url, params: { box: { assembled_by_id: @box.assembled_by_id, box_request_id: @box.box_request_id, design_reviewed_by_id: @box.design_reviewed_by_id, designed_by_id: @box.designed_by_id, shipment_tracking_number: @box.shipment_tracking_number, shipped_at: @box.shipped_at, shipped_by_id: @box.shipped_by_id, shipping_payment_id: @box.shipping_payment_id } } + end + + assert_redirected_to box_url(Box.last) + end + + test "should show box" do + get box_url(@box) + assert_response :success + end + + test "should get edit" do + get edit_box_url(@box) + assert_response :success + end + + test "should update box" do + patch box_url(@box), params: { box: { assembled_by_id: @box.assembled_by_id, box_request_id: @box.box_request_id, design_reviewed_by_id: @box.design_reviewed_by_id, designed_by_id: @box.designed_by_id, shipment_tracking_number: @box.shipment_tracking_number, shipped_at: @box.shipped_at, shipped_by_id: @box.shipped_by_id, shipping_payment_id: @box.shipping_payment_id } } + assert_redirected_to box_url(@box) + end + + test "should destroy box" do + assert_difference('Box.count', -1) do + delete box_url(@box) + end + + assert_redirected_to boxes_url + end +end diff --git a/test/fixtures/boxes.yml b/test/fixtures/boxes.yml new file mode 100644 index 00000000..38a5e93e --- /dev/null +++ b/test/fixtures/boxes.yml @@ -0,0 +1,21 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + box_request: one + designed_by: one + design_reviewed_by: one + assembled_by: one + shipped_by: one + shipping_payment: one + shipped_at: 2019-07-26 16:05:25 + shipment_tracking_number: MyString + +two: + box_request: two + designed_by: two + design_reviewed_by: two + assembled_by: two + shipped_by: two + shipping_payment: two + shipped_at: 2019-07-26 16:05:25 + shipment_tracking_number: MyString diff --git a/test/models/box_test.rb b/test/models/box_test.rb new file mode 100644 index 00000000..42107f56 --- /dev/null +++ b/test/models/box_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BoxTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/boxes_test.rb b/test/system/boxes_test.rb new file mode 100644 index 00000000..973378b3 --- /dev/null +++ b/test/system/boxes_test.rb @@ -0,0 +1,57 @@ +require "application_system_test_case" + +class BoxesTest < ApplicationSystemTestCase + setup do + @box = boxes(:one) + end + + test "visiting the index" do + visit boxes_url + assert_selector "h1", text: "Boxes" + end + + test "creating a Box" do + visit boxes_url + click_on "New Box" + + fill_in "Assembled by", with: @box.assembled_by_id + fill_in "Box request", with: @box.box_request_id + fill_in "Design reviewed by", with: @box.design_reviewed_by_id + fill_in "Designed by", with: @box.designed_by_id + fill_in "Shipment tracking number", with: @box.shipment_tracking_number + fill_in "Shipped at", with: @box.shipped_at + fill_in "Shipped by", with: @box.shipped_by_id + fill_in "Shipping payment", with: @box.shipping_payment_id + click_on "Create Box" + + assert_text "Box was successfully created" + click_on "Back" + end + + test "updating a Box" do + visit boxes_url + click_on "Edit", match: :first + + fill_in "Assembled by", with: @box.assembled_by_id + fill_in "Box request", with: @box.box_request_id + fill_in "Design reviewed by", with: @box.design_reviewed_by_id + fill_in "Designed by", with: @box.designed_by_id + fill_in "Shipment tracking number", with: @box.shipment_tracking_number + fill_in "Shipped at", with: @box.shipped_at + fill_in "Shipped by", with: @box.shipped_by_id + fill_in "Shipping payment", with: @box.shipping_payment_id + click_on "Update Box" + + assert_text "Box was successfully updated" + click_on "Back" + end + + test "destroying a Box" do + visit boxes_url + page.accept_confirm do + click_on "Destroy", match: :first + end + + assert_text "Box was successfully destroyed" + end +end