Browse Source

Get settings page working.

old-ruby
Ben Kurtovic 9 years ago
parent
commit
0db7ddaf9c
5 changed files with 17 additions and 14 deletions
  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 View File

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


def update 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' render 'index'
end end
end end

+ 4
- 4
app/helpers/admin_helper.rb View File

@@ -1,8 +1,8 @@
module AdminHelper module AdminHelper
SETTINGS = [ 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 end

+ 2
- 5
app/models/admin_setting.rb View File

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


def self.set(key, value) 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
end end

+ 3
- 3
app/views/admin/index.html.erb View File

@@ -8,11 +8,11 @@
<table> <table>
<% AdminHelper::SETTINGS.each do |s| %> <% AdminHelper::SETTINGS.each do |s| %>
<tr> <tr>
<td><%= label_tag(s[:key], s[:label]) %></td>
<td><%= label_tag("settings[#{s[:key]}]", s[:label]) %></td>
<% if s[:textarea] %> <% 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 %> <% 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 %> <% end %>
</tr> </tr>
<% end %> <% end %>


+ 1
- 1
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')
AdminSetting.create(key: 'site_name', value: 'Your Corporation Site')
AdminSetting.create(key: 'corporation', value: 'Your Corporation') AdminSetting.create(key: 'corporation', value: 'Your Corporation')
AdminSetting.create(key: 'description', value: 'Your corporation\'s description goes here!') AdminSetting.create(key: 'description', value: 'Your corporation\'s description goes here!')
AdminSetting.create(key: 'copyright', value: 'Your Corporation/Your Name') AdminSetting.create(key: 'copyright', value: 'Your Corporation/Your Name')

Loading…
Cancel
Save