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

Ms test pull request #17

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-linux)
Expand Down Expand Up @@ -347,6 +349,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.6.8-arm64-darwin)
sqlite3 (1.6.8-x86_64-darwin)
sqlite3 (1.6.8-x86_64-linux)
stimulus-rails (1.2.2)
Expand Down Expand Up @@ -404,6 +407,7 @@ GEM
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-21
x86_64-darwin-22
x86_64-linux

Expand Down
Binary file added app/.DS_Store
Binary file not shown.
81 changes: 81 additions & 0 deletions app/controllers/deliveries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class DeliveriesController < ApplicationController
before_action :authenticate_user!

def index
matching_deliveries = Delivery.where(:user_id => current_user.id)
@list_of_deliveries = matching_deliveries.order({ :created_at => :desc })
@not_arrived = @list_of_deliveries.where(:actual_arrive => false)
@arrived = @list_of_deliveries.where(:actual_arrive => true)


render({ :template => "deliveries/index" })
end

def show
the_id = params.fetch("path_id")

matching_deliveries = Delivery.where({ :id => the_id })

@the_delivery = matching_deliveries.at(0)

render({ :template => "deliveries/show" })
end

def create
the_delivery = Delivery.new
the_delivery.description = params.fetch("query_description")
the_delivery.supposed_to_arrive = params.fetch("query_supposed_to_arrive")
the_delivery.actual_arrive = false
the_delivery.details = params.fetch("query_details")
# the_delivery.user_id = params.fetch("query_user_id")
the_delivery.user_id = current_user.id

if the_delivery.valid?
the_delivery.save
redirect_to("/deliveries", { :notice => "Added to list." })
else
redirect_to("/deliveries", { :alert => the_delivery.errors.full_messages.to_sentence })
end
end

def mark_as_received
the_id = params.fetch("path_id")
the_delivery = Delivery.where({ :id => the_id }).at(0)

the_delivery.actual_arrive = true

if the_delivery.valid?
the_delivery.save
redirect_to("/deliveries", { :notice => "Delivery marked received successfully." })
else
redirect_to("/deliveries", { :alert => the_delivery.errors.full_messages.to_sentence })
end
end

def update
the_id = params.fetch("path_id")
the_delivery = Delivery.where({ :id => the_id }).at(0)

the_delivery.description = params.fetch("query_description")
the_delivery.supposed_to_arrive = params.fetch("query_supposed_to_arrive")
the_delivery.actual_arrive = params.fetch("query_actual_arrive")
the_delivery.details = params.fetch("query_details")
the_delivery.user_id = params.fetch("query_user_id")

if the_delivery.valid?
the_delivery.save
redirect_to("/deliveries/#{the_delivery.id}", { :notice => "Delivery updated successfully."} )
else
redirect_to("/deliveries/#{the_delivery.id}", { :alert => the_delivery.errors.full_messages.to_sentence })
end
end

def destroy
the_id = params.fetch("path_id")
the_delivery = Delivery.where({ :id => the_id }).at(0)

the_delivery.destroy

redirect_to("/deliveries", { :notice => "Delivery deleted successfully."} )
end
end
16 changes: 16 additions & 0 deletions app/models/delivery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# == Schema Information
#
# Table name: deliveries
#
# id :integer not null, primary key
# actual_arrive :boolean
# description :string
# details :text
# supposed_to_arrive :date
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
#
class Delivery < ApplicationRecord
belongs_to :user, class_name: "User", foreign_key: "user_id"
end
27 changes: 27 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
#
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many :deliveries, class_name: "Delivery", foreign_key: "user_id"

end
Binary file added app/views/.DS_Store
Binary file not shown.
85 changes: 85 additions & 0 deletions app/views/deliveries/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<div>
<div>
<h1>
Delivery Tracker
</h1>
<p>Know if something gets lost in the mail.</p>
</div>
</div>

<hr>

<div>
<div>
<h2>
Expecting a package?
</h2>

<form action="/insert_deliveries" method="post">
<div>
<label for="description_box">
Description
</label>

<input type="text" id="description_box" name="query_description">
</div>

<div>
<label for="supposed_to_arrive_box">
Supposed to arrive on
</label>

<input type="date" id="supposed_to_arrive_box" name="query_supposed_to_arrive">
</div>

<div>
<label for="details_box">
Details
</label>

<textarea id="details_box" name="query_details" rows="3"></textarea>
</div>

<button>
Log delivery
</button>
</form>
</div>
</div>

<hr>
<div>
<div style="background-color: lightgoldenrodyellow" class="waiting_on">
<h2>
Waiting on
</h2>
<ul>
<% @not_arrived.each do |delivery|%>
<form action="/mark_received/<%=delivery.id%>">
<li>
<div><%= delivery.description%></div>
<div>Supposed to arrive on <%= delivery.supposed_to_arrive%></div>
<div><%= delivery.details%></div>
<button>Mark as received</button>
</li>
</form>
<%end%>
</ul>
</div>

<div style="background-color: lightgreen" class="received">
<h2>
Received
</h2>
<ul>
<% @arrived.each do |delivery|%>
<li>
<div><%= delivery.description%></div>
<small><%= delivery.updated_at%> <a href="/delete_delivery/<%= delivery.id%>">Delete</a></small>
</li>
<%end%>
</ul>
</div>
</div>

<hr>
131 changes: 131 additions & 0 deletions app/views/deliveries/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<div>
<div>
<h1>
elivery #<%= @the_delivery.id %> details
</h1>

<div>
<div>
<a href="/deliveries">
Go back
</a>
</div>

<div>
<a href="/delete_delivery/<%= @the_delivery.id %>">
Delete delivery
</a>
</div>
</div>

<dl>
<dt>
Description
</dt>
<dd>
<%= @the_delivery.description %>
</dd>

<dt>
Supposed to arrive
</dt>
<dd>
<%= @the_delivery.supposed_to_arrive %>
</dd>

<dt>
Actual arrive
</dt>
<dd>
<%= @the_delivery.actual_arrive %>
</dd>

<dt>
Details
</dt>
<dd>
<%= @the_delivery.details %>
</dd>

<dt>
User
</dt>
<dd>
<%= @the_delivery.user_id %>
</dd>

<dt>
Created at
</dt>
<dd>
<%= time_ago_in_words(@the_delivery.created_at) %> ago
</dd>

<dt>
Updated at
</dt>
<dd>
<%= time_ago_in_words(@the_delivery.updated_at) %> ago
</dd>
</dl>
</div>
</div>

<hr>


<div>
<div>
<h2>
Edit delivery
</h2>

<form action="/modify_delivery/<%= @the_delivery.id %>" method="post" >
<div>
<label for="description_box">
Description
</label>

<input type="text" id="description_box" name="query_description" value="<%= @the_delivery.description %>">
</div>

<div>
<label for="supposed_to_arrive_box">
Supposed to arrive
</label>

<input type="date" id="supposed_to_arrive_box" name="query_supposed_to_arrive" value="<%= @the_delivery.supposed_to_arrive %>">
</div>

<div>
<label for="actual_arrive_box">
Actual arrive
</label>

<input type="date" id="actual_arrive_box" name="query_actual_arrive" value="<%= @the_delivery.actual_arrive %>">
</div>

<div>
<label for="details_box">
Details
</label>

<textarea id="details_box" name="query_details"><%= @the_delivery.details %></textarea>
</div>

<div>
<label for="user_id_box">
User
</label>

<input type="text" id="user_id_box" name="query_user_id" value="<%= @the_delivery.user_id %>">
</div>

<button>
Update delivery
</button>
</form>
</div>
</div>

<hr>
17 changes: 16 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>

<body>
<div>
<%if current_user == nil%>
<a href="/users/sign_up">Sign up</a> |
<a href="/users/sign_in">Sign in</a>
<%else%>
<a href="/">Delivery Tracker</a> |
<a href="/users/edit">Edit profile</a> |
<a href="/users/sign_out">Sign out</a>
<%end%>
</div>

<hr>

<%= notice%>
<%= alert%>
<%= yield %>
</body>
</html>
Loading