base_controller.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. module Blazer
  2. class BaseController < ApplicationController
  3. # skip all filters
  4. filters = _process_action_callbacks.map(&:filter)
  5. if Rails::VERSION::MAJOR >= 5
  6. skip_before_action(*filters, raise: false)
  7. skip_after_action(*filters, raise: false)
  8. skip_around_action(*filters, raise: false)
  9. else
  10. skip_action_callback *filters
  11. end
  12. protect_from_forgery with: :exception
  13. if ENV["BLAZER_PASSWORD"]
  14. http_basic_authenticate_with name: ENV["BLAZER_USERNAME"], password: ENV["BLAZER_PASSWORD"]
  15. end
  16. if Blazer.before_action
  17. before_action Blazer.before_action
  18. end
  19. layout "blazer/application"
  20. private
  21. def process_vars(statement, data_source)
  22. (@bind_vars ||= []).concat(extract_vars(statement)).uniq!
  23. @bind_vars.each do |var|
  24. params[var] ||= Blazer.data_sources[data_source].variable_defaults[var]
  25. end
  26. @success = @bind_vars.all? { |v| params[v] }
  27. if @success
  28. @bind_vars.each do |var|
  29. value = params[var].presence
  30. if value
  31. if value =~ /\A\d+\z/
  32. value = value.to_i
  33. elsif value =~ /\A\d+\.\d+\z/
  34. value = value.to_f
  35. end
  36. end
  37. if var.end_with?("_at")
  38. value = Blazer.time_zone.parse(value) rescue nil
  39. end
  40. value.gsub!(" ", "+") if ["start_time", "end_time"].include?(var) # fix for Quip bug
  41. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  42. end
  43. end
  44. end
  45. def extract_vars(statement)
  46. # strip commented out lines
  47. # and regex {1} or {1,2}
  48. statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
  49. end
  50. helper_method :extract_vars
  51. def variable_params
  52. 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!
  53. end
  54. helper_method :variable_params
  55. def blazer_user
  56. send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
  57. end
  58. helper_method :blazer_user
  59. end
  60. end