Skip to content
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

#32 - Scope Event Index to events that have not happened yet #59

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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