Class: Blazer::QueriesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/blazer/queries_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/blazer/queries_controller.rb', line 33

def create
  @query = Blazer::Query.new(query_params)
  @query.creator = blazer_user if @query.respond_to?(:creator)

  if @query.save
    redirect_to query_path(@query, variable_params)
  else
    render :new
  end
end

#destroyObject



117
118
119
120
# File 'app/controllers/blazer/queries_controller.rb', line 117

def destroy
  @query.destroy if @query.editable?(blazer_user)
  redirect_to root_url
end

#editObject



63
64
# File 'app/controllers/blazer/queries_controller.rb', line 63

def edit
end

#homeObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/blazer/queries_controller.rb', line 5

def home
  set_queries(1000)
  @dashboards =
    Blazer::Dashboard.order(:name).map do |d|
      {
        name: "<strong>#{view_context.link_to(d.name, d)}</strong>",
        creator: blazer_user && d.try(:creator) == blazer_user ? "You" : d.try(:creator).try(Blazer.user_name),
        hide: d.name.gsub(/\s+/, ""),
        vars: nil
      }
    end
end

#indexObject



18
19
20
21
# File 'app/controllers/blazer/queries_controller.rb', line 18

def index
  set_queries
  render json: @queries
end

#newObject



23
24
25
26
27
28
29
30
31
# File 'app/controllers/blazer/queries_controller.rb', line 23

def new
  @query = Blazer::Query.new(
    data_source: params[:data_source],
    name: params[:name]
  )
  if params[:fork_query_id]
    @query.statement ||= Blazer::Query.find(params[:fork_query_id]).try(:statement)
  end
end

#refreshObject



93
94
95
96
97
98
99
100
# File 'app/controllers/blazer/queries_controller.rb', line 93

def refresh
  data_source = Blazer.data_sources[@query.data_source]
  @statement = @query.statement.dup
  process_vars(@statement, @query.data_source)
  Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statement
  data_source.clear_cache(@statement)
  redirect_to query_path(@query, variable_params)
end

#runObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/blazer/queries_controller.rb', line 66

def run
  @statement = params[:statement]
  data_source = params[:data_source]
  process_vars(@statement, data_source)
  @only_chart = params[:only_chart]

  if @success
    @query = Query.find_by(id: params[:query_id]) if params[:query_id]

    data_source = @query.data_source if @query && @query.data_source
    @data_source = Blazer.data_sources[data_source]

    @columns, @rows, @error, @cached_at, @just_cached = @data_source.run_main_statement(@statement, user: blazer_user, query: @query, refresh_cache: params[:check])

    render_run
  end

  respond_to do |format|
    format.html do
      render layout: false
    end
    format.csv do
      send_data csv_data(@columns, @rows, @data_source), type: "text/csv; charset=utf-8; header=present", disposition: "attachment; filename=\"#{@query.try(:name).try(:parameterize).presence || 'query'}.csv\""
    end
  end
end

#showObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/blazer/queries_controller.rb', line 44

def show
  @statement = @query.statement.dup
  process_vars(@statement, @query.data_source)

  @smart_vars = {}
  @sql_errors = []
  data_source = Blazer.data_sources[@query.data_source]
  @bind_vars.each do |var|
    query = data_source.smart_variables[var]
    if query
      columns, rows, error, cached_at = data_source.run_statement(query)
      @smart_vars[var] = rows.map { |v| v.reverse }
      @sql_errors << error if error
    end
  end

  Blazer.transform_statement.call(data_source, @statement) if Blazer.transform_statement
end

#tablesObject



122
123
124
125
# File 'app/controllers/blazer/queries_controller.rb', line 122

def tables
  @tables = Blazer.data_sources[params[:data_source]].tables
  render partial: "tables", layout: false
end

#updateObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/blazer/queries_controller.rb', line 102

def update
  if params[:commit] == "Fork"
    @query = Blazer::Query.new
    @query.creator = blazer_user if @query.respond_to?(:creator)
  end
  unless @query.editable?(blazer_user)
    @query.errors.add(:base, "Sorry, permission denied")
  end
  if @query.errors.empty? && @query.update(query_params)
    redirect_to query_path(@query, variable_params)
  else
    render :edit
  end
end