-
Notifications
You must be signed in to change notification settings - Fork 32
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
Rename/refactor in models for clarity #533
base: master
Are you sure you want to change the base?
Changes from 1 commit
eff2b85
31679eb
373fdd0
ae5e9c3
99e8f7a
f1bc98a
5e37928
3244e71
0abc0ac
da8c3da
77bb967
9d10003
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ class Eventdate < ApplicationRecord | |
|
||
validates_presence_of :startdate, :enddate, :description, :locations, :calltype, :striketype | ||
validates_associated :locations, :equipment_profile | ||
validate :dates, :validate_call, :validate_strike | ||
validate :validate_chronologicity, :validate_call, :validate_strike | ||
|
||
before_validation :prune_roles | ||
after_save :synchronize_representative_dates | ||
|
@@ -37,7 +37,7 @@ class Eventdate < ApplicationRecord | |
|
||
ThinkingSphinx::Callbacks.append(self, :behaviours => [:sql, :deltas]) | ||
|
||
def dates | ||
def validate_chronologicity | ||
if startdate and enddate | ||
errors.add(:base, "We're not a time machine. (End Date can't be before Start Date)") unless startdate < enddate | ||
end | ||
|
@@ -52,22 +52,38 @@ def validate_strike | |
end | ||
|
||
def valid_call? | ||
(calltype != "literal") || ( | ||
(calldate.to_i <= startdate.to_i) && | ||
((startdate.to_i - calldate.to_i) < Event_Span_Seconds)) | ||
if calltype == "literal" | ||
call_before_start = calldate.to_i <= startdate.to_i | ||
call_not_too_early = (startdate.to_i - calldate.to_i) < Event_Span_Seconds | ||
|
||
return call_before_start && call_not_too_early | ||
end | ||
|
||
# Call is blank or start | ||
true | ||
end | ||
|
||
def valid_strike? | ||
(striketype != "literal") || ( | ||
(strikedate.to_i >= enddate.to_i) && | ||
((strikedate.to_i - enddate.to_i) < Event_Span_Seconds)) | ||
if striketype == "literal" | ||
strike_after_end = strikedate.to_i >= enddate.to_i | ||
strike_not_too_late = (strikedate.to_i - enddate.to_i) < Event_Span_Seconds | ||
|
||
return strike_after_end && strike_not_too_late | ||
end | ||
|
||
# Strike is blank, start, or none | ||
true | ||
end | ||
|
||
def has_call? | ||
# Yes: literal, startdate | ||
# No : blank | ||
self.calltype == "literal" or self.calltype == "startdate" | ||
end | ||
|
||
def has_strike? | ||
# Yes: literal, startdate | ||
# No : blank, none | ||
self.striketype == "literal" or self.striketype == "enddate" | ||
end | ||
|
||
|
@@ -120,6 +136,9 @@ def full_roles | |
else | ||
roles = self.event_roles | ||
|
||
# If eventdate doesn't have these roles, | ||
# copy them from the event | ||
|
||
if not roles.any? { |r| r.role == EventRole::Role_HoT } | ||
roles += self.event.event_roles.find_all { |r| r.role == EventRole::Role_HoT } | ||
end | ||
|
@@ -172,25 +191,27 @@ def run_positions_for(member) | |
self.event_roles.where(member: member) | ||
end | ||
|
||
def self.runify(eventdates) | ||
eventdates.chunk do |ed| | ||
ed.full_roles | ||
end.map do |roles, run| | ||
run | ||
end | ||
def self.group_by_runcrew(eventdates) | ||
# In the event list, we want to be able | ||
# to share one runcrew list with multiple | ||
# adjacent eventdate rows if they are identical | ||
eventdates.chunk(&:full_roles).map { |roles, eds| eds } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested this? I am uncertain if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does still work. I'm not particularly attached to either way of writing it, but I thought all the line breaking made it hard to read. Maybe it would be best to write the chunk with a one-line block? Up to you. |
||
end | ||
|
||
def self.weekify(eventdates) | ||
def self.group_by_weeks_until(eventdates) | ||
# This drives the main events list | ||
# being grouped by weeks until | ||
|
||
eventdates.chunk do |ed| | ||
if ed.startdate < DateTime.now.beginning_of_week | ||
0 | ||
else | ||
DateTime.now.beginning_of_week.upto(ed.startdate.to_datetime).count.fdiv(7).floor # https://stackoverflow.com/a/35092981 | ||
end | ||
end.map do |weeks, eds| | ||
end.map do |weeks_away, eds| | ||
{ | ||
:weeks_away => weeks, | ||
:eventruns => Eventdate.runify(eds) | ||
:weeks_away => weeks_away, | ||
:eventruns => Eventdate.group_by_runcrew(eds) | ||
} | ||
end | ||
end | ||
|
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.
Small typo, should be
enddate