base_controller.rb 2.3 KB

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