Skip to content

Commit

Permalink
Merge pull request #5606 from avalonmediasystem/timeline_search
Browse files Browse the repository at this point in the history
Make timeline searches case insensitive
  • Loading branch information
masaball authored Feb 2, 2024
2 parents 39e6010 + e865ca3 commit ca7a8ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 4 additions & 3 deletions app/models/timeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
class Timeline < ActiveRecord::Base
belongs_to :user
scope :by_user, ->(user) { where(user_id: user.id) }
scope :title_like, ->(title_filter) { where("title LIKE ?", "%#{title_filter}%") }
scope :desc_like, ->(desc_filter) { where("description LIKE ?", "%#{desc_filter}%") }
scope :with_tag, ->(tag_filter) { where("tags LIKE ?", "%\n- #{tag_filter}\n%") }
# Explicitly cast everything to lowercase for DB agnostic case-insentive search.
scope :title_like, ->(title_filter) { where("LOWER(title) LIKE ?", "%#{title_filter.downcase}%") }
scope :desc_like, ->(desc_filter) { where("LOWER(description) LIKE ?", "%#{desc_filter.downcase}%") }
scope :with_tag, ->(tag_filter) { where("LOWER(tags) LIKE ?", "%\n- #{tag_filter.downcase}\n%") }

validates :user, presence: true
validates :title, presence: true
Expand Down
13 changes: 5 additions & 8 deletions spec/models/timeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@
let(:timeline3) { FactoryBot.create(:timeline, title: 'Favorites') }
let(:title_filter) { 'moose' }
it 'returns timelines with matching titles' do
# Commented out since case insensitivity is default for mysql but not postgres
# expect(Timeline.title_like(title_filter)).to include(timeline1)
expect(Timeline.title_like(title_filter)).to include(timeline1)
expect(Timeline.title_like(title_filter)).to include(timeline2)
end
it 'does not return timelines without matching titles' do
Expand All @@ -171,8 +170,7 @@
let(:timeline3) { FactoryBot.create(:timeline, description: 'Favorites') }
let(:desc_filter) { 'moose' }
it 'returns timelines with matching descriptions' do
# Commented out since case insensitivity is default for mysql but not postgres
# expect(Timeline.desc_like(desc_filter)).to include(timeline1)
expect(Timeline.desc_like(desc_filter)).to include(timeline1)
expect(Timeline.desc_like(desc_filter)).to include(timeline2)
end
it 'does not return timelines without matching descriptions' do
Expand All @@ -185,15 +183,14 @@
let(:timeline3) { FactoryBot.create(:timeline, tags: ['smoose', 'Goose']) }
let(:timeline4) { FactoryBot.create(:timeline, tags: ['Goose']) }
let(:tag_filter) { 'moose' }
it 'returns timelines with exact matching tags' do
# Commented out since case insensitivity is default for mysql but not postgres
# expect(Timeline.with_tag(tag_filter)).to include(timeline1)
it 'returns timelines with matching tags' do
expect(Timeline.with_tag(tag_filter)).to include(timeline1)
expect(Timeline.with_tag(tag_filter)).to include(timeline2)
end
it 'does not return timelines with partial matching tag' do
expect(Timeline.with_tag(tag_filter)).not_to include(timeline3)
end
it 'does not return timelines with without the tag' do
it 'does not return timelines without the tag' do
expect(Timeline.with_tag(tag_filter)).not_to include(timeline4)
end
end
Expand Down

0 comments on commit ca7a8ca

Please sign in to comment.