with YAML-defined defaults. Add admin page for editing global settings. Add "site_description" setting that would show as a paragraph on the frontpagemaster
parent
babc6a1528
commit
b11fdc3ae3
@ -1,3 +1,8 @@ |
||||
//= require jquery
|
||||
//= require jquery_ujs
|
||||
//= require extras
|
||||
//= require best_in_place
|
||||
|
||||
$(function () { |
||||
$(".best_in_place").best_in_place(); |
||||
}); |
||||
|
@ -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 |
@ -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 |
@ -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 |
@ -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']) |
@ -0,0 +1,23 @@ |
||||
# config/app.yml for rails-settings-cached |
||||
defaults: &defaults |
||||
site_description: '' |
||||
site_contact_username: '' |
||||
site_contact_email: '' |
||||
notification_emails: |
||||
follow: false |
||||
reblog: false |
||||
favourite: false |
||||
mention: false |
||||
follow_request: true |
||||
interactions: |
||||
must_be_follower: false |
||||
must_be_following: false |
||||
|
||||
development: |
||||
<<: *defaults |
||||
|
||||
test: |
||||
<<: *defaults |
||||
|
||||
production: |
||||
<<: *defaults |
@ -0,0 +1,19 @@ |
||||
class MigrateSettings < ActiveRecord::Migration |
||||
def up |
||||
remove_index :settings, [:target_type, :target_id, :var] |
||||
rename_column :settings, :target_id, :thing_id |
||||
rename_column :settings, :target_type, :thing_type |
||||
change_column :settings, :thing_id, :integer, null: true, default: nil |
||||
change_column :settings, :thing_type, :string, null: true, default: nil |
||||
add_index :settings, [:thing_type, :thing_id, :var], unique: true |
||||
end |
||||
|
||||
def down |
||||
remove_index :settings, [:thing_type, :thing_id, :var] |
||||
rename_column :settings, :thing_id, :target_id |
||||
rename_column :settings, :thing_type, :target_type |
||||
change_column :settings, :target_id, :integer, null: false |
||||
change_column :settings, :target_type, :string, null: false, default: '' |
||||
add_index :settings, [:target_type, :target_id, :var], unique: true |
||||
end |
||||
end |
Loading…
Reference in new issue