diff --git a/backend/app/controllers/post_versions_controller.rb b/backend/app/controllers/post_versions_controller.rb index bcf67c8..04032e3 100644 --- a/backend/app/controllers/post_versions_controller.rb +++ b/backend/app/controllers/post_versions_controller.rb @@ -30,7 +30,9 @@ class PostVersionsController < ApplicationController q = q.where('post_versions.post_id = ?', post_id) if post_id if tag_name escaped = ActiveRecord::Base.sanitize_sql_like(tag_name.name) - q = q.where("CONCAT(' ', post_versions.tags, ' ') LIKE ?", "% #{ escaped } %") + q = q.where(("CONCAT(' ', post_versions.tags, ' ') LIKE :kw " + + "OR CONCAT(' ', prev.tags, ' ') LIKE :kw"), + kw: "% #{ escaped } %") end count = q.except(:select, :order, :limit, :offset).count diff --git a/backend/spec/requests/posts_spec.rb b/backend/spec/requests/posts_spec.rb index 0d7e679..605fd8d 100644 --- a/backend/spec/requests/posts_spec.rb +++ b/backend/spec/requests/posts_spec.rb @@ -927,16 +927,23 @@ RSpec.describe 'Posts API', type: :request do ) end - it 'matches tag filter against current tags snapshot only' do + it 'filters versions by tag when the tag exists in either current or previous snapshot' do get '/posts/versions', params: { post: post_record.id, tag: tag.id } expect(response).to have_http_status(:ok) - expect(json.fetch('count')).to eq(1) + expect(json.fetch('count')).to eq(2) versions = json.fetch('versions') - expect(versions.size).to eq(1) - expect(versions[0]['version_no']).to eq(1) - expect(versions[0]['tags']).to include( + expect(versions.map { |v| v['post_id'] }).to all(eq(post_record.id)) + expect(versions.map { |v| v['version_no'] }).to eq([2, 1]) + + latest = versions[0] + first = versions[1] + + expect(latest['tags']).to include( + { 'name' => 'spec_tag', 'type' => 'removed' } + ) + expect(first['tags']).to include( { 'name' => 'spec_tag', 'type' => 'added' } ) end