-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into aws-eb-test
- Loading branch information
Showing
80 changed files
with
1,815 additions
and
1,058 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,54 @@ | ||
class MailchimpSignupsController < ApplicationController | ||
include NewsletterSubscriber | ||
|
||
skip_before_action :authenticate_user! | ||
|
||
def new | ||
@config = mailchimp_signup_params(params) | ||
@onboarding_complete = params[:onboarding_complete] | ||
@list = mailchimp_api.list_with_interests | ||
audience_manager.list # load to ensure config is set | ||
@email_types = list_of_email_types | ||
@contact = populate_contact_for_form(current_user, params) | ||
rescue => e | ||
flash[:error] = 'Mailchimp API is not configured' | ||
Rails.logger.error "Mailchimp API is not configured - #{e.message}" | ||
Rollbar.error(e) | ||
end | ||
|
||
def index | ||
raise e | ||
end | ||
|
||
def create | ||
list_id = params[:list_id] | ||
@config = mailchimp_signup_params(params) | ||
if @config.valid? | ||
begin | ||
mailchimp_api.subscribe(list_id, @config) | ||
redirect_to mailchimp_signups_path and return | ||
rescue MailchimpApi::Error => e | ||
flash[:error] = e.message | ||
end | ||
if params[:contact_source] | ||
@contact = create_contact_from_user(current_user, sign_up_params) | ||
else | ||
flash[:error] = @config.errors.full_messages.join(', ') | ||
@contact = create_contact(sign_up_params) | ||
end | ||
|
||
@list = mailchimp_api.list_with_interests | ||
resp = subscribe_contact(@contact) | ||
if resp | ||
redirect_to subscribed_mailchimp_signups_path and return | ||
end | ||
@email_types = list_of_email_types | ||
render :new | ||
end | ||
|
||
private | ||
|
||
def mailchimp_api | ||
@mailchimp_api ||= MailchimpApi.new | ||
def populate_contact_for_form(user, params) | ||
if user | ||
contact = Mailchimp::Contact.new(user.email, user.name) | ||
contact.school = user&.school&.name | ||
contact | ||
else | ||
Mailchimp::Contact.new(params[:email_address], nil) | ||
end | ||
end | ||
|
||
def create_contact(sign_up_params) | ||
existing_user = User.find_by_email(sign_up_params[:email_address].downcase) | ||
if existing_user | ||
create_contact_from_user(existing_user, sign_up_params) | ||
else | ||
Mailchimp::Contact.from_params(sign_up_params) | ||
end | ||
end | ||
|
||
def mailchimp_signup_params(params) | ||
MailchimpSignupParams.new( | ||
email_address: params[:email_address], | ||
tags: params[:tags], | ||
interests: params[:interests], | ||
merge_fields: params[:merge_fields] | ||
) | ||
def sign_up_params | ||
params.permit(:email_address, :name, :school, interests: {}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/controllers/school_groups/digital_signage_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module SchoolGroups | ||
class DigitalSignageController < ApplicationController | ||
load_and_authorize_resource :school_group | ||
|
||
def index | ||
end | ||
|
||
def charts | ||
send_data charts_csv, filename: csv_filename_for(:charts) | ||
end | ||
|
||
def equivalences | ||
send_data equivalences_csv, filename: csv_filename_for(:equivalences) | ||
end | ||
|
||
private | ||
|
||
def csv_filename_for(link_type) | ||
title = I18n.t("pupils.digital_signage.index.school_group.links.#{link_type}") | ||
"#{@school_group.name}-#{title}-#{Time.zone.now.strftime('%Y-%m-%d')}".parameterize + '.csv' | ||
end | ||
|
||
def equivalences_csv | ||
CSV.generate do |csv| | ||
csv << [ | ||
t('common.school'), | ||
t('advice_pages.index.priorities.table.columns.fuel_type'), | ||
t('pupils.digital_signage.table.columns.link') | ||
] | ||
schools.each do |school| | ||
if school.has_electricity? | ||
csv << [ | ||
school.name, | ||
t('common.electricity'), | ||
pupils_school_digital_signage_equivalences_url(school, :electricity) | ||
] | ||
end | ||
if school.has_gas? | ||
csv << [ | ||
school.name, | ||
t('common.gas'), | ||
pupils_school_digital_signage_equivalences_url(school, :gas) | ||
] | ||
end | ||
end | ||
end | ||
end | ||
|
||
def charts_csv | ||
CSV.generate do |csv| | ||
csv << [ | ||
t('common.school'), | ||
t('advice_pages.index.priorities.table.columns.fuel_type'), | ||
t('pupils.digital_signage.table.columns.chart_type'), | ||
t('pupils.digital_signage.table.columns.description'), | ||
t('pupils.digital_signage.table.columns.link') | ||
] | ||
schools.each do |school| | ||
if school.has_electricity? | ||
Pupils::DigitalSignageController::CHART_TYPES.each do |chart_type| | ||
csv << [ | ||
school.name, | ||
t('common.electricity'), | ||
t("pupils.digital_signage.index.charts.#{chart_type}.title"), | ||
t("pupils.digital_signage.index.charts.#{chart_type}.description"), | ||
pupils_school_digital_signage_charts_url(school, :electricity, chart_type) | ||
] | ||
end | ||
end | ||
if school.has_gas? | ||
Pupils::DigitalSignageController::CHART_TYPES.each do |chart_type| | ||
csv << [ | ||
school.name, | ||
t('common.gas'), | ||
t("pupils.digital_signage.index.charts.#{chart_type}.title"), | ||
t("pupils.digital_signage.index.charts.#{chart_type}.description"), | ||
pupils_school_digital_signage_charts_url(school, :gas, chart_type) | ||
] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def schools | ||
@school_group.schools.active.data_enabled.where(data_sharing: :public).order(:name) | ||
end | ||
end | ||
end |
Oops, something went wrong.