Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6f14fd6
WIP - survey result refactor
jamesspeake Dec 18, 2025
9233201
WIP - survey result refactor
jamesspeake Dec 18, 2025
e9218cd
WIP - survey result refactor
jamesspeake Dec 18, 2025
d76903f
WIP - survey result refactor
jamesspeake Dec 18, 2025
a9381e0
WIP - survey result refactor
jamesspeake Dec 19, 2025
285a036
WIP - survey refactor
jamesspeake Dec 23, 2025
f55661e
WIP add option logic
jamesspeake Dec 29, 2025
d5fa323
[TAN-5965] Working version of new survey result generator
jamesspeake Dec 29, 2025
81ba50f
[TAN-5965] Updated comments
jamesspeake Dec 29, 2025
6b38dfb
Merge branch 'master' into TAN-5965-survey-results-refactor
jamesspeake Dec 29, 2025
8f2ec66
[TAN-5965] Reduced number of SQL queries used when building results
jamesspeake Dec 29, 2025
070e330
[TAN-5965] Removed commented code
jamesspeake Dec 29, 2025
09c3016
[TAN-5965] Fixed community monitor tests
jamesspeake Dec 30, 2025
0dfeaeb
[TAN-5965] Tidied up structure of results generator to use IDs and no…
jamesspeake Jan 5, 2026
0dce4b6
[TAN-5965] Fixed grouping survey results
jamesspeake Jan 5, 2026
85f1297
Merge branch 'master' into TAN-5965-survey-results-refactor
jamesspeake Jan 5, 2026
bb57fb1
[TAN-5965] Moved all logic back into main results generator
jamesspeake Jan 9, 2026
4ae45c8
[TAN-5965] Added multiselect to logic and added more tests
jamesspeake Jan 12, 2026
b715bc3
[TAN-5965] Rearranged test code
jamesspeake Jan 12, 2026
7965062
Merge branch 'master' into TAN-5965-survey-results-refactor
jamesspeake Jan 20, 2026
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
40 changes: 3 additions & 37 deletions back/app/models/custom_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,40 +215,6 @@ def supports_selection?
supports_single_selection? || supports_multiple_selection?
end

def average_rankings(scope)
# This basically starts from all combinations of scope ID, option key (value)
# and position (ordinality) and then calculates the average position for each
# option. "#>> '{}'" is used to unescape the double quotes in the JSONB value.
return {} if input_type != 'ranking'

scope
.where.not("custom_field_values ->> '#{key}' IS NULL")
.joins("CROSS JOIN jsonb_array_elements(custom_field_values->'#{key}') WITH ORDINALITY AS elem(value, ordinality)")
.group("elem.value #>> '{}'")
.average('elem.ordinality')
end

def rankings_counts(scope)
# This basically starts from all combinations of scope ID, option key (value)
# and position (ordinality) and then calculates the count for each option and
# position. "#>> '{}'" is used to unescape the double quotes in the JSONB
# value.
return {} if input_type != 'ranking'

query_result = scope
.where.not("custom_field_values ->> '#{key}' IS NULL")
.joins("CROSS JOIN jsonb_array_elements(custom_field_values->'#{key}') WITH ORDINALITY AS elem(value, ordinality)")
.group("elem.value #>> '{}'", 'elem.ordinality')
.count

# Transform pair to ordinality hash into a hash of hashes
options.pluck(:key).index_with do |option_key|
(1..options.size).index_with do |ranking|
query_result[[option_key, ranking]] || 0
end
end
end

Comment on lines -223 to -256
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved this to results as no longer being done in SQL

def built_in?
!!code
end
Expand Down Expand Up @@ -465,7 +431,7 @@ def ordered_options
@ordered_options ||= if random_option_ordering
options.shuffle.sort_by { |o| o.other ? 1 : 0 }
else
options.order(:ordering)
options # options are by default ordered by :ordering from the association
Comment on lines -465 to +430
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed .order(:ordering) as by default options are returned in this order. Noticed that having this additional clause results in an additional query for options that are already loaded when this method is used.

end
end

Expand Down Expand Up @@ -572,12 +538,12 @@ def sanitize_description_multiloc

# Return domicile options with IDs and descriptions taken from areas
def domicile_options
return options.order(:ordering) unless domicile?
return options unless domicile?

areas = Area.where(custom_field_option_id: options.pluck(:id))
area_id_map = areas.map { |a| { a.custom_field_option_id => { id: a.id, title: a.title_multiloc } } }.reduce({}, :merge)

options.order(:ordering).map do |option|
options.map do |option|
option.key = area_id_map.dig(option.id, :id) || 'outside'
option.title_multiloc = area_id_map.dig(option.id, :title) || MultilocService.new.i18n_to_multiloc('custom_field_options.domicile.outside')
option
Expand Down
6 changes: 3 additions & 3 deletions back/app/services/idea_custom_fields_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def all_fields
fields = if @custom_form.custom_field_ids.empty?
@participation_method.default_fields(@custom_form)
else
@custom_form.custom_fields.includes(:map_config, options: [:image])
@custom_form.custom_fields.includes(:map_config, :matrix_statements, options: [:image])
end

fields = fields.to_a
Expand Down Expand Up @@ -78,12 +78,12 @@ def extra_visible_fields
end

def survey_results_fields(structure_by_category: false)
return enabled_fields unless structure_by_category && @participation_method.supports_custom_field_categories?
return enabled_fields_with_other_options unless structure_by_category && @participation_method.supports_custom_field_categories?

# Restructure the results to order by category with each category as a page

# Remove the original pages
fields = enabled_fields.reject { |field| field.input_type == 'page' }
fields = enabled_fields_with_other_options.reject { |field| field.input_type == 'page' }

# Order fields by the order of categories in custom field
categories = CustomField::QUESTION_CATEGORIES
Expand Down
Loading