base_controller.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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)
  19. (@bind_vars ||= []).concat(extract_vars(statement)).uniq!
  20. @success = @bind_vars.all? { |v| params[v] }
  21. if @success
  22. @bind_vars.each do |var|
  23. value = params[var].presence
  24. value = value.to_i if value.to_i.to_s == value
  25. if var.end_with?("_at")
  26. value = Blazer.time_zone.parse(value) rescue nil
  27. end
  28. value.gsub!(" ", "+") if ["start_time", "end_time"].include?(var) # fix for Quip bug
  29. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  30. end
  31. end
  32. end
  33. def extract_vars(statement)
  34. statement.scan(/\{.*?\}/).map { |v| v[1...-1] }.uniq
  35. end
  36. helper_method :extract_vars
  37. def variable_params
  38. 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)
  39. end
  40. helper_method :variable_params
  41. def blazer_user
  42. send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
  43. end
  44. helper_method :blazer_user
  45. end
  46. end