Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plural variables #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/blazer/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def process_vars(statement, data_source)
value = value.to_s.gsub(" ", "+") # fix for Quip bug
end

if value.is_a?(Array) && Blazer.data_sources[data_source].supports_array?
value = Blazer.data_sources[data_source].array(value)
end

if var.end_with?("_at")
begin
value = Blazer.time_zone.parse(value)
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/blazer/queries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ def show
process_vars(@statement, @query.data_source)

@smart_vars = {}
@plural_vars = {}
@sql_errors = []
data_source = Blazer.data_sources[@query.data_source]
@bind_vars.each do |var|
smart_var, error = parse_smart_variables(var, data_source)
@smart_vars[var] = smart_var if smart_var
plural_var, error = parse_smart_variables(var.singularize, data_source) if smart_var.nil? && error.nil?
@smart_vars[var] = smart_var || plural_var
@plural_vars[var] = true if smart_var.nil? && plural_var.present?
@sql_errors << error if error
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/blazer/_variables.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<% @bind_vars.each_with_index do |var, i| %>
<%= label_tag var, var %>
<% if (data = @smart_vars[var]) %>
<%= select_tag var, options_for_select([[nil, nil]] + data, selected: params[var]), style: "margin-right: 20px; width: 200px; display: none;" %>
<%= select_tag var, options_for_select([[nil, nil]] + data, selected: params[var]), multiple: @plural_vars[var].present?, style: "margin-right: 20px; width: 200px; display: none;" %>
<script>
$("#<%= var %>").selectize({
create: true
Expand Down
8 changes: 8 additions & 0 deletions lib/blazer/adapters/base_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def supports_cohort_analysis?
false # optional
end

def supports_array?
false # optional
end

def array(value)
# optional
end

def cohort_analysis_statement(statement, period:, days:)
# optional
end
Expand Down
10 changes: 10 additions & 0 deletions lib/blazer/adapters/sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ def supports_cohort_analysis?
postgresql? || mysql?
end

def array(value)
if postgresql?
"{#{value.join(",")}}"
end
end

def supports_array?
postgresql?
end

# TODO treat date columns as already in time zone
def cohort_analysis_statement(statement, period:, days:)
raise "Cohort analysis not supported" unless supports_cohort_analysis?
Expand Down
2 changes: 1 addition & 1 deletion lib/blazer/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DataSource

attr_reader :id, :settings

def_delegators :adapter_instance, :schema, :tables, :preview_statement, :reconnect, :cost, :explain, :cancel, :supports_cohort_analysis?, :cohort_analysis_statement
def_delegators :adapter_instance, :schema, :tables, :preview_statement, :reconnect, :cost, :explain, :cancel, :supports_cohort_analysis?, :cohort_analysis_statement, :supports_array?, :array

def initialize(id, settings)
@id = id
Expand Down