FIX: better handling of edge cases (#187)

- Require query name is present
- Ensure all routes are treated by default as .json, so errors flow correctly
- Remove superflous save/cancel controls from group settings
- Remove group control when item is destroyed
- Disable editing of query when it is deleted

Co-authored-by: Osama Sayegh <asooomaasoooma90@gmail.com>
This commit is contained in:
Sam 2022-11-18 07:09:50 +11:00 committed by GitHub
parent 41a5e0a27a
commit 2313237a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 39 deletions

View File

@ -6,6 +6,7 @@ module DataExplorer
has_many :query_groups has_many :query_groups
has_many :groups, through: :query_groups has_many :groups, through: :query_groups
belongs_to :user belongs_to :user
validates :name, presence: true
scope :for_group, ->(group) do scope :for_group, ->(group) do
where(hidden: false) where(hidden: false)

View File

@ -88,6 +88,7 @@
</div> </div>
{{/if}} {{/if}}
{{#if (not selectedItem.destroyed)}}
<div class="pull-left"> <div class="pull-left">
<div class="groups"> <div class="groups">
<span class="label">{{i18n "explorer.allow_groups"}}</span> <span class="label">{{i18n "explorer.allow_groups"}}</span>
@ -99,25 +100,9 @@
onSelect=(action (mut selectedItem.group_ids)) onSelect=(action (mut selectedItem.group_ids))
}} }}
</span> </span>
</div>
{{#if runDisabled}} </div>
{{#unless editing}}
<span class="setting-controls">
{{d-button
class="ok"
action=(action "save")
icon="check"
}}
{{d-button
class="cancel"
action=(action "discard")
icon="times"
}}
</span>
{{/unless}}
{{/if}} {{/if}}
</div>
</div>
<div class="clear"></div> <div class="clear"></div>
@ -126,7 +111,7 @@
<div class="query-editor {{if hideSchema "no-schema"}}"> <div class="query-editor {{if hideSchema "no-schema"}}">
<div class="panels-flex"> <div class="panels-flex">
<div class="editor-panel"> <div class="editor-panel">
{{ace-editor content=selectedItem.sql mode="sql"}} {{ace-editor content=selectedItem.sql mode="sql" disabled=selectedItem.destroyed}}
</div> </div>
<div class="right-panel"> <div class="right-panel">

View File

@ -886,8 +886,9 @@ SQL
DataExplorer::Engine.routes.draw do DataExplorer::Engine.routes.draw do
root to: "query#index" root to: "query#index"
get 'schema' => "query#schema"
get 'queries' => "query#index" get 'queries' => "query#index"
scope "/", defaults: { format: :json } do
get 'schema' => "query#schema"
get 'groups' => "query#groups" get 'groups' => "query#groups"
post 'queries' => "query#create" post 'queries' => "query#create"
get 'queries/:id' => "query#show" get 'queries/:id' => "query#show"
@ -895,6 +896,7 @@ SQL
delete 'queries/:id' => "query#destroy" delete 'queries/:id' => "query#destroy"
post 'queries/:id/run' => "query#run", constraints: { format: /(json|csv)/ } post 'queries/:id/run' => "query#run", constraints: { format: /(json|csv)/ }
end end
end
Discourse::Application.routes.append do Discourse::Application.routes.append do
get '/g/:group_name/reports' => 'data_explorer/query#group_reports_index' get '/g/:group_name/reports' => 'data_explorer/query#group_reports_index'

View File

@ -107,6 +107,19 @@ describe DataExplorer::QueryController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
it "returns a proper json error for invalid updates" do
query = DataExplorer::Query.find(-4)
put "/admin/plugins/explorer/queries/#{query.id}", params: {
"query" => {
"name" => "",
},
"id" => query.id }
expect(response.status).to eq(422)
expect(response.parsed_body["errors"]).to eq(["Name can't be blank"])
end
end end
describe "#run" do describe "#run" do
@ -304,8 +317,6 @@ describe DataExplorer::QueryController do
DataExplorer.send(:remove_const, "QUERY_RESULT_MAX_LIMIT") DataExplorer.send(:remove_const, "QUERY_RESULT_MAX_LIMIT")
DataExplorer.const_set("QUERY_RESULT_MAX_LIMIT", 2) DataExplorer.const_set("QUERY_RESULT_MAX_LIMIT", 2)
ids = Post.order(:id).pluck(:id)
query = make_query <<~SQL query = make_query <<~SQL
SELECT id FROM posts SELECT id FROM posts
SQL SQL