FIX: `expired` option in `EventFinder` didn't work (#164)

FIX: expired option in EventFinder didn’t work (#164)

diff --git a/lib/discourse_post_event/event_finder.rb b/lib/discourse_post_event/event_finder.rb
index 876ec77..9240ecc 100644
--- a/lib/discourse_post_event/event_finder.rb
+++ b/lib/discourse_post_event/event_finder.rb
@@ -9,12 +9,12 @@ module DiscoursePostEvent
 
       if params[:expired]
         event_ids = DiscoursePostEvent::EventDate.expired.order(starts_at: :asc).pluck(:event_id)
-        events = DiscoursePostEvent::Event.where.not(id: event_ids)
       else
         event_ids = DiscoursePostEvent::EventDate.not_expired.order(starts_at: :asc).pluck(:event_id)
-        events = DiscoursePostEvent::Event.where(id: event_ids)
       end
 
+      events = DiscoursePostEvent::Event.where(id: event_ids)
+
       if params[:post_id]
         events = events.where(id: Array(params[:post_id]))
       end
diff --git a/spec/lib/discourse_post_event/event_finder_spec.rb b/spec/lib/discourse_post_event/event_finder_spec.rb
index 5dc0e53..3f7f3d2 100644
--- a/spec/lib/discourse_post_event/event_finder_spec.rb
+++ b/spec/lib/discourse_post_event/event_finder_spec.rb
@@ -87,5 +87,32 @@ describe DiscoursePostEvent::EventFinder do
         expect(subject.search(current_user, { post_id: post2.id })).to match_array([event2])
       end
     end
+
+    context 'by expiration status' do
+      let(:post1) {
+        PostCreator.create!(
+          user,
+          title: 'We should buy a boat',
+          raw: 'The boat market is quite active lately.'
+        )
+      }
+      let(:post2) {
+        PostCreator.create!(
+          user,
+          title: 'We should buy another boat',
+          raw: 'The boat market is very active lately.'
+        )
+      }
+      let!(:event1) { Fabricate(:event, post: post1, original_starts_at: 2.hours.ago, original_ends_at: 1.hour.ago) }
+      let!(:event2) { Fabricate(:event, post: post2, original_starts_at: 1.hour.from_now, original_ends_at: 2.hours.from_now) }
+
+      it 'returns non-expired events when false' do
+        expect(subject.search(current_user, { expired: false })).to match_array([event2])
+      end
+
+      it 'returns expired events when true' do
+        expect(subject.search(current_user, { expired: true })).to match_array([event1])
+      end
+    end
   end
 end

GitHub sha: 3d1caa15e9c5bbe5a196b0358277005bb0867dc2

This commit appears in #164 which was approved by jjaffeux. It was merged by CvX.