@@ -3,10 +3,8 @@ class AdminController < ApplicationController | |||||
end | end | ||||
def update | def update | ||||
if !params[:settings].nil? | |||||
if params[:settings].is_a? Hash | |||||
params[:settings].each do |key, value| | params[:settings].each do |key, value| | ||||
## assert in list of valid settings... | |||||
## only if changed (i.e. not equal to get) | |||||
AdminSetting.set(key, value) | AdminSetting.set(key, value) | ||||
end | end | ||||
end | end | ||||
@@ -1,12 +1,21 @@ | |||||
class AdminSetting < ActiveRecord::Base | class AdminSetting < ActiveRecord::Base | ||||
def self.add(key, value) | |||||
self.create(key: key, value: value) | |||||
end | |||||
def self.get(key) | def self.get(key) | ||||
Rails.cache.fetch("admin_setting/#{key}") do | |||||
self.find_by(key: key).value | |||||
end | |||||
existing = Rails.cache.read("admin_setting/#{key}") | |||||
return existing unless existing.nil? | |||||
setting = self.find_by(key: key) | |||||
return nil if setting.nil? | |||||
Rails.cache.write("admin_setting/#{key}", setting.value) | |||||
setting.value | |||||
end | end | ||||
def self.set(key, value) | def self.set(key, value) | ||||
existing = self.get(key) | |||||
return if existing.nil? || existing == value | |||||
self.find_by(key: key).update(value: value) | self.find_by(key: key).update(value: value) | ||||
Rails.cache.write("admin_setting/#{key}", value) | Rails.cache.write("admin_setting/#{key}", value) | ||||
end | end | ||||
@@ -6,7 +6,7 @@ | |||||
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) | # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) | ||||
# Mayor.create(name: 'Emanuel', city: cities.first) | # Mayor.create(name: 'Emanuel', city: cities.first) | ||||
AdminSetting.create(key: 'site_name', value: 'Your Corporation Site') | |||||
AdminSetting.create(key: 'corporation', value: 'Your Corporation') | |||||
AdminSetting.create(key: 'description', value: 'Your corporation\'s description goes here!') | |||||
AdminSetting.create(key: 'copyright', value: 'Your Corporation/Your Name') | |||||
AdminSetting.add('site_name', 'Your Corporation Site') | |||||
AdminSetting.add('corporation', 'Your Corporation') | |||||
AdminSetting.add('description', 'Your corporation\'s description goes here!') | |||||
AdminSetting.add('copyright', 'Your Corporation/Your Name') |