base_controller.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. module Blazer
  2. class BaseController < ApplicationController
  3. # skip filters
  4. filters = _process_action_callbacks.map(&:filter) - [:activate_authlogic]
  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. layout "blazer/application"
  20. private
  21. def process_vars(statement, data_source)
  22. (@bind_vars ||= []).concat(Blazer.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 ["start_time", "end_time"].include?(var)
  32. value = value.to_s.gsub(" ", "+") # fix for Quip bug
  33. end
  34. if var.end_with?("_at")
  35. begin
  36. value = Blazer.time_zone.parse(value)
  37. rescue
  38. # do nothing
  39. end
  40. end
  41. if value =~ /\A\d+\z/
  42. value = value.to_i
  43. elsif value =~ /\A\d+\.\d+\z/
  44. value = value.to_f
  45. end
  46. end
  47. statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
  48. end
  49. end
  50. end
  51. def parse_smart_variables(var, data_source)
  52. smart_var_data_source =
  53. ([data_source] + Array(data_source.settings["inherit_smart_settings"]).map { |ds| Blazer.data_sources[ds] }).find { |ds| ds.smart_variables[var] }
  54. if smart_var_data_source
  55. query = smart_var_data_source.smart_variables[var]
  56. if query.is_a? Hash
  57. smart_var = query.map { |k,v| [v, k] }
  58. elsif query.is_a? Array
  59. smart_var = query.map { |v| [v, v] }
  60. elsif query
  61. result = smart_var_data_source.run_statement(query)
  62. smart_var = result.rows.map { |v| v.reverse }
  63. error = result.error if result.error
  64. end
  65. end
  66. [smart_var, error]
  67. end
  68. def variable_params
  69. 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!
  70. end
  71. helper_method :variable_params
  72. def blazer_user
  73. send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
  74. end
  75. helper_method :blazer_user
  76. def render_errors(resource)
  77. @errors = resource.errors
  78. action = resource.persisted? ? :edit : :new
  79. render action, status: :unprocessable_entity
  80. end
  81. # do not inherit from ApplicationController - #120
  82. def default_url_options
  83. {}
  84. end
  85. end
  86. end