Business intelligence made simple
Improved pattern for variable params
Andrew Kane29b8a50fbd14
diff --git a/app/controllers/blazer/base_controller.rb b/app/controllers/blazer/base_controller.rbindex 950977a0..17703be3 100644--- a/app/controllers/blazer/base_controller.rb+++ b/app/controllers/blazer/base_controller.rb@@ -86,8 +86,13 @@ module Blazer[smart_var, error]end- def variable_params- params.except(:controller, :action, :id, :host, :query, :dashboard, :query_id, :query_ids, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement, :data_source, :name, :fork_query_id, :blazer, :run_id, :script_name, :original_script_name).permit!+ # not permitted to be passed to url helpers+ BLACKLISTED_KEYS = [:controller, :action, :id, :host, :query, :dashboard, :query_id, :query_ids, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement, :data_source, :name, :fork_query_id, :blazer, :run_id, :script_name, :original_script_name]++ # remove blacklisted keys from both params and permitted keys for better sleep+ def variable_params(record)+ permitted_keys = record.variables - BLACKLISTED_KEYS.map(&:to_s)+ params.except(*BLACKLISTED_KEYS).permit(*permitted_keys)endhelper_method :variable_paramsdiff --git a/app/controllers/blazer/dashboards_controller.rb b/app/controllers/blazer/dashboards_controller.rbindex 98a214bb..f45297af 100644--- a/app/controllers/blazer/dashboards_controller.rb+++ b/app/controllers/blazer/dashboards_controller.rb@@ -43,7 +43,7 @@ module Blazerdef updateif update_dashboard(@dashboard)- redirect_to dashboard_path(@dashboard, variable_params)+ redirect_to dashboard_path(@dashboard, variable_params(@dashboard))elserender_errors @dashboardend@@ -62,7 +62,7 @@ module BlazerBlazer.transform_statement.call(data_source, statement) if Blazer.transform_statementdata_source.clear_cache(statement)end- redirect_to dashboard_path(@dashboard, variable_params)+ redirect_to dashboard_path(@dashboard, variable_params(@dashboard))endprivatediff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rbindex 96d4a76e..80db6282 100644--- a/app/controllers/blazer/queries_controller.rb+++ b/app/controllers/blazer/queries_controller.rb@@ -45,7 +45,7 @@ module Blazer@query.creator = blazer_user if @query.respond_to?(:creator)if @query.save- redirect_to query_path(@query, variable_params)+ redirect_to query_path(@query, variable_params(@query))elserender_errors @queryend@@ -156,7 +156,7 @@ module Blazerprocess_vars(@statement, @query.data_source)Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statementdata_source.clear_cache(@statement)- redirect_to query_path(@query, variable_params)+ redirect_to query_path(@query, variable_params(@query))enddef update@@ -168,7 +168,7 @@ module Blazer@query.errors.add(:base, "Sorry, permission denied")endif @query.errors.empty? && @query.update(query_params)- redirect_to query_path(@query, variable_params)+ redirect_to query_path(@query, variable_params(@query))elserender_errors @queryenddiff --git a/app/models/blazer/dashboard.rb b/app/models/blazer/dashboard.rbindex 941d95ab..7f843a50 100644--- a/app/models/blazer/dashboard.rb+++ b/app/models/blazer/dashboard.rb@@ -6,6 +6,10 @@ module Blazervalidates :name, presence: true+ def variables+ queries.flat_map { |q| q.variables }.uniq+ end+def to_param[id, name.gsub("'", "").parameterize].join("-")enddiff --git a/app/views/blazer/dashboards/_form.html.erb b/app/views/blazer/dashboards/_form.html.erbindex dfd047cd..f6ef1396 100644--- a/app/views/blazer/dashboards/_form.html.erb+++ b/app/views/blazer/dashboards/_form.html.erb@@ -1,4 +1,4 @@-<%= form_for @dashboard, url: (@dashboard.persisted? ? dashboard_path(@dashboard, variable_params) : dashboards_path(variable_params)), html: {id: "app", class: "small-form"} do |f| %>+<%= form_for @dashboard, url: (@dashboard.persisted? ? dashboard_path(@dashboard, variable_params(@dashboard)) : dashboards_path(variable_params(@dashboard))), html: {id: "app", class: "small-form"} do |f| %><% if @dashboard.errors.any? %><div class="alert alert-danger"><%= @dashboard.errors.full_messages.first %></div><% end %>diff --git a/app/views/blazer/dashboards/show.html.erb b/app/views/blazer/dashboards/show.html.erbindex 29496f0f..1305cfff 100644--- a/app/views/blazer/dashboards/show.html.erb+++ b/app/views/blazer/dashboards/show.html.erb@@ -10,7 +10,7 @@</h3></div><div class="col-sm-3 text-right">- <%= link_to "Edit", edit_dashboard_path(@dashboard, variable_params), class: "btn btn-info" %>+ <%= link_to "Edit", edit_dashboard_path(@dashboard, variable_params(@dashboard)), class: "btn btn-info" %></div></div></div>@@ -21,7 +21,7 @@<% if @data_sources.any? { |ds| ds.cache_mode != "off" } %><p class="text-muted" style="float: right;">Some queries may be cached- <%= link_to "Refresh", refresh_dashboard_path(@dashboard, variable_params), method: :post %>+ <%= link_to "Refresh", refresh_dashboard_path(@dashboard, variable_params(@dashboard)), method: :post %></p><% end %>@@ -33,7 +33,7 @@<% @queries.each_with_index do |query, i| %><div class="chart-container">- <h4><%= link_to query.friendly_name, query_path(query, variable_params), target: "_blank" %></h4>+ <h4><%= link_to query.friendly_name, query_path(query, variable_params(query)), target: "_blank" %></h4><div id="chart-<%= i %>" class="chart"><p class="text-muted">Loading...</p></div>diff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erbindex 81a5a124..7585a1dc 100644--- a/app/views/blazer/queries/_form.html.erb+++ b/app/views/blazer/queries/_form.html.erb@@ -3,7 +3,7 @@<% end %><div id="app" v-cloak>- <%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {autocomplete: "off"} do |f| %>+ <%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params(@query)) : queries_path(variable_params(@query))), html: {autocomplete: "off"} do |f| %><div class="row"><div id="statement-box" class="col-xs-8"><div class= "form-group">@@ -67,7 +67,7 @@</div><script>- <%= blazer_js_var "params", variable_params %>+ <%= blazer_js_var "params", variable_params(@query) %><%= blazer_js_var "previewStatement", Hash[Blazer.data_sources.map { |k, v| [k, (v.preview_statement rescue "")] }] %>var app = new Vue({diff --git a/app/views/blazer/queries/run.html.erb b/app/views/blazer/queries/run.html.erbindex d992f4e2..cdf58367 100644--- a/app/views/blazer/queries/run.html.erb+++ b/app/views/blazer/queries/run.html.erb@@ -20,7 +20,7 @@<% end %><% if @query && params[:query_id] %>- <%= link_to "Refresh", refresh_query_path(@query, variable_params), method: :post %>+ <%= link_to "Refresh", refresh_query_path(@query, variable_params(@query)), method: :post %><% end %></p><% end %>@@ -36,7 +36,7 @@<% if @query && @result.forecastable? && !params[:forecast] %>·- <%= link_to "Forecast", query_path(@query, {forecast: "t"}.merge(variable_params)) %>+ <%= link_to "Forecast", query_path(@query, {forecast: "t"}.merge(variable_params(@query))) %><% end %></p><% end %>diff --git a/app/views/blazer/queries/show.html.erb b/app/views/blazer/queries/show.html.erbindex 75d76f63..77884448 100644--- a/app/views/blazer/queries/show.html.erb+++ b/app/views/blazer/queries/show.html.erb@@ -10,8 +10,8 @@</h3></div><div class="col-sm-3 text-right">- <%= link_to "Edit", edit_query_path(@query, variable_params), class: "btn btn-default", disabled: [email protected]?(blazer_user) %>- <%= link_to "Fork", new_query_path(variable_params.merge(fork_query_id: @query.id, data_source: @query.data_source, name: @query.name)), class: "btn btn-info" %>+ <%= link_to "Edit", edit_query_path(@query, variable_params(@query)), class: "btn btn-default", disabled: [email protected]?(blazer_user) %>+ <%= link_to "Fork", new_query_path(variable_params(@query).merge(fork_query_id: @query.id, data_source: @query.data_source, name: @query.name)), class: "btn btn-info" %><% if !@error && @success %><%= button_to "Download", run_queries_path(query_id: @query.id, format: "csv", forecast: params[:forecast]), params: {statement: @statement}, class: "btn btn-primary" %>@@ -56,7 +56,7 @@$("#results").addClass("query-error").html(message)}- <%= blazer_js_var "data", variable_params.merge(statement: @statement, query_id: @query.id, data_source: @query.data_source) %>+ <%= blazer_js_var "data", variable_params(@query).merge(statement: @statement, query_id: @query.id, data_source: @query.data_source) %>runQuery(data, showRun, showError)</script>