From 51700a7386ea83515f418325d503fe9fda93ea16 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 13 Sep 2014 14:16:50 -0500 Subject: [PATCH] better donation feature outfit validation In particular, outfit_id == 0 would cause outfit_id? to return false, so it wouldn't run the outfit presence validation, so /donations/features would try to load outfit #0 and fail. Also, flash[:alert] instead of flash[:error] when outfit_id is bad. --- app/controllers/donations_controller.rb | 2 +- app/models/donation_feature.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index ba5cdb36..e818065b 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -28,7 +28,7 @@ class DonationsController < ApplicationController @features.each(&:save!) end rescue ActiveRecord::RecordInvalid - flash[:error] = "Couldn't save donation details. Do those outfits exist?" + flash[:alert] = "Couldn't save donation details. Do those outfits exist?" redirect_to @donation else flash[:success] = 'Donation details saved! ' + diff --git a/app/models/donation_feature.rb b/app/models/donation_feature.rb index bd0c7bb0..0d650aa5 100644 --- a/app/models/donation_feature.rb +++ b/app/models/donation_feature.rb @@ -2,7 +2,7 @@ class DonationFeature < ActiveRecord::Base belongs_to :donation belongs_to :outfit - validates :outfit, presence: true, if: :outfit_id? + validates :outfit, presence: true, if: :outfit_id_present? delegate :donor_name, to: :donation @@ -13,4 +13,8 @@ class DonationFeature < ActiveRecord::Base def outfit_url=(outfit_url) self.outfit_id = outfit_url.split('/').last rescue nil end + + def outfit_id_present? + outfit_id.present? + end end