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

Add to dashboard from query page #239

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 33 additions & 1 deletion app/controllers/blazer/queries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Blazer
class QueriesController < BaseController
before_action :set_query, only: [:show, :edit, :update, :destroy, :refresh]
before_action :set_query, only: [:show, :edit, :update, :destroy, :refresh, :add_to_dashboards]
before_action :set_dashboards, only: [:show]
before_action :set_data_source, only: [:tables, :docs, :schema, :cancel]

def home
Expand Down Expand Up @@ -198,6 +199,33 @@ def cancel
head :ok
end

def add_to_dashboards
dashboard_ids = params[:query] ? params[:query][:dashboard_ids] : []
if dashboard_ids && @query.persisted?
# Remove Dashboard Queries where dashboard ID was not selected
destroyed_dashboard_queries = @query.dashboard_queries.where.not(dashboard_id: dashboard_ids).destroy_all

# Fix/Reset dashboard_query positions after removal of queries
destroyed_dashboard_queries.each do |destroyed_dashboard_query|
dashboard_id = destroyed_dashboard_query.dashboard_id
Blazer::Dashboard.find(dashboard_id).dashboard_queries.order(:position).each_with_index do |dashboard_query, i|
dashboard_query.position = i
dashboard_query.save!
end
end

# Add query to selected dashboards
dashboard_ids.each do |dashboard_id|
# only modifies new dashboard queries
@query.dashboard_queries.where(dashboard_id: dashboard_id).first_or_initialize do |dashboard_query|
dashboard_query.position = Blazer::Dashboard.find(dashboard_id).query_ids.length
dashboard_query.save!
end
end
end
redirect_to query_path(@query)
end

private

def set_data_source
Expand Down Expand Up @@ -312,6 +340,10 @@ def set_query
end
end

def set_dashboards
@dashboards = Blazer::Dashboard.order(:name)
end

def render_forbidden
render plain: "Access denied", status: :forbidden
end
Expand Down
28 changes: 28 additions & 0 deletions app/views/blazer/queries/_add_to_dashboards.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<button type="button" class="btn btn-default pull-right" data-toggle="modal" data-target="#addToDashboards" style="margin-bottom: 10px;">Add to Dashboards</button>
<!-- Modal -->
<div class="modal fade" id="addToDashboards" tabindex="-1" role="dialog" aria-labelledby="addToDashboardsLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="addToDashboardsLabel">Add <%= query.name %> to Dashboards</h4>
</div>
<div class="modal-body">
<%= form_tag(add_to_dashboards_query_path, method: 'post', :class => 'form-group') do %>
<div class="modal-body">
<div class="list-group">
<% dashboards.each do |dashboard| %>
<label type="button" class="list-group-item">
<%= check_box_tag "query[dashboard_ids][]", dashboard.id, dashboard.query_ids.include?(@query.id) %>
<span class="button-indecator"><%= dashboard.name %></span>
<%= link_to 'View Dashboard', dashboard_path(dashboard), :target => "_blank", :class => 'pull-right' %>
</label>
<% end %>
</div>
<%= submit_tag 'Add Query to Dashboards', :class => 'btn btn-primary btn-block' %>
<% end %>
</div>
</div>
</div>
</div>

6 changes: 6 additions & 0 deletions app/views/blazer/queries/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

<div style="margin-bottom: 60px;"></div>

<div class="row">
<div class="col-sm-12">
<%= render "add_to_dashboards", query: @query, dashboards: @dashboards %>
</div>
</div>

<% if @sql_errors.any? %>
<div class="alert alert-danger">
<ul>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
post :run, on: :collection # err on the side of caution
post :cancel, on: :collection
post :refresh, on: :member
post :add_to_dashboards, on: :member
get :tables, on: :collection
get :schema, on: :collection
get :docs, on: :collection
Expand Down