-
-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathproduct_drives_controller.rb
126 lines (105 loc) · 3.83 KB
/
product_drives_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class ProductDrivesController < ApplicationController
include Importable
before_action :set_product_drive, only: [:show, :edit, :update, :destroy]
before_action :verify_role, only: :destroy
def index
setup_date_range_picker
@product_drives = current_organization
.product_drives
.includes(donations: {line_items: :item})
.class_filter(filter_params)
.within_date_range(@selected_date_range)
.order(start_date: :desc)
# to be used in the name filter to sort product drives in alpha order
@product_drives_alphabetical = @product_drives.sort_by { |pd| pd.name.downcase }
@item_categories = current_organization.item_categories
@selected_name_filter = filter_params[:by_name]
@selected_item_category = filter_params[:by_item_category_id]
respond_to do |format|
format.html
format.csv do
send_data Exports::ExportProductDrivesCSVService.new(
@product_drives,
current_organization,
helpers.selected_range
).generate_csv, filename: "Product-Drives-#{Time.zone.today}.csv"
end
end
end
# GET /product_drives/1
# GET /product_drives/1.json
def create
@product_drive = current_organization.product_drives.new(product_drive_params.merge(organization: current_organization))
respond_to do |format|
if @product_drive.save
format.html { redirect_to product_drives_path, notice: "New product drive added!" }
format.js
else
flash[:error] = "Something didn't work quite right -- try again?"
format.html { render action: :new }
format.js { render template: "product_drives/new_modal" }
end
end
end
def new
@product_drive = current_organization.product_drives.new
if request.xhr?
respond_to do |format|
format.js { render template: "product_drives/new_modal" }
end
end
end
def edit
@product_drive = current_organization.product_drives.find(params[:id])
end
def show
@selected_name_filter = filter_params[:by_name]
@selected_item_category = filter_params[:by_item_category_id]
@product_drive = current_organization.product_drives.includes(:donations).find(params[:id])
end
def update
@product_drive = current_organization.product_drives.find(params[:id])
if @product_drive.update(product_drive_params)
redirect_to product_drives_path, notice: "#{@product_drive.name} updated!"
else
flash[:error] = "Something didn't work quite right -- try again?"
render action: :edit
end
end
def destroy
product_drive = current_organization.product_drives.find(params[:id])
product_drive.destroy
if product_drive.errors.any?
flash[:error] = product_drive.errors.full_messages.join("\n")
redirect_back(fallback_location: product_drives_url)
return
end
respond_to do |format|
format.html { redirect_to product_drives_url, notice: 'Product drive was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def verify_role
return if current_user.has_role?(Role::ORG_ADMIN, current_organization)
flash[:error] = 'You are not allowed to perform this action.'
redirect_to product_drives_url
end
# Use callbacks to share common setup or constraints between actions.
def set_product_drive
@product_drive_info = ProductDrive.find(params[:id])
end
def product_drive_params
params.require(:product_drive)
.permit(:name, :start_date, :end_date, :virtual)
end
def date_range_filter
return '' unless params.key?(:filters)
params.require(:filters)[:date_range]
end
helper_method \
def filter_params
return {} unless params.key?(:filters)
params.require(:filters).permit(:by_name, :by_item_category_id)
end
end