Skip to content

Commit

Permalink
Requests and Donations now support multiple rows (#101)
Browse files Browse the repository at this point in the history
* add javascript to dynamically load table
* Split item selection into own form
* Working in donation form; TODO: abstract out of here
* Factoring out item selection to a partial
* Refactoring object relationships
* Refactored item system in database
* Making todo more specific
* Adding and removing rows with updating dropdowns
* Validation appears functional
* Editing and showing work
* All displays updated
* Commenting JS
* Adding donations relationship to item_changes
* Fixing lock file
* Fixing webpack
* Cleaning package.json
* Authenticate Request#show (#112)
* Added time checking to Request#show
* Show the top 3 item requests in table (sorted by default by quantity)
  • Loading branch information
tumbleshack authored Mar 19, 2021
1 parent 63b7db0 commit ed83e1c
Show file tree
Hide file tree
Showing 35 changed files with 457 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,4 @@ RUBY VERSION
ruby 2.7.2p137

BUNDLED WITH
2.2.8
2.2.8
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def require_role(redirect_path, role)
def configure_permitted_parameters
#devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
devise_parameter_sanitizer.permit(:account_update, keys: [:email, :password, :current_password, :admin, :volunteer, :donor, :donee])
end
end

end
27 changes: 21 additions & 6 deletions app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,43 @@ class DonationsController < ApplicationController
# GET /donations or /donations.json
def index
@donations = Donation.all
@allCategories = Category.all
@allItems = Item.all
end

# GET /donations/1 or /donations/1.json
def show
@allCategories = Category.all
@allItems = Item.all
end

# GET /donations/new
def new
@donation = Donation.new
@allCategories = Category.all
@allItems = Item.all
1.times { @donation.item_changes.build }
end

# GET /donations/1/edit
def edit
@allCategories = Category.all
@allItems = Item.all
end

# GET /donations/my-donations
def my_donations
@allCategories = Category.all
@allItems = Item.all
@donations = current_user != nil ? Donation.where("email like ?", "%#{current_user.email}%") : nil
end

# POST /donations or /donations.json
def create
@donation = Donation.new(donation_params.except(:items_quantity, :items_category, :items_itemType, :items_sizes))
@donation.items = "#{donation_params["items_quantity"]}x #{donation_params["items_category"]} #{donation_params["items_itemType"]} size #{donation_params["items_sizes"]}"

@allCategories = Category.all
@allItems = Item.all
@donation = Donation.new(donation_params)

respond_to do |format|
if @donation.save

Expand All @@ -49,9 +61,10 @@ def create

# PATCH/PUT /donations/1 or /donations/1.json
def update
@allCategories = Category.all
@allItems = Item.all
respond_to do |format|
if @donation.update(donation_params.except(:items_quantity, :items_category, :items_itemType, :items_sizes))
@donation.items = "#{donation_params["items_quantity"]}x #{donation_params["items_category"]} #{donation_params["items_itemType"]} Size #{donation_params["items_sizes"]}"
if @donation.update(donation_params)
@donation.save

format.html { redirect_to @donation, notice: "Donation was successfully updated." }
Expand All @@ -65,6 +78,8 @@ def update

# DELETE /donations/1 or /donations/1.json
def destroy
@allCategories = Category.all
@allItems = Item.all
@donation.destroy
respond_to do |format|
format.html { redirect_to donations_url, notice: "Donation was successfully destroyed." }
Expand All @@ -80,6 +95,6 @@ def set_donation

# Only allow a list of trusted parameters through.
def donation_params
params.require(:donation).permit(:full_name, :email, :phone, :county, :meet, :address, :availability, :items, :items_quantity, :items_category, :items_itemType, :items_sizes, :comments)
params.require(:donation).permit(:full_name, :email, :phone, :county, :meet, :address, :availability, :comments, item_changes_attributes: [:id, :category_id, :quantity, :itemType, :size, :change_type, :_destroy])
end
end
38 changes: 29 additions & 9 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,39 @@ class RequestsController < ApplicationController

before_action -> { require_role(:root, :admin) }, only: :index

before_action :check_timeout, only: :show

# GET /requests or /requests.json
def index
@requests = Request.all
@allCategories = Category.all
@allItems = Item.all
end

# GET /requests/1 or /requests/1.json
def show
@allCategories = Category.all
@allItems = Item.all
end

# GET /requests/new
def new
@request = Request.new
@allCategories = Category.all
@allItems = Item.all
1.times { @request.item_changes.build }
end

# GET /requests/1/edit
def edit
@allCategories = Category.all
@allItems = Item.all
end

# GET /requests/my-requests
def my_requests
@allCategories = Category.all
@allItems = Item.all
@requests = current_user != nil ? Request.where("email like ?", "%#{current_user.email}%") : nil
end

Expand All @@ -35,14 +48,14 @@ def popup

# POST /requests or /requests.json
def create

@request = Request.new(request_params.except(:items_quantity, :items_category, :items_itemType, :items_sizes))
@request.items = "#{request_params["items_quantity"]}x #{request_params["items_category"]} #{request_params["items_itemType"]} size #{request_params["items_sizes"]}"
@allCategories = Category.all
@allItems = Item.all
@request = Request.new(request_params)


respond_to do |format|
if @request.save

if @request.save


# ActionMailer should send email immediately after new request creation is saved
Expand All @@ -65,10 +78,11 @@ def create

# PATCH/PUT /requests/1 or /requests/1.json
def update
respond_to do |format|
@allCategories = Category.all
@allItems = Item.all

if @request.update(request_params.except(:items_quantity, :items_category, :items_itemType, :items_sizes))
@request.items = "#{request_params["items_quantity"]}x #{request_params["items_category"]} #{request_params["items_itemType"]} Size #{request_params["items_sizes"]}"
respond_to do |format|
if @request.update(request_params)
@request.save

format.html { redirect_to @request, notice: "Request was successfully updated." }
Expand All @@ -82,6 +96,8 @@ def update

# DELETE /requests/1 or /requests/1.json
def destroy
@allCategories = Category.all
@allItems = Item.all
@request.destroy
respond_to do |format|
format.html { redirect_to requests_url, notice: "Request was successfully destroyed." }
Expand All @@ -98,8 +114,12 @@ def set_request

# Only allow a list of trusted parameters through.
def request_params
params.require(:request).permit(:urgency, :full_name, :email, :phone, :relationship, :county, :meet, :address, :availability, :comments, item_changes_attributes: [:id, :category_id, :quantity, :itemType, :size, :change_type, :_destroy])
end

params.require(:request).permit(:urgency, :full_name, :email, :phone, :relationship, :county, :meet, :address, :availability, :items, :items_quantity, :items_category, :items_itemType, :items_sizes, :comments)

# Redirects to :root if accessed by non-volunteer after 30 minutes to
# protect information contained in request.
def check_timeout
require_role(:root, :volunteer) if @request.nil? || (Time.current.utc - @request.created_at.utc) / 1.minute > 30
end
end
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
module ApplicationHelper

# Given a list of attributes (key, value), returns true if any are blank
end
2 changes: 1 addition & 1 deletion app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ import "channels"

Rails.start()
Turbolinks.start()
ActiveStorage.start()
ActiveStorage.start()
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Category < ApplicationRecord
has_many :items
end
16 changes: 4 additions & 12 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
class Donation < ApplicationRecord
COUNTIES = {
'': 0,
'Fulton': 1,
'Dekalb': 2,
'Gwinnett': 3,
'Cobb': 4,
'Clayton': 5,
'Henry': 6,
'Muscogee': 7,
'Other (provide your address below)': 8
}.freeze

has_many :item_changes
accepts_nested_attributes_for :item_changes, allow_destroy: true

validates_presence_of :full_name, :email, :availability,
:county, :meet, :phone, :items
:county, :meet, :phone, :item_changes

validates_numericality_of :county, greater_than: 0, :message => "can't be blank"

Expand Down
3 changes: 2 additions & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Item < ApplicationRecord
has_one :category

validates_presence_of :quantity, :itemType, :size

belongs_to :category

validates_numericality_of :quantity, greater_than: -1, :message => "can't be negative"

def category
Expand Down
21 changes: 21 additions & 0 deletions app/models/item_change.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ItemChange < ApplicationRecord


CHANGE_TYPES = {
'request': 1,
'donation': 2,
}.freeze

belongs_to :category
belongs_to :request, optional: true
belongs_to :donation, optional: true

validates :change_type, inclusion: { in: CHANGE_TYPES.values }

validates_presence_of :quantity, :itemType, :size

validates_numericality_of :quantity, greater_than: 0

default_scope { order(quantity: :desc) }

end
11 changes: 10 additions & 1 deletion app/models/request.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Request < ApplicationRecord

URGENCIES = {
'': 0,
'Within 24 hours': 1,
Expand Down Expand Up @@ -27,8 +28,15 @@ class Request < ApplicationRecord
'Other (provide your address below)': 8
}.freeze

has_many :item_changes
accepts_nested_attributes_for :item_changes, allow_destroy: true

def any_blank(att)
att.any? { |k, v| v.blank? }
end

validates_presence_of :relationship, :full_name, :urgency, :email, :availability,
:county, :meet, :phone, :items
:county, :meet, :phone, :item_changes

validates_numericality_of :urgency, :relationship, :county, greater_than: 0, :message => "can't be blank"

Expand All @@ -43,4 +51,5 @@ class Request < ApplicationRecord

validates :full_name,
length: { in: 2..80 }

end
23 changes: 1 addition & 22 deletions app/views/donations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,7 @@
<%= form.text_field :availability %>
</div>

<div class="field">
<%= form.label :items, 'What items would you like to donate?' %>
<table>
<thead>
<tr>
<th>Quantity</th>
<th>Category</th>
<th>Item Type</th>
<th>Size</th>
</tr>
</thead>

<tbody>
<tr>
<td><%= form.number_field :items_quantity %></td>
<td><%= form.select :items_category, Category.all.map { |category| category.name } %></td>
<td><%= form.select :items_sizes, Item.all.map{ |item| item.size } %></td>
<td><%= form.select :items_itemType, Item.all.map{ |item| item.itemType } %></td>
</tr>
</tbody>
</table>
</div>
<%= render partial: 'form_helpers/item_selection', locals: { form: form, parent_f: :donation } %>

<div class="field">
<%= form.label :comments %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/donations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td><%= donation.meet %></td>
<td><%= donation.address %></td>
<td><%= donation.availability %></td>
<td><%= donation.items %></td>
<td>ITEMS HERE</td>
<td><%= donation.comments %></td>
<td><%= link_to 'Show', donation %></td>
<td><%= link_to 'Edit', edit_donation_path(donation) %></td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/donations/my_donations.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td><%= donation.meet %></td>
<td><%= donation.address %></td>
<td><%= donation.availability %></td>
<td><%= donation.items %></td>
<td>ITEMS HERE</td>
<td><%= donation.comments %></td>
<td><%= link_to 'Show', donation %></td>
<td><%= link_to 'Edit', edit_donation_path(donation) %></td>
Expand Down
4 changes: 3 additions & 1 deletion app/views/donations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

<p>
<strong>Items:</strong>
<%= @donation.items %>
<%@donation.item_changes.each do |item|%>
<p><%= item.quantity %> <%= Category.find(item.category_id).name %> <%= item.itemType %>, size: <%= item.size %></p>
<%end%>
</p>

<p>
Expand Down
Loading

0 comments on commit ed83e1c

Please sign in to comment.