Skip to content

Commit

Permalink
Fix #32 - Add scope for upcoming events.
Browse files Browse the repository at this point in the history
Added a scope to the Event model to get ongoing or upcoming Events.
Added spec for testing that only future and ongoing events are selected, while past events are not.
Updated events controller to select only ongoing or upcoming events.
  • Loading branch information
exegeteio committed Oct 4, 2021
1 parent 8893f0f commit 49cb4fb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EventsController < ApplicationController

# GET /events or /events.json
def index
@events = Event.all
@events = Event.ongoing_or_upcoming
end

# GET /events/1 or /events/1.json
Expand Down
2 changes: 2 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Event < ApplicationRecord

validates :start_at, :end_at, presence: true

scope :ongoing_or_upcoming, -> { where("end_at >= ?", Time.zone.now) }

def to_s
name
end
Expand Down
16 changes: 15 additions & 1 deletion spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@
require "rails_helper"

RSpec.describe Event, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:event) { create :event }

it { expect(event).to be_valid }

describe "#ongoing_or_upcoming" do
subject { described_class.ongoing_or_upcoming }

let!(:past_event) { create :event, end_at: 1.day.ago }
let!(:upcoming_event) { create :event, start_at: 3.days.from_now, end_at: 5.days.from_now }
let!(:ongoing_event) { create :event, start_at: 3.days.ago, end_at: 1.day.from_now }

it { is_expected.to include(upcoming_event) }
it { is_expected.to include(ongoing_event) }
it { is_expected.not_to include(past_event) }
end
end

0 comments on commit 49cb4fb

Please sign in to comment.