diff --git a/app/controllers/product_drive_participants_controller.rb b/app/controllers/product_drive_participants_controller.rb index 9925d4a05d..211287394f 100644 --- a/app/controllers/product_drive_participants_controller.rb +++ b/app/controllers/product_drive_participants_controller.rb @@ -3,14 +3,12 @@ class ProductDriveParticipantsController < ApplicationController include Importable - # TODO: Should there be a :destroy action for this? - def index - @product_drive_participants = current_organization.product_drive_participants.includes(:donations).with_volumes.order(:business_name) + @products = View::ProductDriveParticipants.new(params: params, organization: current_organization) respond_to do |format| format.html - format.csv { send_data ProductDriveParticipant.generate_csv(@product_drive_participants), filename: "ProductDriveParticipants-#{Time.zone.today}.csv" } + format.csv { send_data ProductDriveParticipant.generate_csv(@products.participants), filename: "ProductDriveParticipants-#{Time.zone.today}.csv" } end end @@ -65,6 +63,8 @@ def product_drive_participant_params helper_method \ def filter_params - {} + return {} unless params.key?(:filters) + + params.require(:filters).permit(:by_business_name, :by_contact_name) end end diff --git a/app/models/product_drive_participant.rb b/app/models/product_drive_participant.rb index f4029f4c58..dd2d1c656e 100644 --- a/app/models/product_drive_participant.rb +++ b/app/models/product_drive_participant.rb @@ -18,8 +18,9 @@ class ProductDriveParticipant < ApplicationRecord has_paper_trail - include Provideable + include Filterable include Geocodable + include Provideable has_many :donations, inverse_of: :product_drive_participant, dependent: :destroy @@ -30,6 +31,8 @@ class ProductDriveParticipant < ApplicationRecord validates :comment, length: { maximum: 500 } scope :alphabetized, -> { order(:contact_name) } + scope :by_business_name, ->(business_name) { where("business_name ILIKE ?", "%#{business_name}%") } + scope :by_contact_name, ->(contact_name) { where("contact_name ILIKE ?", "%#{contact_name}%") } scope :with_volumes, -> { left_joins(donations: :line_items) .select("product_drive_participants.*, SUM(COALESCE(line_items.quantity, 0)) AS volume") diff --git a/app/models/view/product_drive_participants.rb b/app/models/view/product_drive_participants.rb new file mode 100644 index 0000000000..95287cf4ed --- /dev/null +++ b/app/models/view/product_drive_participants.rb @@ -0,0 +1,33 @@ +module View + class ProductDriveParticipants + attr_reader :filters, :params, :participants + + def initialize(params:, organization:) + @params = params + @filters = filter_params(params) + + @participants = organization + .product_drive_participants + .includes(:donations) + .with_volumes + .class_filter(filters) + .order(:business_name) + end + + def filter_params(params = {}) + if params.key?(:filters) + params.require(:filters).permit(:by_business_name, :by_contact_name) + else + {} + end + end + + def selected_business_name + filters[:by_business_name] + end + + def selected_contact_name + filters[:by_contact_name] + end + end +end diff --git a/app/views/product_drive_participants/index.html.erb b/app/views/product_drive_participants/index.html.erb index 8fe5864221..2e471df069 100644 --- a/app/views/product_drive_participants/index.html.erb +++ b/app/views/product_drive_participants/index.html.erb @@ -22,6 +22,36 @@ +