Skip to content

Business intelligence made simple

Merge branch 'master' of https://github.com/ermolaev/blazer into ermolaev-master

Andrew Kane2a5922d86e98
diff --cc app/controllers/blazer/base_controller.rb
index 3598135a,00000000..2ec37753
mode 100644,000000..100644
--- a/app/controllers/blazer/base_controller.rb
+++ b/app/controllers/blazer/base_controller.rb
@@@ -1,48 -1,0 +1,53 @@@
+module Blazer
+ class BaseController < ApplicationController
+ # skip all filters
+ skip_filter *_process_action_callbacks.map(&:filter)
+
+ protect_from_forgery with: :exception
+
+ if ENV["BLAZER_PASSWORD"]
+ http_basic_authenticate_with name: ENV["BLAZER_USERNAME"], password: ENV["BLAZER_PASSWORD"]
+ end
+
+ layout "blazer/application"
+
+ before_action :ensure_database_url
+
+ private
+
+ def ensure_database_url
+ render text: "BLAZER_DATABASE_URL required" if !ENV["BLAZER_DATABASE_URL"] && !Rails.env.development?
+ end
+
+ def process_vars(statement)
+ (@bind_vars ||= []).concat(extract_vars(statement)).uniq!
+ @success = @bind_vars.all? { |v| params[v] }
+
+ if @success
+ @bind_vars.each do |var|
+ value = params[var].presence
+ value = value.to_i if value.to_i.to_s == value
+ if var.end_with?("_at")
+ value = Blazer.time_zone.parse(value) rescue nil
+ end
+ statement.gsub!("{#{var}}", ActiveRecord::Base.connection.quote(value))
+ end
+ end
+ end
+
+ def extract_vars(statement)
+ statement.scan(/\{.*?\}/).map { |v| v[1...-1] }.uniq
+ end
+ helper_method :extract_vars
+
+ def variable_params
+ params.except(:controller, :action, :id, :host, :query, :table_names, :authenticity_token, :utf8, :_method, :commit, :statement, :data_source)
+ end
+ helper_method :variable_params
++
++ def blazer_user
++ send(Blazer.user_method) if Blazer.user_method && respond_to?(Blazer.user_method)
++ end
++ helper_method :blazer_user
+ end
+end
diff --cc app/controllers/blazer/queries_controller.rb
index 65379a69,0e197177..895f8b3b
--- a/app/controllers/blazer/queries_controller.rb
+++ b/app/controllers/blazer/queries_controller.rb
@@@ -59,8 -65,7 +59,8 @@@ module Blaze
if Blazer.audit
audit = Blazer::Audit.new(statement: @statement)
audit.query = @query
+ audit.data_source = data_source
- audit.user = current_user if respond_to?(:current_user) && Blazer.user_class
+ audit.user = blazer_user
audit.save!
end
diff --cc lib/blazer.rb
index 3d1efdd8,dc628787..da3b7657
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@@ -7,11 -5,8 +7,12 @@@ require "blazer/tasks
module Blazer
class << self
+ attr_accessor :audit
attr_reader :time_zone
- attr_accessor :audit, :user_name, :user_class, :timeout, :current_user_name
+ attr_accessor :user_name
+ attr_accessor :user_class
++ attr_accessor :user_method
+ attr_accessor :from_email
end
self.audit = true
self.user_name = :name
diff --cc lib/blazer/engine.rb
index b5a27516,ad3cacb7..cdf80913
--- a/lib/blazer/engine.rb
+++ b/lib/blazer/engine.rb
@@@ -6,12 -6,13 +6,17 @@@ module Blaze
# use a proc instead of a string
app.config.assets.precompile << proc { |path| path =~ /\Ablazer\/application\.(js|css)\z/ }
- Blazer.time_zone ||= Time.zone
- Blazer.user_class ||= User.to_s rescue nil
+ Blazer.time_zone ||= Blazer.settings["time_zone"] || Time.zone
+ Blazer.audit = Blazer.settings.key?("audit") ? Blazer.settings["audit"] : true
+ Blazer.user_name = Blazer.settings["user_name"] if Blazer.settings["user_name"]
+ Blazer.from_email = Blazer.settings["from_email"] if Blazer.settings["from_email"]
+ Blazer.user_class ||= Blazer.settings["user_class"] || User rescue nil
++ Blazer.user_method = Blazer.settings["user_method"]
+ if Blazer.user_class
- Blazer.current_user_name = "current_#{Blazer.user_class.downcase.singularize}"
++ Blazer.user_method ||= "current_#{Blazer.user_class.to_s.downcase.singularize}"
+ end
+
Blazer::Query.belongs_to :creator, class_name: Blazer.user_class.to_s if Blazer.user_class
end
end