Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.
Merged
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
18 changes: 1 addition & 17 deletions lib/discourse_data_explorer/report_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,10 @@ def self.generate_post(query_id, query_params, opts = {})
build_report_post(query, table, attach_csv: opts[:attach_csv], result:)
end

private
Copy link
Copy Markdown
Contributor Author

@Drenmi Drenmi Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scoping keywords put in the class body only affect instance methods. All methods in this class are class methods and therefore already public.


def self.params_to_hash(query_params)
params = JSON.parse(query_params)
params_hash = {}

if !params.blank?
param_key, param_value = [], []
params.flatten.each.with_index do |data, i|
if i % 2 == 0
param_key << data
else
param_value << data
end
end

params_hash = Hash[param_key.zip(param_value)]
end
Comment on lines -42 to -55
Copy link
Copy Markdown
Contributor Author

@Drenmi Drenmi Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk is the same as Array#to_h.


params_hash
params.map { |p| p.is_a?(Hash) ? [p["key"], p["value"]] : p }.to_h
end

def self.build_report_pms(query, table = "", targets = [], attach_csv: false, result: nil)
Expand Down
20 changes: 20 additions & 0 deletions spec/report_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,24 @@
)
end
end

describe ".params_to_hash" do
context "when passing nothing" do
let(:query_params) { "[]" }

it { expect(described_class.params_to_hash(query_params)).to eq({}) }
end

context "when passing an array of arrays" do
let(:query_params) { '[["foo", 1], ["bar", 2]]' }

it { expect(described_class.params_to_hash(query_params)).to eq({ "foo" => 1, "bar" => 2 }) }
end

context "when passing an array of hashes" do
let(:query_params) { '[{ "key": "foo", "value": 1 }, { "key": "bar", "value": 2 }]' }

it { expect(described_class.params_to_hash(query_params)).to eq({ "foo" => 1, "bar" => 2 }) }
end
end
end