Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Fix spec regressions caused by test-prof change #326

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions app/models/discourse_data_explorer/query.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# frozen_string_literal: true

module ::DiscourseDataExplorer
class QueryFinder
def self.find(id)
default_query = Queries.default[id.to_s]
return raise ActiveRecord::RecordNotFound unless default_query

query = Query.find_by(id: id) || Query.new
query.attributes = default_query
query.user_id = Discourse::SYSTEM_USER_ID.to_s
query
end
end

class Query < ActiveRecord::Base
self.table_name = "data_explorer_queries"

Expand Down Expand Up @@ -35,15 +47,17 @@ def slug
end

def self.find(id)
if id.to_i < 0
default_query = Queries.default[id.to_s]
return raise ActiveRecord::RecordNotFound unless default_query
query = Query.find_by(id: id) || Query.new
query.attributes = default_query
query.user_id = Discourse::SYSTEM_USER_ID.to_s
query
else
super
return super if id.to_i >= 0
QueryFinder.find(id)
end

private

# for `Query.unscoped.find`
class ActiveRecord_Relation
def find(id)
return super if id.to_i >= 0
QueryFinder.find(id)
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/models/query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

RSpec.describe DiscourseDataExplorer::Query do
before { SiteSetting.data_explorer_enabled = true }

describe ".find" do
it "returns default queries" do
expect(DiscourseDataExplorer::Query.find(-1)).to be_present
end
end

describe "unscoped .find" do
it "returns default queries" do
expect(DiscourseDataExplorer::Query.unscoped.find(-1)).to be_present
end
end
end