Browse Source

Make admin settings more efficient and safer.

old-ruby
Ben Kurtovic 9 years ago
parent
commit
6d5a625ea6
3 changed files with 17 additions and 10 deletions
  1. +1
    -3
      app/controllers/admin_controller.rb
  2. +12
    -3
      app/models/admin_setting.rb
  3. +4
    -4
      db/seeds.rb

+ 1
- 3
app/controllers/admin_controller.rb View File

@@ -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


+ 12
- 3
app/models/admin_setting.rb View File

@@ -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


+ 4
- 4
db/seeds.rb View File

@@ -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')

Loading…
Cancel
Save