Skip to content
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

Add an issued_at validation to distributions/donations/purchases. #464

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions app/models/concerns/issued_at.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ module IssuedAt
extend ActiveSupport::Concern

included do
before_create :initialize_issued_at
scope :by_issued_at, ->(issued_at) { where(issued_at: issued_at.beginning_of_month..issued_at.end_of_month) }
end

private

def initialize_issued_at
self.issued_at ||= created_at
end
end
19 changes: 1 addition & 18 deletions app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ class Distribution < ApplicationRecord
# Distributions contain many different items
include Itemizable

validates :storage_location, :partner, :organization, presence: true
validates :issued_at, :storage_location, :partner, :organization, presence: true
validate :line_item_items_exist_in_inventory

include IssuedAt

scope :recent, ->(count = 3) { order(issued_at: :desc).limit(count) }
scope :during, ->(range) { where(distributions: { issued_at: range }) }
scope :for_csv_export, ->(organization) {
where(organization: organization)
.includes(:partner, :storage_location, :line_items)
}

delegate :name, to: :partner, prefix: true

Expand All @@ -59,17 +55,4 @@ def copy_from_donation(donation_id, storage_location_id)
copy_line_items(donation_id) if donation_id
self.storage_location = StorageLocation.find(storage_location_id) if storage_location_id
end

def self.csv_export_headers
["Partner", "Date of Distribution", "Source Inventory", "Total items"]
end

def csv_export_attributes
[
partner.name,
issued_at.strftime("%F"),
storage_location.name,
line_items.total
]
end
end
21 changes: 1 addition & 20 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ class Donation < ApplicationRecord
scope :by_diaper_drive_participant, ->(diaper_drive_participant_id) {
where(diaper_drive_participant_id: diaper_drive_participant_id)
}
scope :for_csv_export, ->(organization) {
where(organization: organization)
.includes(:line_items, :storage_location, :donation_site)
.order(created_at: :desc)
}

before_create :combine_duplicates
before_destroy :remove_inventory
Expand All @@ -55,6 +50,7 @@ class Donation < ApplicationRecord

include IssuedAt

validates :issued_at, presence: true
scope :during, ->(range) { where(donations: { issued_at: range }) }
scope :by_source, ->(source) {
source = SOURCES[source] if source.is_a?(Symbol)
Expand Down Expand Up @@ -139,21 +135,6 @@ def remove_inventory
storage_location.remove!(self)
end

def self.csv_export_headers
["Source", "Date", "Donation Site", "Storage Location", "Quantity of Items", "Variety of Items"]
end

def csv_export_attributes
[
source_view,
issued_at.strftime("%F"),
donation_site.try(:name),
storage_location.name,
line_items.total,
line_items.size
]
end

private

def combine_duplicates
Expand Down
20 changes: 1 addition & 19 deletions app/models/purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ class Purchase < ApplicationRecord
scope :purchased_from, ->(purchased_from) { where(purchased_from: purchased_from) }
scope :during, ->(range) { where(purchases: { issued_at: range }) }
scope :recent, ->(count = 3) { order(issued_at: :desc).limit(count) }
scope :for_csv_export, ->(organization) {
where(organization: organization)
.includes(:line_items, :storage_location)
.order(created_at: :desc)
}

before_create :combine_duplicates
before_destroy :remove_inventory
validates :issued_at, presence: true

validates :amount_spent, numericality: { greater_than: 0 }

Expand Down Expand Up @@ -79,20 +75,6 @@ def update_quantity(quantity, item)
line_item.save
end

def self.csv_export_headers
["Purchases from", "Storage Location", "Quantity of Items", "Variety of Items", "Amount spent"]
end

def csv_export_attributes
[
purchased_from,
storage_location.name,
line_items.total,
line_items.size,
amount_spent,
]
end

private

def combine_duplicates
Expand Down
8 changes: 4 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ def random_record(klass)
# Depending on which source it uses, additional data may need to be provided.
donation = case source
when Donation::SOURCES[:diaper_drive]
Donation.create! source: source, diaper_drive_participant: random_record(DiaperDriveParticipant), storage_location: random_record(StorageLocation), organization: pdx_org
Donation.create! source: source, diaper_drive_participant: random_record(DiaperDriveParticipant), storage_location: random_record(StorageLocation), organization: pdx_org, issued_at: Time.now
when Donation::SOURCES[:donation_site]
Donation.create! source: source, donation_site: random_record(DonationSite), storage_location: random_record(StorageLocation), organization: pdx_org
Donation.create! source: source, donation_site: random_record(DonationSite), storage_location: random_record(StorageLocation), organization: pdx_org, issued_at: Time.now
else
Donation.create! source: source, storage_location: random_record(StorageLocation), organization: pdx_org
Donation.create! source: source, storage_location: random_record(StorageLocation), organization: pdx_org, issued_at: Time.now
end

rand(1..5).times.each do
Expand All @@ -226,7 +226,7 @@ def random_record(klass)
end

20.times.each do
distribution = Distribution.create! storage_location: random_record(StorageLocation), partner: random_record(Partner), organization: pdx_org
distribution = Distribution.create! storage_location: random_record(StorageLocation), partner: random_record(Partner), organization: pdx_org, issued_at: Time.now

rand(1..5).times.each do
LineItem.create! quantity: rand(1..500), item: random_record(Item), itemizable: distribution
Expand Down