Skip to content

Commit

Permalink
Validate event end time is after start time
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescmartinez committed Mar 29, 2019
1 parent 28ab42f commit b1cfbd1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Event < ApplicationRecord
validates :name, presence: true
validates :group, presence: true
validates :foursquare_venue_id, presence: true
validate :end_at_cannot_be_before_start_at

scope :future_or_now, -> { where("end_at >= ?", Time.current).order(:start_at) }
scope :past, -> { where("end_at < ?", Time.current).order(start_at: :desc) }
Expand All @@ -29,6 +30,10 @@ def formatted_local_time

private

def end_at_cannot_be_before_start_at
errors.add(:end_at, "can't be before start_at") if end_at && start_at && end_at <= start_at
end

def ensure_updated_foursquare_venue_data
if new_record?
EventVenueHydrator.run(self)
Expand Down
10 changes: 10 additions & 0 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
it { is_expected.to validate_presence_of(:group) }
it { is_expected.to validate_presence_of(:foursquare_venue_id) }

describe "validate" do
describe "#end_at_cannot_be_before_start_at" do
it "validates end at is after start at" do
event = build(:event, start_at: Time.current, end_at: Time.current.yesterday)
expect(event).not_to be_valid
expect(event.errors[:end_at]).to eq(["can't be before start_at"])
end
end
end

describe ".future_or_now" do
it "returns all future events" do
events = create_list(:future_event, 3)
Expand Down
4 changes: 2 additions & 2 deletions spec/system/group_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@

it "orders past events with the most recent at the top", vcr: { cassette_name: :foursquare_venue_details } do
group = create(:group)
create(:past_event, group: group, name: "Old Name", start_at: group.created_at - 1.second)
create(:past_event, group: group, name: "More Recent Name", start_at: group.created_at + 1.second)
create(:past_event, group: group, name: "Old Name", start_at: group.created_at - 1.second, end_at: group.created_at)
create(:past_event, group: group, name: "More Recent Name", start_at: group.created_at + 1.second, end_at: group.created_at + 2.seconds)
visit group_path(group)
expect(page.body.index("More Recent Name")).to be < page.body.index("Old Name")
end
Expand Down

0 comments on commit b1cfbd1

Please sign in to comment.