Skip to content

Commit

Permalink
Merge branch 'master' into feature_project_setup
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinKato committed Nov 6, 2023
2 parents ef0c9a2 + bea737a commit c34d3ba
Show file tree
Hide file tree
Showing 166 changed files with 33,004 additions and 18,367 deletions.
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ install-front-dependencies: &install-front-dependencies
npm ci
npm i --save @citizenlab/cl2-component-library@${CL2_VERSION}
parameters:
# This parameter is used to trigger the main workflow
trigger:
Expand Down Expand Up @@ -839,8 +838,8 @@ workflows:
- master
ssh_host: $CITIZENLAB_CLUSTER_IP_ADDRESS_STAGING
compose_file: docker-compose-staging.yml
stack_name: cl2-back-stg
env_file: ".env-staging"
stack_name: "cl2"
env_file: ".env-web"

back-deploy-production:
when: << pipeline.parameters.back >>
Expand Down
3 changes: 3 additions & 0 deletions back/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Style/NumericPredicate:
# are not the same if `something` is not a number.
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Layout/FirstArgumentIndentation:
EnforcedStyle: consistent

Expand Down
5 changes: 4 additions & 1 deletion back/app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def self.membership_types
private

def generate_slug
self.slug ||= SlugService.new.generate_slug self, title_multiloc.values.first
self.slug ||= SlugService.new.generate_slug(
self,
title_multiloc.find { |_key, value| value.present? }&.last
)
end

def set_membership_type
Expand Down
7 changes: 2 additions & 5 deletions back/app/uploaders/initiative_image_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
class InitiativeImageUploader < BaseImageUploader
version :small do
process resize_to_fill: [96, 96]
# process optimize: [{ quality: 90, quiet: true }]
end

version :medium do
process resize_to_fill: [298, 135]
# process optimize: [{ quality: 90, quiet: true }]
process resize_to_fill: [480, 217]
end

version :large do
process resize_to_limit: [480, nil]
# process optimize: [{ quality: 90, quiet: true }]
process resize_to_limit: [960, nil]
end

version :fb do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,8 @@ def infer_scope_class(scope)
end

def serialize_admin_publications(scope)
publications = serialize_records(scope)

# The parent publications must be listed before their children since the
# children publications reference their parent.
child_to_parent = publications.transform_values do |attributes|
Array.wrap(attributes[:parent_ref]&.id)
end

each_node = ->(&block) { child_to_parent.each_key(&block) }
each_child = ->(node, &block) { child_to_parent[node].each(&block) }
ordered_ids = TSort.tsort(each_node, each_child)

publications.slice(*ordered_ids)
# This code will stop working when folders can contain folders
serialize_records(scope.order(parent_id: :desc, ordering: :asc))
end

def serialize_comments(*post_scopes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def users_by_age
unknown_age_count: age_stats.unknown_age_count,
series: {
user_counts: age_stats.binned_counts,
expected_user_counts: age_stats.expected_binned_counts,
reference_population: age_stats.population_counts,
bins: age_stats.bins
}
Expand All @@ -31,7 +30,6 @@ def users_by_age_as_xlsx
def users_by_custom_field
json_response = { series: {
users: user_counts,
expected_users: expected_user_counts,
reference_population: reference_population
} }

Expand Down Expand Up @@ -124,23 +122,6 @@ def find_users
UsersFinder.new(users, finder_params).execute
end

def expected_user_counts
@expected_user_counts ||= calculate_expected_user_counts
end

def calculate_expected_user_counts
return if custom_field.key == 'birthyear'
return if (ref_distribution = custom_field.current_ref_distribution).blank?

# user counts for toggled off options are not used to calculate expected user counts
toggled_on_option_keys = ref_distribution.distribution_by_option_key.keys
nb_users_to_redistribute = user_counts.slice(*toggled_on_option_keys).values.sum
expected_counts = ref_distribution.expected_counts(nb_users_to_redistribute)

option_id_to_key = custom_field.options.to_h { |option| [option.id, option.key] }
expected_counts.transform_keys { |option_id| option_id_to_key.fetch(option_id) }
end

def reference_population
@reference_population ||= calculate_reference_population
end
Expand All @@ -161,7 +142,6 @@ def users_by_custom_field_xlsx
}.tap do |cols|
option_ids = cols[:option_id]
cols[:users] = user_counts.fetch_values(*option_ids) { 0 }
cols[:expected_users] = expected_user_counts.fetch_values(*option_ids) { nil } if expected_user_counts.present?
cols[:reference_population] = reference_population.fetch_values(*option_ids) { nil } if reference_population.present?
end
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,5 @@ def self.calculate(users)
def population_counts
reference_distribution&.counts
end

# The expected counts for each bin if the set of users was perfectly representative
# according to the reference distribution.
# @return [Array<Integer>, nil]
def expected_binned_counts
@expected_binned_counts ||= begin
nb_users_with_age = counter_result.binned_counts.sum
reference_distribution&.expected_counts(nb_users_with_age)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ def counts
@counts ||= distribution['counts'].freeze
end

# Returns the expected count distribution for a given (total) number of users.
# @return [Array<Numeric>]
def expected_counts(nb_users)
counts.map { |count| (count.to_f * nb_users / total_population).round(1) }
end

def compute_rscore(users)
user_counts = AgeStats.calculate(users).binned_counts
score_value = RScore.compute_scores(user_counts, counts)[:min_max_p_ratio]
Expand All @@ -74,7 +68,7 @@ def validate_distribution
MSG

errors.add(:distribution, <<~MSG.squish) if bin_boundaries.size != counts.size + 1
bins are not properly defined. The number of bins must match the number of
bins are not properly defined. The number of bins must match the number of
counts.
MSG
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ def probabilities_and_counts
end
end

# Returns the expected count distribution for a given (total) number of users.
def expected_counts(nb_users)
probabilities.transform_values { |probability| (probability * nb_users).round(1) }
end

def distribution_by_option_key
@distribution_by_option_key ||= distribution.transform_keys do |option_id|
option_id_to_key.fetch(option_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def compute_columns(age_stats)
columns = {
'age' => age_ranges_column(age_stats.bins),
'user_count' => age_stats.binned_counts,
'expected_user_count' => age_stats.expected_binned_counts,
'total_population' => age_stats.population_counts
}.compact

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def xlsx_worksheet_to_array(worksheet)
expect(json_response_body.dig(:data, :attributes)).to include(
series: {
users: { female: 2, unspecified: 1, male: 0, _blank: 0 },
expected_users: nil,
reference_population: nil
}
)
Expand All @@ -105,11 +104,6 @@ def xlsx_worksheet_to_array(worksheet)
expect(json_response_body.dig(:data, :attributes)).to include(
series: {
users: { female: 2, unspecified: 1, male: 0, _blank: 0 },
expected_users: {
male: kind_of(Numeric),
female: kind_of(Numeric),
unspecified: kind_of(Numeric)
},
reference_population: ref_distribution.distribution_by_option_key.symbolize_keys
}
)
Expand Down Expand Up @@ -189,7 +183,6 @@ def xlsx_worksheet_to_array(worksheet)
expect(json_response_body.dig(:data, :attributes)).to match({
series: {
users: { '1980': 2, '1976': 1, _blank: 0 },
expected_users: nil,
reference_population: nil
}
})
Expand Down Expand Up @@ -338,7 +331,6 @@ def xlsx_worksheet_to_array(worksheet)
@option3.key => 0,
_blank: 1
},
expected_users: nil,
reference_population: nil
}
}.deep_symbolize_keys)
Expand All @@ -348,11 +340,14 @@ def xlsx_worksheet_to_array(worksheet)
context 'when the custom field has a reference distribution' do
before { create(:categorical_distribution, custom_field: @custom_field) }

example_request 'Users by custom field (select) including expected nb of users' do
example_request 'Users by custom field (select) including reference population' do
expect(response_status).to eq 200
expect(json_response_body.dig(:data, :attributes)).to include(series: hash_including(
expected_users: @custom_field.options.to_h { |option| [option.key.to_sym, kind_of(Numeric)] }
))
reference_population = json_response_body.dig(:data, :attributes, :series, :reference_population)
expect(reference_population).to match({
@option1.key => 450,
@option2.key => 550,
@option3.key => 450
}.deep_symbolize_keys)
end
end
end
Expand Down Expand Up @@ -396,7 +391,6 @@ def xlsx_worksheet_to_array(worksheet)
@option3.key => 0,
_blank: 1
},
expected_users: nil,
reference_population: nil
}
}.deep_symbolize_keys)
Expand Down Expand Up @@ -436,7 +430,6 @@ def xlsx_worksheet_to_array(worksheet)
# rubocop:enable Lint/BooleanSymbol
_blank: 1
},
expected_users: nil,
reference_population: nil
}
})
Expand Down Expand Up @@ -476,7 +469,6 @@ def xlsx_worksheet_to_array(worksheet)
# rubocop:enable Lint/BooleanSymbol
_blank: 0
},
expected_users: nil,
reference_population: nil
}
})
Expand All @@ -497,7 +489,6 @@ def xlsx_worksheet_to_array(worksheet)
# rubocop:enable Lint/BooleanSymbol
_blank: 0
},
expected_users: nil,
reference_population: nil
}
})
Expand Down Expand Up @@ -566,11 +557,11 @@ def xlsx_worksheet_to_array(worksheet)
let(:expected_worksheet_name) { 'users_by_select_field' }
let(:expected_worksheet_values) do
[
%w[option option_id users expected_users reference_population],
['youth council', @option1.key, 1, 0.8, 80],
['youth council', @option2.key, 1, '', ''],
['youth council', @option3.key, 0, 0.2, 20],
['_blank', '_blank', 1, '', '']
%w[option option_id users reference_population],
['youth council', @option1.key, 1, 80],
['youth council', @option2.key, 1, ''],
['youth council', @option3.key, 0, 20],
['_blank', '_blank', 1, '']
]
end
end
Expand Down Expand Up @@ -688,7 +679,6 @@ def xlsx_worksheet_to_array(worksheet)
unknown_age_count: 1,
series: {
user_counts: [0, 2, 2, 1, 1, 1, 0, 0, 0, 0],
expected_user_counts: nil,
reference_population: nil,
bins: UserCustomFields::AgeCounter::DEFAULT_BINS
}
Expand All @@ -714,7 +704,6 @@ def xlsx_worksheet_to_array(worksheet)
unknown_age_count: 1,
series: {
user_counts: [2, 4, 1, 0],
expected_user_counts: ref_distribution.expected_counts(7),
reference_population: ref_distribution.counts,
bins: ref_distribution.bin_boundaries
}
Expand Down Expand Up @@ -763,12 +752,12 @@ def xlsx_worksheet_to_array(worksheet)
let(:expected_worksheet_name) { 'users_by_age' }
let(:expected_worksheet_values) do
[
%w[age user_count expected_user_count total_population],
['0-24', 2, 1.3, 190],
['25-49', 4, 2.0, 279],
['50-74', 1, 2.2, 308],
['75+', 0, 1.5, 213],
['unknown', 1, '', '']
%w[age user_count total_population],
['0-24', 2, 190],
['25-49', 4, 279],
['50-74', 1, 308],
['75+', 0, 213],
['unknown', 1, '']
]
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def event_title
end

def event_time
start_at = I18n.l(event.event_attributes.start_at, format: :short, locale: locale)
end_at = I18n.l(event.event_attributes.end_at, format: :short, locale: locale)
timezone_name = AppConfiguration.instance.settings.dig('core', 'timezone')
timezone = ActiveSupport::TimeZone[timezone_name] || (raise KeyError, timezone_name)

start_at = I18n.l(event.event_attributes.start_at.in_time_zone(timezone), format: :short, locale: locale)
end_at = I18n.l(event.event_attributes.end_at.in_time_zone(timezone), format: :short, locale: locale)

"#{start_at}#{end_at}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ def self.trigger_multiloc_key
end

def generate_commands(recipient:, activity:)
locale = recipient.locale
project = activity.item.project
[{
event_payload: {
project_title_multiloc: project.title_multiloc,
project_ideas_count: project.ideas_count,
project_url: Frontend::UrlService.new.model_to_path(project),
project_url: Frontend::UrlService.new.model_to_url(project, locale: locale),
unfollow_url: Frontend::UrlService.new.unfollow_url(Follower.new(user: recipient))
}
}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe EmailCampaigns::EventRegistrationConfirmationMailer do
describe 'campaign_mail' do
let_it_be(:event) { create(:event) }
let_it_be(:event) { create(:event, start_at: '2017-05-01 18:00', end_at: '2017-05-01 20:00') }
let_it_be(:recipient) { create(:user, locale: 'en') }

let(:event_attributes) { event.attributes }
Expand Down Expand Up @@ -96,10 +96,11 @@
)
end

it 'contains time details' do
it 'contains time details in the platform timezone' do
# Check for presence of:
# <div> Date </div>
# <div style="font-weight: 400;"> 01 May 20:00 – 01 May 22:00 </div>
# Timezone of test platform is CEST = 2 hours ahead of UTC (the date specified in :event)
label_div = page.find('div', exact_text: /\s*Date\s*/)
expect(label_div).to have_css('+ div', text: '01 May 20:00 – 01 May 22:00')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
it 'assigns cta url' do
project_url = command.dig(:event_payload, :project_url)
expect(project_url).to be_present
expect(project_url).to match('http://example.org')
expect(mail.body.encoded).to match(project_url)
end

Expand Down
Loading

0 comments on commit c34d3ba

Please sign in to comment.