Skip to content
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

Use tenant host and name as default email sender #5034

Merged
merged 2 commits into from
Nov 28, 2022
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
19 changes: 15 additions & 4 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,14 @@ def defaults
"twitter_handle": nil,
"twitter_hashtag": nil,
"youtube_handle": nil,
# CONSUL installation's organization name
"org_name": "CONSUL",
"org_name": default_org_name,
"meta_title": nil,
"meta_description": nil,
"meta_keywords": nil,
"proposal_notification_minimum_interval_in_days": 3,
"direct_message_max_per_day": 3,
"mailer_from_name": "CONSUL",
"mailer_from_address": "noreply@consul.dev",
"mailer_from_name": default_org_name,
"mailer_from_address": default_mailer_from_address,
"min_age_to_participate": 16,
"hot_score_period_in_days": 31,
"related_content_score_threshold": -0.3,
Expand Down Expand Up @@ -199,6 +198,18 @@ def defaults
}
end

def default_org_name
Tenant.current&.name || default_main_org_name
end

def default_main_org_name
"CONSUL"
end

def default_mailer_from_address
"noreply@#{Tenant.current_host.presence || "consul.dev"}"
end

def reset_defaults
defaults.each { |name, value| self[name] = value }
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def self.current_schema
Apartment::Tenant.current
end

def self.current
find_by(schema: current_schema)
end

def self.switch(...)
Apartment::Tenant.switch(...)
end
Expand Down
3 changes: 0 additions & 3 deletions db/dev_seeds/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
"feature.featured_proposals": "true",
"feature.map": "true",
"instagram_handle": "CONSUL",
"mailer_from_address": "noreply@consul.dev",
"mailer_from_name": "CONSUL",
"meta_description": "Citizen participation tool for an open, "\
"transparent and democratic government",
"meta_keywords": "citizen participation, open government",
"meta_title": "CONSUL",
"org_name": "CONSUL",
"proposal_code_prefix": "MAD",
"proposal_notification_minimum_interval_in_days": 0,
"telegram_handle": "CONSUL",
Expand Down
36 changes: 36 additions & 0 deletions spec/models/setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,42 @@
end
end

describe ".default_org_name" do
it "returns the main org name for the default tenant" do
expect(Setting.default_org_name).to eq "CONSUL"
end

it "returns the tenant name for other tenants" do
create(:tenant, schema: "new", name: "New Institution")

Tenant.switch("new") do
expect(Setting.default_org_name).to eq "New Institution"
end
end
end

describe ".default_mailer_from_address" do
before { allow(Tenant).to receive(:default_host).and_return("consulproject.org") }

it "uses the default host for the default tenant" do
expect(Setting.default_mailer_from_address).to eq "noreply@consulproject.org"
end

it "uses the tenant host for other tenants" do
allow(Tenant).to receive(:current_schema).and_return("new")

expect(Setting.default_mailer_from_address).to eq "noreply@new.consulproject.org"
end

context "empty default host" do
before { allow(Tenant).to receive(:default_host).and_return("") }

it "uses consul.dev as host" do
expect(Setting.default_mailer_from_address).to eq "noreply@consul.dev"
end
end
end

describe ".add_new_settings" do
context "default settings with strings" do
before do
Expand Down