base_controller.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. module Blazer
  2. class BaseController < ApplicationController
  3. # skip all filters
  4. skip_filter *_process_action_callbacks.map(&:filter)
  5. protect_from_forgery with: :exception
  6. if ENV["BLAZER_PASSWORD"]
  7. http_basic_authenticate_with name: ENV["BLAZER_USERNAME"], password: ENV["BLAZER_PASSWORD"]
  8. end
  9. if Blazer.before_action
  10. before_action Blazer.before_action
  11. end
  12. layout "blazer/application"
  13. before_action :ensure_database_url
  14. private
  15. def ensure_database_url
  16. render text: "BLAZER_DATABASE_URL required" if !ENV["BLAZER_DATABASE_URL"] && !Rails.env.development?
  17. end
  18. def process_vars(statement, data_source)
  19. (@bind_vars ||= []).concat(extract_vars(statement)).uniq!
  20. @bind_vars.each do |var|
  21. params[var] ||= Blazer.data_sources[data_source].variable_defaults[var]
  22. end
  23. @success = @bind_vars.all? { |v| params[v] }
  24. if @success
  25. @bind_vars.each do |var|
  26. value = params[var].presence
  27. value = value.to_i if value.to_i.to_s == value
  28. if var.end_with?("_at")
  29. value = Blazer.time_zone.parse(value) rescue nil
  30. end
  31. value.gsub!(" ", "+") if ["start_time", "end_time"].include?(var) # fix for Quip bug
  32. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  33. end
  34. end
  35. end
  36. def extract_vars(statement)
  37. # strip commented out lines
  38. statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{.*?\}/).map { |v| v[1...-1] }.uniq
  39. end
  40. helper_method :extract_vars
  41. def variable_params
  42. 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)
  43. end
  44. helper_method :variable_params
  45. def blazer_user
  46. send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
  47. end
  48. helper_method :blazer_user
  49. end
  50. end