dashboards_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module Blazer
  2. class DashboardsController < BaseController
  3. before_action :set_dashboard, only: [:show, :edit, :update, :destroy, :refresh]
  4. def index
  5. @dashboards = Blazer::Dashboard.order(:name)
  6. end
  7. def new
  8. @dashboard = Blazer::Dashboard.new
  9. end
  10. def create
  11. @dashboard = Blazer::Dashboard.new
  12. # use creator_id instead of creator
  13. # since we setup association without checking if column exists
  14. @dashboard.creator = blazer_user if @dashboard.respond_to?(:creator_id=) && blazer_user
  15. if update_dashboard(@dashboard)
  16. redirect_to dashboard_path(@dashboard)
  17. else
  18. render :new
  19. end
  20. end
  21. def show
  22. @queries = @dashboard.dashboard_queries.order(:position).preload(:query).map(&:query)
  23. @queries.each do |query|
  24. process_vars(query.statement, query.data_source)
  25. end
  26. @bind_vars ||= []
  27. @smart_vars = {}
  28. @sql_errors = []
  29. @data_sources = @queries.map { |q| Blazer.data_sources[q.data_source] }.uniq
  30. @bind_vars.each do |var|
  31. @data_sources.each do |data_source|
  32. smart_var, error = parse_smart_variables(var, data_source)
  33. ((@smart_vars[var] ||= []).concat(smart_var)).uniq! if smart_var
  34. @sql_errors << error if error
  35. end
  36. end
  37. end
  38. def edit
  39. end
  40. def update
  41. if update_dashboard(@dashboard)
  42. redirect_to dashboard_path(@dashboard, variable_params)
  43. else
  44. render :edit
  45. end
  46. end
  47. def destroy
  48. @dashboard.destroy
  49. redirect_to dashboards_path
  50. end
  51. def refresh
  52. @dashboard.queries.each do |query|
  53. data_source = Blazer.data_sources[query.data_source]
  54. statement = query.statement.dup
  55. process_vars(statement, query.data_source)
  56. Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
  57. data_source.clear_cache(statement)
  58. end
  59. redirect_to dashboard_path(@dashboard, variable_params)
  60. end
  61. protected
  62. def dashboard_params
  63. params.require(:dashboard).permit(:name)
  64. end
  65. def set_dashboard
  66. @dashboard = Blazer::Dashboard.find(params[:id])
  67. end
  68. def update_dashboard(dashboard)
  69. dashboard.assign_attributes(dashboard_params)
  70. Blazer::Dashboard.transaction do
  71. if params[:query_ids].is_a?(Array)
  72. query_ids = params[:query_ids].map(&:to_i)
  73. @queries = Blazer::Query.find(query_ids).sort_by { |q| query_ids.index(q.id) }
  74. end
  75. if dashboard.save
  76. if @queries
  77. @queries.each_with_index do |query, i|
  78. dashboard_query = dashboard.dashboard_queries.where(query_id: query.id).first_or_initialize
  79. dashboard_query.position = i
  80. dashboard_query.save!
  81. end
  82. if dashboard.persisted?
  83. dashboard.dashboard_queries.where.not(query_id: query_ids).destroy_all
  84. end
  85. end
  86. true
  87. end
  88. end
  89. end
  90. end
  91. end