-
-
Notifications
You must be signed in to change notification settings - Fork 568
#5358: Refactor donations controller to use a view object #5388
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
Merged
+161
−94
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
module View | ||
Donations = Data.define( | ||
:donations, | ||
:filters, | ||
:item_categories, | ||
:paginated_donations, | ||
:product_drives, | ||
:product_drive_participants, | ||
:storage_locations, | ||
:donation_sites, | ||
:manufacturers | ||
) do | ||
include DateRangeHelper | ||
|
||
class << self | ||
def filter_params(params) | ||
if params.key?(:filters) | ||
params.require(:filters).permit( | ||
:at_storage_location, :by_source, :from_donation_site, | ||
:by_product_drive, :by_product_drive_participant, | ||
:from_manufacturer, :by_category | ||
) | ||
else | ||
{} | ||
end | ||
end | ||
|
||
def from_params(params:, organization:, helpers:) | ||
filters = filter_params(params) | ||
donations = organization.donations | ||
.includes(:storage_location, | ||
:donation_site, | ||
:product_drive, | ||
:product_drive_participant, | ||
:manufacturer, | ||
line_items: [:item]) | ||
.order(created_at: :desc) | ||
.class_filter(filters) | ||
.during(helpers.selected_range) | ||
|
||
paginated_donations = donations.page(params[:page]) | ||
|
||
storage_locations = donations.filter_map do |donation| | ||
donation.storage_location unless donation.storage_location.discarded_at | ||
end.compact.uniq.sort | ||
|
||
manufacturers = donations.collect(&:manufacturer).compact.uniq.sort | ||
|
||
new( | ||
donations: donations, | ||
filters: filters, | ||
item_categories: organization.item_categories.pluck(:name).uniq, | ||
paginated_donations: paginated_donations, | ||
product_drives: organization.product_drives.alphabetized, | ||
product_drive_participants: organization.product_drive_participants.alphabetized, | ||
storage_locations: storage_locations, | ||
donation_sites: donations.map(&:donation_site).compact.uniq.sort_by { |site| site.name.downcase }, | ||
manufacturers: manufacturers | ||
) | ||
end | ||
end | ||
|
||
def selected_storage_location | ||
filters[:at_storage_location] | ||
end | ||
|
||
def selected_source | ||
filters[:by_source] | ||
end | ||
|
||
def selected_item_category | ||
filters[:by_category] | ||
end | ||
|
||
def sources | ||
donations.map(&:source).uniq.sort | ||
end | ||
|
||
def donations_quantity | ||
donations.map(&:total_quantity).sum | ||
end | ||
|
||
def selected_donation_site | ||
filters[:from_donation_site] | ||
end | ||
|
||
def selected_product_drive | ||
filters[:by_product_drive] | ||
end | ||
|
||
def selected_product_drive_participant | ||
filters[:by_product_drive_participant] | ||
end | ||
|
||
def selected_manufacturer | ||
filters[:from_manufacturer] | ||
end | ||
|
||
def paginated_donations_quantity | ||
paginated_donations.map(&:total_quantity).sum | ||
end | ||
|
||
def paginated_in_kind_value | ||
paginated_donations.sum { |donation| donation.value_per_itemizable } | ||
end | ||
|
||
def total_money_raised | ||
donations.sum { |d| d.money_raised.to_i } | ||
end | ||
|
||
def total_value_all_donations | ||
donations.sum { |donation| donation.value_per_itemizable } | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like several of these helpers should also be removed from this controller now that they live in the view