Ver a proveniência

Make admin settings more efficient and safer.

old-ruby
Ben Kurtovic há 9 anos
ascendente
cometimento
6d5a625ea6
3 ficheiros alterados com 17 adições e 10 eliminações
  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 Ver ficheiro

@@ -3,10 +3,8 @@ class AdminController < ApplicationController
end

def update
if !params[:settings].nil?
if params[:settings].is_a? Hash
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)
end
end


+ 12
- 3
app/models/admin_setting.rb Ver ficheiro

@@ -1,12 +1,21 @@
class AdminSetting < ActiveRecord::Base

def self.add(key, value)
self.create(key: key, value: value)
end

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

def self.set(key, value)
existing = self.get(key)
return if existing.nil? || existing == value
self.find_by(key: key).update(value: value)
Rails.cache.write("admin_setting/#{key}", value)
end


+ 4
- 4
db/seeds.rb Ver ficheiro

@@ -6,7 +6,7 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# 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')

Carregando…
Cancelar
Guardar