Skip to content

Commit

Permalink
FIX: Use with_deleted only in topic and post (#308)
Browse files Browse the repository at this point in the history
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 #307 (comment)
  • Loading branch information
Lhcfl authored Aug 15, 2024
1 parent 6425462 commit b063db4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/discourse_data_explorer/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@ def cast_to_ruby(string)
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"
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/parameter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,23 @@ def param(identifier, type, default, nullable)
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

0 comments on commit b063db4

Please sign in to comment.