Class: Sage::DashboardsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/sage/dashboards_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/sage/dashboards_controller.rb', line 27

def create
  @dashboard = Blazer::Dashboard.new
  # use creator_id instead of creator
  # since we setup association without checking if column exists
  @dashboard.creator = blazer_user if @dashboard.respond_to?(:creator_id=) && blazer_user

  if update_dashboard(@dashboard)
    redirect_to dashboard_path(@dashboard)
  else
    render_errors @dashboard
  end
end

#destroyObject



84
85
86
87
# File 'app/controllers/sage/dashboards_controller.rb', line 84

def destroy
  @dashboard.destroy
  redirect_to root_path
end

#editObject



73
74
# File 'app/controllers/sage/dashboards_controller.rb', line 73

def edit
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/sage/dashboards_controller.rb', line 5

def index
  @q = Blazer::Dashboard.ransack(params[:q])
  @dashboards = @q.result
  
  # Only include creator if Blazer.user_class is configured
  @dashboards = @dashboards.includes(:creator) if Blazer.user_class
  
  @dashboards = @dashboards.order(:name)
  
  # Apply additional filters if needed
  if blazer_user && params[:filter] == "mine"
    @dashboards = @dashboards.where(creator_id: blazer_user.id).reorder(updated_at: :desc)
  end
  
  # Apply pagination with Pagy
  @pagy, @dashboards = pagy(@dashboards)
end

#newObject



23
24
25
# File 'app/controllers/sage/dashboards_controller.rb', line 23

def new
  @dashboard = Blazer::Dashboard.new
end

#refreshObject



89
90
91
92
93
94
# File 'app/controllers/sage/dashboards_controller.rb', line 89

def refresh
  @dashboard.queries.each do |query|
    refresh_query(query)
  end
  redirect_to dashboard_path(@dashboard, params: variable_params(@dashboard))
end

#showObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/sage/dashboards_controller.rb', line 40

def show
  @queries = @dashboard.dashboard_queries.order(:position).preload(:query).map(&:query)
  @query_errors = {}
  
  @queries.each do |query|
    # Check if the query has a valid data source
    if query.data_source.blank? || !Blazer.data_sources.key?(query.data_source)
      @query_errors[query.id] = "Invalid or missing data source: #{query.data_source.inspect}"
    else
      @success = process_vars(query.statement_object)
    end
  end
  @bind_vars ||= []

  @smart_vars = {}
  @sql_errors = []
  @data_sources = @queries.map { |q| 
    # Use the query's data source if specified, otherwise use the default
    source = q.data_source.presence || Blazer.data_sources.keys.first
    Blazer.data_sources[source]
  }.compact.uniq
  
  @bind_vars.each do |var|
    @data_sources.each do |data_source|
      smart_var, error = parse_smart_variables(var, data_source)
      ((@smart_vars[var] ||= []).concat(smart_var)).uniq! if smart_var
      @sql_errors << error if error
    end
  end

  add_cohort_analysis_vars if @queries.any?(&:cohort_analysis?)
end

#updateObject



76
77
78
79
80
81
82
# File 'app/controllers/sage/dashboards_controller.rb', line 76

def update
  if update_dashboard(@dashboard)
    redirect_to dashboard_path(@dashboard, params: variable_params(@dashboard))
  else
    render_errors @dashboard
  end
end