-
-
Notifications
You must be signed in to change notification settings - Fork 198
Add "how you found us" summary to the chapter admin page #2466
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||
| class HowYouFoundUsPresenter | ||||||
|
|
||||||
| def initialize(chapter) | ||||||
| @chapter = chapter | ||||||
| end | ||||||
|
|
||||||
| def by_percentage | ||||||
| return how_values.to_h { |how| [how, 0] } unless data_present? | ||||||
|
|
||||||
| # use the largest remainder algorithm so that percentages are whole | ||||||
| # numbers but always add up to 100 | ||||||
| # https://stackoverflow.com/a/13483710/ | ||||||
| entries = how_values.map do |how| | ||||||
| count = raw_stats.fetch(how, 0) | ||||||
| exact = (count / total_responses.to_f) * 100 | ||||||
| percentage_value = exact.floor | ||||||
| remainder = exact - percentage_value | ||||||
| { how: how, percentage_value: percentage_value, remainder: remainder } | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I assume we are running a new-enough Ruby to use the "omit value in Hash when the local variable name matches the Hash key". |
||||||
| end | ||||||
|
|
||||||
| allocated_so_far = entries.sum { |entry| entry[:percentage_value] } | ||||||
| left_to_allocate = 100 - allocated_so_far | ||||||
|
|
||||||
| entries | ||||||
| .sort_by { |entry| [-entry[:remainder], entry[:how].to_s] } | ||||||
| .first(left_to_allocate) | ||||||
| .each { |entry| entry[:percentage_value] += 1 } | ||||||
|
|
||||||
| entries.to_h { |entry| [entry[:how], entry[:percentage_value]] } | ||||||
| end | ||||||
|
|
||||||
| def total_responses | ||||||
| raw_stats.values.sum(0) | ||||||
| end | ||||||
|
|
||||||
| def data_present? | ||||||
| total_responses.positive? | ||||||
| end | ||||||
|
|
||||||
| private | ||||||
|
|
||||||
| def raw_stats | ||||||
| @stats ||= @chapter.members.where.not(how_you_found_us: nil).group(:how_you_found_us).count | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style detail: It is conventional to use the same name for the method and the memoizing instance variables (here raw_stats/stats do no match up). |
||||||
| end | ||||||
|
|
||||||
| def how_values | ||||||
| Member.how_you_found_us.keys | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| RSpec.describe HowYouFoundUsPresenter do | ||
| def add_member(group, how) | ||
| member = Fabricate(:member, how_you_found_us: how) | ||
| Fabricate(:subscription, member: member, group: group) | ||
| member | ||
| end | ||
|
|
||
| def add_member_without_how(group) | ||
| member = Fabricate(:member, how_you_found_us: nil) | ||
| Fabricate(:subscription, member: member, group: group) | ||
| member | ||
| end | ||
|
|
||
| let(:chapter) { Fabricate(:chapter_without_organisers) } | ||
| let(:group) { Fabricate(:group, chapter: chapter) } | ||
| let(:presenter) { HowYouFoundUsPresenter.new(chapter) } | ||
|
|
||
| describe '#by_percentage' do | ||
| it 'returns integer percentages for all enum values in enum order using largest remainder rounding' do | ||
| add_member(group, :from_a_friend) | ||
| add_member(group, :search_engine) | ||
| add_member(group, :search_engine) | ||
| add_member(group, :social_media) | ||
| add_member(group, :social_media) | ||
| add_member(group, :social_media) | ||
|
|
||
| expect(presenter.by_percentage).to eq( | ||
| { | ||
| 'from_a_friend' => 17, | ||
| 'search_engine' => 33, | ||
| 'social_media' => 50, | ||
| 'codebar_host_or_partner' => 0, | ||
| 'other' => 0 | ||
| } | ||
| ) | ||
| expect(presenter.by_percentage.values.sum).to eq(100) | ||
| end | ||
|
|
||
| it 'returns all enum values with zeros when there is no data' do | ||
| expect(presenter.by_percentage).to eq( | ||
| { | ||
| 'from_a_friend' => 0, | ||
| 'search_engine' => 0, | ||
| 'social_media' => 0, | ||
| 'codebar_host_or_partner' => 0, | ||
| 'other' => 0 | ||
| } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| describe '#total_responses' do | ||
| it 'sums the counts' do | ||
| add_member(group, :from_a_friend) | ||
| add_member(group, :search_engine) | ||
|
|
||
| expect(presenter.total_responses).to eq(2) | ||
| end | ||
| end | ||
|
|
||
| describe '#data_present?' do | ||
| it 'returns true when there are responses' do | ||
| add_member(group, :from_a_friend) | ||
|
|
||
| expect(presenter.data_present?).to eq(true) | ||
| end | ||
|
|
||
| it 'returns false when there are no responses' do | ||
| expect(presenter.data_present?).to eq(false) | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: not a blocker.