From e89cc7ffca9a66d0895120845ac6f2386719cdea Mon Sep 17 00:00:00 2001 From: Sean Marcia Date: Sat, 18 Aug 2018 15:51:40 -0400 Subject: [PATCH] Add an issued_at validation to distributions/donations/purchases. --- app/models/concerns/issued_at.rb | 7 ------- app/models/distribution.rb | 19 +------------------ app/models/donation.rb | 21 +-------------------- app/models/purchase.rb | 20 +------------------- db/seeds.rb | 8 ++++---- 5 files changed, 7 insertions(+), 68 deletions(-) diff --git a/app/models/concerns/issued_at.rb b/app/models/concerns/issued_at.rb index 06dc9ae6c9..061788118d 100644 --- a/app/models/concerns/issued_at.rb +++ b/app/models/concerns/issued_at.rb @@ -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 diff --git a/app/models/distribution.rb b/app/models/distribution.rb index 17e90779b0..8134c0f85f 100644 --- a/app/models/distribution.rb +++ b/app/models/distribution.rb @@ -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 @@ -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 diff --git a/app/models/donation.rb b/app/models/donation.rb index 30d28013f6..8861a8935a 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -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 @@ -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) @@ -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 diff --git a/app/models/purchase.rb b/app/models/purchase.rb index e3fc6eb932..c2b5d4d55e 100644 --- a/app/models/purchase.rb +++ b/app/models/purchase.rb @@ -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 } @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index dd16e4ac0f..42ab2ebf0e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 @@ -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