Skip to content

Commit b562178

Browse files
Eventdate validation helpers and form errors
Add can_destroy? helper method, orm-level checks before the eventdate is destroyed, and a validator to event so trying to destroy an eventdate that has been billed for raises an error.
1 parent 84de300 commit b562178

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

app/models/event.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Event < ActiveRecord::Base
6969
validates_associated :organization, :emails, :event_roles, :eventdates
7070
validates_format_of :contactemail, :with => Event::EmailRegex, :multiline => true
7171
# validate :eventdate_valid?
72-
validate :textable_social_valid?
72+
validate :textable_social_valid?, :check_eventdates_to_be_destroyed
7373

7474
scope :current_year, -> { where("representative_date >= ? or last_representative_date > ?", Account.magic_date, Account.magic_date) }
7575

@@ -230,4 +230,17 @@ def textable_social_valid?
230230
errors.add(:textable_social, "Textable must be enabled if social textable is enabled")
231231
end
232232
end
233+
234+
def check_eventdates_to_be_destroyed
235+
# If we are trying to update an event, someone might try to delete an eventdate.
236+
# This is bad if someone has billed for it. There is before_destroy validation,
237+
# and a foreign key check, but because the eventdate is removed from the event,
238+
# it is not automatically validated on submission. This we must manually check
239+
# that we can delete events here.
240+
eventdates.each do |eventdate|
241+
if eventdate.marked_for_destruction? and !eventdate.can_destroy?
242+
errors.add(:eventdates, "Cannot delete an eventdate that has been billed for")
243+
end
244+
end
245+
end
233246
end

app/models/eventdate.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Eventdate < ApplicationRecord
1818
before_validation :prune_roles
1919
after_save :synchronize_representative_dates
2020

21+
before_destroy :check_can_destroy
22+
2123
Event_Span_Days = 2;
2224
Event_Span_Seconds = Event_Span_Days * 24 * 60 * 60;
2325

@@ -63,6 +65,17 @@ def valid_strike?
6365
((strikedate.to_i - enddate.to_i) < Event_Span_Seconds))
6466
end
6567

68+
def can_destroy?
69+
not self.timecard_entries.any?
70+
end
71+
72+
def check_can_destroy
73+
if not self.can_destroy?
74+
errors.add(:base, "Cannot delete an eventdate that has been billed for")
75+
throw(:abort)
76+
end
77+
end
78+
6679
def has_call?
6780
self.calltype == "literal" or self.calltype == "startdate"
6881
end

app/models/timecard_entry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TimecardEntry < ApplicationRecord
66
extend Enumerize
77
enumerize :eventpart, in: ["call", "show", "strike"], default: "show"
88

9-
validates_presence_of :eventdate, :member_id, :eventdate_id, :hours, :eventpart
9+
validates_presence_of :eventdate, :member_id, :hours, :eventpart
1010
validates_numericality_of :hours, :less_than_or_equal_to => 37.5, :greater_than => 0
1111
validates_associated :member
1212
validates_inclusion_of :timecard, :in => ->(t){Timecard.valid_timecards}, :message => 'is not a current timecard', :allow_nil => true

app/views/events/_eventdate_fields.html.erb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,14 @@
108108
<% if can? :tic, @event %>
109109
<dl>
110110
<dt>Delete?</dt>
111-
<% if @event.new_record? %>
112-
<dd><%= link_to_remove_fields image_tag("cross.png"), f, true %></dd>
111+
<% if f.object.can_destroy? %>
112+
<% if @event.new_record? %>
113+
<dd><%= link_to_remove_fields image_tag("cross.png"), f, true %></dd>
114+
<% else %>
115+
<dd><%= link_to_remove_fields image_tag("cross.png"), f %></dd>
116+
<% end %>
113117
<% else %>
114-
<dd><%= link_to_remove_fields image_tag("cross.png"), f %></dd>
118+
<dd>You can't delete this date, as someone has already billed for it.</dd>
115119
<% end %>
116120
</dl>
117121
<% end %>

0 commit comments

Comments
 (0)