-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonations_controller.rb
97 lines (83 loc) · 2.87 KB
/
donations_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class DonationsController < ApplicationController
before_action :set_donation, only: %i[ show edit update destroy ]
before_action :authenticate_user!, only: %i[ index, my_donations ]
before_action -> { require_role(:root, :admin) }, only: :index
# 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
@allCategories = Category.all
@allItems = Item.all
@donation = Donation.new(donation_params)
respond_to do |format|
if @donation.save
format.html { redirect_to @donation, notice: "Donation was successfully created." }
format.json { render :show, status: :created, location: @donation }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @donation.errors, status: :unprocessable_entity }
end
end
end
# 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)
@donation.save
format.html { redirect_to @donation, notice: "Donation was successfully updated." }
format.json { render :show, status: :ok, location: @donation }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @donation.errors, status: :unprocessable_entity }
end
end
end
# 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." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_donation
@donation = Donation.find(params[:id])
end
# Only allow a list of trusted parameters through.
def donation_params
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