Переглянути джерело

Get settings page working.

old-ruby
Ben Kurtovic 9 роки тому
джерело
коміт
0db7ddaf9c
5 змінених файлів з 17 додано та 14 видалено
  1. +7
    -1
      app/controllers/admin_controller.rb
  2. +4
    -4
      app/helpers/admin_helper.rb
  3. +2
    -5
      app/models/admin_setting.rb
  4. +3
    -3
      app/views/admin/index.html.erb
  5. +1
    -1
      db/seeds.rb

+ 7
- 1
app/controllers/admin_controller.rb Переглянути файл

@@ -3,7 +3,13 @@ class AdminController < ApplicationController
end

def update
# respond to form data...
if !params[:settings].nil?
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
render 'index'
end
end

+ 4
- 4
app/helpers/admin_helper.rb Переглянути файл

@@ -1,8 +1,8 @@
module AdminHelper
SETTINGS = [
{:key => :site_name, :label => 'Site name'},
{:key => :corporation, :label => 'Corporation'},
{:key => :description, :label => 'Home page description', :textarea => true},
{:key => :copyright, :label => 'Copyright'},
{:key => 'site_name', :label => 'Site name'},
{:key => 'corporation', :label => 'Corporation'},
{:key => 'description', :label => 'Home page description', :textarea => true},
{:key => 'copyright', :label => 'Copyright'},
]
end

+ 2
- 5
app/models/admin_setting.rb Переглянути файл

@@ -7,10 +7,7 @@ class AdminSetting < ActiveRecord::Base
end

def self.set(key, value)
setting = self.find_by(key: key)
setting.update(value: value)
Rails.cache.write("admin_setting/#{key}") do
value
end
self.find_by(key: key).update(value: value)
Rails.cache.write("admin_setting/#{key}", value)
end
end

+ 3
- 3
app/views/admin/index.html.erb Переглянути файл

@@ -8,11 +8,11 @@
<table>
<% AdminHelper::SETTINGS.each do |s| %>
<tr>
<td><%= label_tag(s[:key], s[:label]) %></td>
<td><%= label_tag("settings[#{s[:key]}]", s[:label]) %></td>
<% if s[:textarea] %>
<td><%= text_area_tag(s[:key], AdminSetting.get(s[:key].to_s), size: "60x5") %></td>
<td><%= text_area_tag("settings[#{s[:key]}]", AdminSetting.get(s[:key]), size: "60x5") %></td>
<% else %>
<td><%= text_field_tag(s[:key], AdminSetting.get(s[:key].to_s), size: 40) %></td>
<td><%= text_field_tag("settings[#{s[:key]}]", AdminSetting.get(s[:key]), size: 40) %></td>
<% end %>
</tr>
<% end %>


+ 1
- 1
db/seeds.rb Переглянути файл

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

Завантаження…
Відмінити
Зберегти