From b11fdc3ae3f90731c01149a5a36dc64e065d4ea2 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 12 Jan 2017 20:46:24 +0100 Subject: 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 --- app/models/setting.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 app/models/setting.rb (limited to 'app/models/setting.rb') diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 000000000..0a429a62b --- /dev/null +++ b/app/models/setting.rb @@ -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 -- cgit From 2939e9898b1e0e7da6db802a00e594be4c85499d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 13 Jan 2017 02:42:22 +0100 Subject: Extend rails-settings-cached to merge db-saved hash values with defaults --- app/controllers/api/v1/accounts_controller.rb | 2 +- app/lib/settings/extend.rb | 9 ++++ app/lib/settings/scoped_settings.rb | 12 +++++ app/models/setting.rb | 20 ++++++++ app/models/user.rb | 2 +- db/schema.rb | 69 +-------------------------- 6 files changed, 44 insertions(+), 70 deletions(-) create mode 100644 app/lib/settings/extend.rb create mode 100644 app/lib/settings/scoped_settings.rb (limited to 'app/models/setting.rb') diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 05ff806c5..bd52dfb2c 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -96,7 +96,7 @@ class Api::V1::AccountsController < ApiController limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true') - set_account_counters_maps(@accounts) + set_account_counters_maps(@accounts) unless @accounts.nil? render action: :index end diff --git a/app/lib/settings/extend.rb b/app/lib/settings/extend.rb new file mode 100644 index 000000000..7241a1221 --- /dev/null +++ b/app/lib/settings/extend.rb @@ -0,0 +1,9 @@ +module Settings + module Extend + extend ActiveSupport::Concern + + def settings + ScopedSettings.for_thing(self) + end + end +end \ No newline at end of file diff --git a/app/lib/settings/scoped_settings.rb b/app/lib/settings/scoped_settings.rb new file mode 100644 index 000000000..f8f22a91b --- /dev/null +++ b/app/lib/settings/scoped_settings.rb @@ -0,0 +1,12 @@ +module Settings + class ScopedSettings < ::Setting + def self.for_thing(object) + @object = object + self + end + + def self.thing_scoped + unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id) + end + end +end \ No newline at end of file diff --git a/app/models/setting.rb b/app/models/setting.rb index 0a429a62b..f3c65c054 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -9,6 +9,26 @@ class Setting < RailsSettings::Base end class << self + + def [](key) + return super(key) unless rails_initialized? + + val = Rails.cache.fetch(cache_key(key, @object)) do + db_val = object(key) + + if db_val + default_value = default_settings[key] + + return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash) + db_val.value + else + default_settings[key] + end + end + + val + end + def all_as_records vars = thing_scoped records = vars.map { |r| [r.var, r] }.to_h diff --git a/app/models/user.rb b/app/models/user.rb index bf7d04d7c..71d3ee0b8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class User < ApplicationRecord - include RailsSettings::Extend + include Settings::Extend devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable diff --git a/db/schema.rb b/db/schema.rb index 1cd1258db..f1bd752c9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -169,74 +169,6 @@ ActiveRecord::Schema.define(version: 20170112154826) do t.index ["topic", "callback"], name: "index_pubsubhubbub_subscriptions_on_topic_and_callback", unique: true, using: :btree end - create_table "push_devices", force: :cascade do |t| - t.string "service", default: "", null: false - t.string "token", default: "", null: false - t.integer "account", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["service", "token"], name: "index_push_devices_on_service_and_token", unique: true, using: :btree - end - - create_table "rpush_apps", force: :cascade do |t| - t.string "name", null: false - t.string "environment" - t.text "certificate" - t.string "password" - t.integer "connections", default: 1, null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "type", null: false - t.string "auth_key" - t.string "client_id" - t.string "client_secret" - t.string "access_token" - t.datetime "access_token_expiration" - end - - create_table "rpush_feedback", force: :cascade do |t| - t.string "device_token", limit: 64, null: false - t.datetime "failed_at", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "app_id" - t.index ["device_token"], name: "index_rpush_feedback_on_device_token", using: :btree - end - - create_table "rpush_notifications", force: :cascade do |t| - t.integer "badge" - t.string "device_token", limit: 64 - t.string "sound", default: "default" - t.text "alert" - t.text "data" - t.integer "expiry", default: 86400 - t.boolean "delivered", default: false, null: false - t.datetime "delivered_at" - t.boolean "failed", default: false, null: false - t.datetime "failed_at" - t.integer "error_code" - t.text "error_description" - t.datetime "deliver_after" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "alert_is_json", default: false - t.string "type", null: false - t.string "collapse_key" - t.boolean "delay_while_idle", default: false, null: false - t.text "registration_ids" - t.integer "app_id", null: false - t.integer "retries", default: 0 - t.string "uri" - t.datetime "fail_after" - t.boolean "processing", default: false, null: false - t.integer "priority" - t.text "url_args" - t.string "category" - t.boolean "content_available", default: false - t.text "notification" - t.index ["delivered", "failed"], name: "index_rpush_notifications_multi", where: "((NOT delivered) AND (NOT failed))", using: :btree - end - create_table "settings", force: :cascade do |t| t.string "var", null: false t.text "value" @@ -259,6 +191,7 @@ ActiveRecord::Schema.define(version: 20170112154826) do t.boolean "sensitive", default: false t.integer "visibility", default: 0, null: false t.integer "in_reply_to_account_id" + t.string "conversation_uri" t.index ["account_id"], name: "index_statuses_on_account_id", using: :btree t.index ["in_reply_to_id"], name: "index_statuses_on_in_reply_to_id", using: :btree t.index ["reblog_of_id"], name: "index_statuses_on_reblog_of_id", using: :btree -- cgit From e25170f960bdc890e4f8a6b1373ab55192669629 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 13 Jan 2017 03:24:41 +0100 Subject: Add extended about page stub --- app/controllers/about_controller.rb | 6 ++++++ app/lib/settings/extend.rb | 6 ++++-- app/lib/settings/scoped_settings.rb | 4 +++- app/models/account.rb | 1 + app/models/setting.rb | 5 ++--- app/models/web.rb | 2 ++ app/views/about/more.html.haml | 8 ++++++++ app/views/admin/settings/index.html.haml | 18 +++++++++++++----- config/routes.rb | 7 ++++--- config/settings.yml | 2 +- 10 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 app/views/about/more.html.haml (limited to 'app/models/setting.rb') diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb index 84e5fbbd9..b69c8e790 100644 --- a/app/controllers/about_controller.rb +++ b/app/controllers/about_controller.rb @@ -7,6 +7,12 @@ class AboutController < ApplicationController @description = Setting.site_description end + def more + @extended_description = Setting.site_extended_description + @contact_account = Account.find_local(Setting.site_contact_username) + @contact_email = Setting.site_contact_email + end + def terms; end private diff --git a/app/lib/settings/extend.rb b/app/lib/settings/extend.rb index 7241a1221..407c3480f 100644 --- a/app/lib/settings/extend.rb +++ b/app/lib/settings/extend.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + module Settings module Extend - extend ActiveSupport::Concern + extend ActiveSupport::Concern def settings ScopedSettings.for_thing(self) end end -end \ No newline at end of file +end diff --git a/app/lib/settings/scoped_settings.rb b/app/lib/settings/scoped_settings.rb index f8f22a91b..82b70d128 100644 --- a/app/lib/settings/scoped_settings.rb +++ b/app/lib/settings/scoped_settings.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Settings class ScopedSettings < ::Setting def self.for_thing(object) @@ -9,4 +11,4 @@ module Settings unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id) end end -end \ No newline at end of file +end diff --git a/app/models/account.rb b/app/models/account.rb index ec0e81f7c..5f07615fc 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -159,6 +159,7 @@ class Account < ApplicationRecord end def find_remote!(username, domain) + return if username.blank? where(arel_table[:username].matches(username.gsub(/[%_]/, '\\\\\0'))).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain.gsub(/[%_]/, '\\\\\0'))).take! end diff --git a/app/models/setting.rb b/app/models/setting.rb index f3c65c054..3796253d4 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -9,10 +9,9 @@ class Setting < RailsSettings::Base end class << self - def [](key) return super(key) unless rails_initialized? - + val = Rails.cache.fetch(cache_key(key, @object)) do db_val = object(key) @@ -25,7 +24,7 @@ class Setting < RailsSettings::Base default_settings[key] end end - + val end diff --git a/app/models/web.rb b/app/models/web.rb index 3c6eebbe2..58654fd77 100644 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Web def self.table_name_prefix 'web_' diff --git a/app/views/about/more.html.haml b/app/views/about/more.html.haml new file mode 100644 index 000000000..49a3a0c95 --- /dev/null +++ b/app/views/about/more.html.haml @@ -0,0 +1,8 @@ +- content_for :page_title do + #{Rails.configuration.x.local_domain} + +.wrapper + = @extended_description.html_safe + + - if @contact_account + = render partial: 'authorize_follow/card', locals: { account: @contact_account } \ No newline at end of file diff --git a/app/views/admin/settings/index.html.haml b/app/views/admin/settings/index.html.haml index 4f9ad2fde..5b482213b 100644 --- a/app/views/admin/settings/index.html.haml +++ b/app/views/admin/settings/index.html.haml @@ -9,6 +9,12 @@ %th Setting %th Click to edit %tbody + %tr + %td{ rowspan: 2 } + %strong Contact information + %td= best_in_place @settings['site_contact_username'], :value, url: admin_setting_path(@settings['site_contact_username']), place_holder: 'Enter a username' + %tr + %td= best_in_place @settings['site_contact_email'], :value, url: admin_setting_path(@settings['site_contact_email']), place_holder: 'Enter a public e-mail address' %tr %td %strong Site description @@ -21,8 +27,10 @@ %code= '' %td= best_in_place @settings['site_description'], :value, as: :textarea, url: admin_setting_path(@settings['site_description']) %tr - %td{ rowspan: 2 } - %strong Contact information - %td= best_in_place @settings['site_contact_username'], :value, url: admin_setting_path(@settings['site_contact_username']), place_holder: 'Enter a username' - %tr - %td= best_in_place @settings['site_contact_email'], :value, url: admin_setting_path(@settings['site_contact_email']), place_holder: 'Enter a public e-mail address' + %td + %strong Extended site description + %br/ + Displayed on extended information page + %br/ + You can use HTML tags + %td= best_in_place @settings['site_extended_description'], :value, as: :textarea, url: admin_setting_path(@settings['site_extended_description']) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c0262e933..42de503f0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -143,9 +143,10 @@ Rails.application.routes.draw do get '/web/(*any)', to: 'home#index', as: :web - get :about, to: 'about#index' - get :terms, to: 'about#terms' - + get '/about', to: 'about#index' + get '/about/more', to: 'about#more' + get '/terms', to: 'about#terms' + root 'home#index' match '*unmatched_route', via: :all, to: 'application#raise_not_found' diff --git a/config/settings.yml b/config/settings.yml index 2e309e46e..a78bd067d 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1,6 +1,7 @@ # config/app.yml for rails-settings-cached defaults: &defaults site_description: '' + site_extended_description: '' site_contact_username: '' site_contact_email: '' notification_emails: @@ -12,7 +13,6 @@ defaults: &defaults interactions: must_be_follower: false must_be_following: false - development: <<: *defaults -- cgit