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 Factory for past events.
Added spec for testing that only future and ongoing events are selected, while past events are not.
Added spec for ensuring that past events are not presented on the Event index.
Updated events controller to select only ongoing or upcoming events.
  • Loading branch information
exegeteio authored and ChaelCodes committed Oct 4, 2021
1 parent 8893f0f commit c85223e
Show file tree
Hide file tree
Showing 5 changed files with 30 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
7 changes: 7 additions & 0 deletions spec/factories/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@
start_at { "2021-08-05 21:00:00" }
end_at { "2021-08-05 23:59:00" }
end

trait :past_event do
name { "PastConf" }
description { "The conference you always miss by a few days." }
start_at { 3.days.ago }
end_at { 1.day.ago }
end
end
end
5 changes: 5 additions & 0 deletions spec/features/events/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
expect(page).not_to have_button "Delete"
end

it "only shows future events" do
past_event = create(:event, :past_event)
expect(page).not_to have_link past_event.name, href: event_path(past_event)
end

context "when user logged in" do
let(:user) { create :user }

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, :past_event }
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 c85223e

Please sign in to comment.