Migrate from ledermann/rails-settings to rails-settings-cached which allows global settings

with YAML-defined defaults. Add admin page for editing global settings. Add "site_description"
setting that would show as a paragraph on the frontpage
This commit is contained in:
Eugen Rochko
2017-01-12 20:46:24 +01:00
parent babc6a1528
commit b11fdc3ae3
20 changed files with 188 additions and 34 deletions

View File

@ -1,3 +1,8 @@
//= require jquery
//= require jquery_ujs
//= require extras
//= require best_in_place
$(function () {
$(".best_in_place").best_in_place();
});

View File

@ -36,6 +36,10 @@
text-decoration: none;
}
}
strong {
font-weight: 500;
}
}
samp {

View File

@ -4,10 +4,10 @@ class AboutController < ApplicationController
before_action :set_body_classes
def index
@description = Setting.site_description
end
def terms
end
def terms; end
private

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
class Admin::SettingsController < ApplicationController
before_action :require_admin!
layout 'admin'
def index
@settings = Setting.all_as_records
end
def update
@setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
if @setting.value != params[:setting][:value]
@setting.value = params[:setting][:value]
@setting.save
end
respond_to do |format|
format.html { redirect_to admin_settings_path }
format.json { respond_with_bip(@setting) }
end
end
end

View File

@ -8,14 +8,18 @@ class Settings::PreferencesController < ApplicationController
def show; end
def update
current_user.settings(:notification_emails).follow = user_params[:notification_emails][:follow] == '1'
current_user.settings(:notification_emails).follow_request = user_params[:notification_emails][:follow_request] == '1'
current_user.settings(:notification_emails).reblog = user_params[:notification_emails][:reblog] == '1'
current_user.settings(:notification_emails).favourite = user_params[:notification_emails][:favourite] == '1'
current_user.settings(:notification_emails).mention = user_params[:notification_emails][:mention] == '1'
current_user.settings['notification_emails'] = {
follow: user_params[:notification_emails][:follow] == '1',
follow_request: user_params[:notification_emails][:follow_request] == '1',
reblog: user_params[:notification_emails][:reblog] == '1',
favourite: user_params[:notification_emails][:favourite] == '1',
mention: user_params[:notification_emails][:mention] == '1',
}
current_user.settings(:interactions).must_be_follower = user_params[:interactions][:must_be_follower] == '1'
current_user.settings(:interactions).must_be_following = user_params[:interactions][:must_be_following] == '1'
current_user.settings['interactions'] = {
must_be_follower: user_params[:interactions][:must_be_follower] == '1',
must_be_following: user_params[:interactions][:must_be_following] == '1',
}
if current_user.update(user_params.except(:notification_emails, :interactions))
redirect_to settings_preferences_path, notice: I18n.t('generic.changes_saved_msg')

View File

@ -14,4 +14,8 @@ module SettingsHelper
def human_locale(locale)
HUMAN_LOCALES[locale]
end
def hash_to_object(hash)
HashObject.new(hash)
end
end

10
app/lib/hash_object.rb Normal file
View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
class HashObject
def initialize(hash)
hash.each do |k, v|
instance_variable_set("@#{k}", v)
self.class.send(:define_method, k, proc { instance_variable_get("@#{k}") })
end
end
end

31
app/models/setting.rb Normal file
View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
class Setting < RailsSettings::Base
source Rails.root.join('config/settings.yml')
namespace Rails.env
def to_param
var
end
class << self
def all_as_records
vars = thing_scoped
records = vars.map { |r| [r.var, r] }.to_h
default_settings.each do |key, default_value|
next if records.key?(key) || default_value.is_a?(Hash)
records[key] = Setting.new(var: key, value: default_value)
end
records
end
private
def default_settings
return {} unless RailsSettings::Default.enabled?
RailsSettings::Default.instance
end
end
end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
class User < ApplicationRecord
include RailsSettings::Extend
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
belongs_to :account, inverse_of: :user
@ -14,11 +16,6 @@ class User < ApplicationRecord
scope :recent, -> { order('id desc') }
scope :admins, -> { where(admin: true) }
has_settings do |s|
s.key :notification_emails, defaults: { follow: false, reblog: false, favourite: false, mention: false, follow_request: true }
s.key :interactions, defaults: { must_be_follower: false, must_be_following: false }
end
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end

View File

@ -37,13 +37,13 @@ class NotifyService < BaseService
end
def blocked?
blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self
blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban
blocked ||= (@recipient.user.settings(:interactions).must_be_follower && !@notification.from_account.following?(@recipient)) # Options
blocked ||= (@recipient.user.settings(:interactions).must_be_following && !@recipient.following?(@notification.from_account)) # Options
blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self
blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban
blocked ||= (@recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient)) # Options
blocked ||= (@recipient.user.settings.interactions['must_be_following'] && !@recipient.following?(@notification.from_account)) # Options
blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
blocked
end
@ -58,6 +58,6 @@ class NotifyService < BaseService
end
def email_enabled?
@recipient.user.settings(:notification_emails).send(@notification.type)
@recipient.user.settings.notification_emails[@notification.type]
end
end

View File

@ -8,7 +8,7 @@
%meta{ property: 'og:site_name', content: 'Mastodon' }/
%meta{ property: 'og:type', content: 'website' }/
%meta{ property: 'og:title', content: Rails.configuration.x.local_domain }/
%meta{ property: 'og:description', content: "Mastodon is a free, open-source social network server. A decentralized alternative to commercial platforms, it avoids the risks of a single company monopolizing your communication. Anyone can run Mastodon and participate in the social network seamlessly" }/
%meta{ property: 'og:description', content: @description.blank? ? "Mastodon is a free, open-source social network server. A decentralized alternative to commercial platforms, it avoids the risks of a single company monopolizing your communication. Anyone can run Mastodon and participate in the social network seamlessly" : strip_tags(@description) }/
%meta{ property: 'og:image', content: asset_url('mastodon_small.jpg') }/
%meta{ property: 'og:image:width', content: '400' }/
%meta{ property: 'og:image:height', content: '400' }/
@ -24,6 +24,9 @@
.screenshot= image_tag 'screenshot.png'
- unless @description.blank?
%p= @description.html_safe
.actions
.info
= link_to t('about.terms'), terms_path

View File

@ -0,0 +1,22 @@
- content_for :page_title do
Site Settings
%table.table
%colgroup
%col{ width: '35%' }/
%thead
%tr
%th Setting
%th Click to edit
%tbody
%tr
%td
%strong Site description
%br/
Displayed as a paragraph on the frontpage and used as a meta tag.
%br/
You can use HTML tags, in particular
%code= '<a>'
and
%code= '<em>'
%td= best_in_place @settings['site_description'], :value, as: :textarea, url: admin_setting_path(@settings['site_description'])

View File

@ -6,14 +6,14 @@
= f.input :locale, collection: I18n.available_locales, wrapper: :with_label, include_blank: false, label_method: lambda { |locale| human_locale(locale) }
= f.simple_fields_for :notification_emails, current_user.settings(:notification_emails) do |ff|
= f.simple_fields_for :notification_emails, hash_to_object(current_user.settings.notification_emails) do |ff|
= ff.input :follow, as: :boolean, wrapper: :with_label
= ff.input :follow_request, as: :boolean, wrapper: :with_label
= ff.input :reblog, as: :boolean, wrapper: :with_label
= ff.input :favourite, as: :boolean, wrapper: :with_label
= ff.input :mention, as: :boolean, wrapper: :with_label
= f.simple_fields_for :interactions, current_user.settings(:interactions) do |ff|
= f.simple_fields_for :interactions, hash_to_object(current_user.settings.interactions) do |ff|
= ff.input :must_be_follower, as: :boolean, wrapper: :with_label
= ff.input :must_be_following, as: :boolean, wrapper: :with_label