Skip to content

Commit

Permalink
Make an instance's channels configurable
Browse files Browse the repository at this point in the history
- So that the clients can choose which channels to use.
  • Loading branch information
mattwr18 committed Sep 15, 2023
1 parent c985028 commit 2a294c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
11 changes: 6 additions & 5 deletions app/components/field/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

module Field
class Field < ApplicationComponent
def initialize(object:, attr:, value: nil, **)
def initialize(object:, attr:, value: nil, label: nil, **)
super

@object = object
@attr = attr
@value = value
@label = label
end

def call
c('base_field', {
id: id,
label: label,
label: label_or_default,
help: help,
errors: errors,
styles: styles,
Expand All @@ -34,7 +35,7 @@ def input_defaults

private

attr_reader :object, :attr
attr_reader :object, :attr, :label

def id
"#{model_name}[#{attr}]"
Expand All @@ -48,8 +49,8 @@ def value
@value ||= object.send(attr)
end

def label
I18n.t("#{model_name}.form.#{attr}.label")
def label_or_default
label || I18n.t("#{model_name}.form.#{attr}.label")
end

def help
Expand Down
17 changes: 17 additions & 0 deletions app/components/settings_form/settings_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
<%= c('settings_field', type: :image_input, attr: :onboarding_hero) %>
<%= c('settings_field', type: :input, attr: :onboarding_title) %>
<%= c('settings_field', type: :textarea, attr: :onboarding_page) %>
<%= c 'section', styles: [:noHorizontalPadding] do %>
<%= c 'heading', styles: [:beta] do %>
Channels
<% end %>
<%= c 'flex' do %>
<% Setting.channels.each do |key, value| %>
<%= c('field',
object: Setting.new,
attr: :channels,
styles: [:leftAligned],
label: key.to_s.camelize
) do |field| %>
<%= c 'checkbox', id: "setting[channels][#{key}]", checked: value %>
<% end %>
<% end %>
<% end %>
<% end %>
<% if current_user.admin? %>
<%= c('settings_field', type: :url_input, attr: :onboarding_data_protection_link) %>
<%= c('settings_field', type: :textarea, attr: :onboarding_data_processing_consent_additional_info) %>
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/onboarding_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class OnboardingController < ApplicationController

def index
@jwt = jwt_param
@channels = %w[threema telegram email]
@channels << 'signal' if Setting.signal_server_phone_number.present?
@channels << 'whats_app' if Setting.whats_app_server_phone_number.present?
@channels = Setting.channels.select { |_key, value| value == true }.keys
end

def success; end
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def update
Setting.send("#{key}=", blob)
end

settings_channel_param.each do |key, values_params|
values_hash = values_params.to_h.transform_values { |value| ActiveModel::Type::Boolean.new.cast(value) }
Setting.send("#{key}=", values_hash)
end

flash[:success] = I18n.t('settings.success')
redirect_to settings_url
end
Expand Down Expand Up @@ -52,4 +57,8 @@ def settings_params
:about
)
end

def settings_channel_param
params.require(:setting).permit(channels: {})
end
end
7 changes: 7 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ def self.onboarding_hero=(blob)

field :channel_image
field :about, default: File.read(File.join('config', 'locales', 'about.txt'))
field :channels, type: :hash, default: {
threema: true,
telegram: true,
email: true,
signal: true,
whats_app: true
}
end

0 comments on commit 2a294c6

Please sign in to comment.