FIX: Use with_deleted only in topic and post (#308)

We used `with_deleted` incorrectly in the code. This method does not exist
on `User`, `Badge`, and `Group`. This will cause an error when entering a
numeric id in these three parameter input, forcing the user to enter the
name/username of these inputs.

See https://github.com/discourse/discourse-data-explorer/pull/307#issuecomment-2291017256
This commit is contained in:
锦心 2024-08-15 20:01:53 +08:00 committed by GitHub
parent 6425462b15
commit b063db4ba4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -193,7 +193,13 @@ module ::DiscourseDataExplorer
if string.gsub(/[ _]/, "") =~ /^-?\d+$/
klass_name = (/^(.*)_id$/.match(type.to_s)[1].classify.to_sym)
begin
object = Object.const_get(klass_name).with_deleted.find(string.gsub(/[ _]/, "").to_i)
finder =
if type == :post_id || type == :topic_id
Object.const_get(klass_name).with_deleted
else
Object.const_get(klass_name)
end
object = finder.find(string.gsub(/[ _]/, "").to_i)
value = object.id
rescue ActiveRecord::RecordNotFound
invalid_format string, "The specified #{klass_name} was not found"

View File

@ -58,5 +58,23 @@ RSpec.describe DiscourseDataExplorer::Parameter do
end
end
end
describe "group_id type" do
fab!(:group)
context "when the value provided is an integer" do
it "raises an error if no such group exists" do
expect { param("group_id", :group_id, nil, false).cast_to_ruby("-999") }.to raise_error(
::DiscourseDataExplorer::ValidationError,
)
end
it "returns the group id if the group exists" do
expect(param("group_id", :group_id, nil, false).cast_to_ruby(group.id.to_s)).to eq(
group.id,
)
end
end
end
end
end