base_controller.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.to_sym
  18. end
  19. before_action :set_js_routes
  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 ["start_time", "end_time"].include?(var)
  33. value = value.to_s.gsub(" ", "+") # fix for Quip bug
  34. end
  35. if var.end_with?("_at")
  36. begin
  37. value = Blazer.time_zone.parse(value)
  38. rescue
  39. # do nothing
  40. end
  41. end
  42. if value =~ /\A\d+\z/
  43. value = value.to_i
  44. elsif value =~ /\A\d+\.\d+\z/
  45. value = value.to_f
  46. end
  47. end
  48. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  49. end
  50. end
  51. end
  52. def parse_smart_variables(var, data_source)
  53. smart_var_data_source =
  54. ([data_source] + Array(data_source.settings["inherit_smart_settings"]).map { |ds| Blazer.data_sources[ds] }).find { |ds| ds.smart_variables[var] }
  55. if smart_var_data_source
  56. query = smart_var_data_source.smart_variables[var]
  57. if query.is_a? Hash
  58. smart_var = query.map { |k,v| [v, k] }
  59. elsif query.is_a? Array
  60. smart_var = query.map { |v| [v, v] }
  61. elsif query
  62. result = smart_var_data_source.run_statement(query)
  63. smart_var = result.rows.map { |v| v.reverse }
  64. error = result.error if result.error
  65. end
  66. end
  67. [smart_var, error]
  68. end
  69. def extract_vars(statement)
  70. # strip commented out lines
  71. # and regex {1} or {1,2}
  72. statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
  73. end
  74. helper_method :extract_vars
  75. def variable_params
  76. 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, :run_id).permit!
  77. end
  78. helper_method :variable_params
  79. def blazer_user
  80. send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
  81. end
  82. helper_method :blazer_user
  83. def render_errors(resource)
  84. @errors = resource.errors
  85. action = resource.persisted? ? :edit : :new
  86. render action, status: :unprocessable_entity
  87. end
  88. def set_js_routes
  89. gon.push(
  90. root_path: root_path
  91. )
  92. end
  93. end
  94. end