base_controller.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. private
  14. def process_vars(statement, data_source)
  15. (@bind_vars ||= []).concat(extract_vars(statement)).uniq!
  16. @bind_vars.each do |var|
  17. params[var] ||= Blazer.data_sources[data_source].variable_defaults[var]
  18. end
  19. @success = @bind_vars.all? { |v| params[v] }
  20. if @success
  21. @bind_vars.each do |var|
  22. value = params[var].presence
  23. if value
  24. if value =~ /\A\d+\z/
  25. value = value.to_i
  26. elsif value =~ /\A\d+\.\d+\z/
  27. value = value.to_f
  28. end
  29. end
  30. if var.end_with?("_at")
  31. value = Blazer.time_zone.parse(value) rescue nil
  32. end
  33. value.gsub!(" ", "+") if ["start_time", "end_time"].include?(var) # fix for Quip bug
  34. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  35. end
  36. end
  37. end
  38. def extract_vars(statement)
  39. # strip commented out lines
  40. # and regex {1} or {1,2}
  41. statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{.*?\}/).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) }.uniq
  42. end
  43. helper_method :extract_vars
  44. def variable_params
  45. 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).permit!
  46. end
  47. helper_method :variable_params
  48. def blazer_user
  49. send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
  50. end
  51. helper_method :blazer_user
  52. end
  53. end