Permalink
Browse files

Better settings

  • Loading branch information...
1 parent 1ac7078 commit 4600799ea451c16fe0b94dc2318de6c7a2cdf2a7 @ariejan ariejan committed Feb 29, 2008
@@ -3,6 +3,12 @@ class Admin::SettingsController < ApplicationController
layout 'admin'
def index
- @settingsgroups = SettingsGroup.find(:all, :order => 'name ASC')
+ # Render index.html.erb
+ end
+
+ def update
+ # Do stuff
+
+ redirect_to :action => :index
end
end
View
@@ -1,14 +1,18 @@
class Setting < ActiveRecord::Base
validates_presence_of :label
- validates_uniqueness_of :label, :scope => :settings_group_id
+ validates_uniqueness_of :label
validates_uniqueness_of :identifier
- belongs_to :settings_group
-
# Story any kind of object in the value field.
# This is nice, but you should also make it editable through admin/settings
serialize :value
+ def self.load(identifier)
+ identifier = identifier.to_s if identifier.is_a?(Symbol)
+
+ find_by_identifier(identifier)
+ end
+
# Return the value for a setting
def self.get(identifier)
identifier = identifier.to_s if identifier.is_a?(Symbol)
@@ -1,5 +0,0 @@
-class SettingsGroup < ActiveRecord::Base
- validates_uniqueness_of :name
-
- has_many :settings
-end
@@ -0,0 +1,9 @@
+<div class="setting textfield" id="setting=<%= setting.id %>">
+ <p>
+ <strong><%= setting.label %></strong><br />
+ <small><%= setting.description %></small>
+ </p>
+ <p>
+ <%= text_field_tag("settings[#{setting.id}]", setting.value.to_s, :size => 40 ) %>
+ </p>
+</div>
@@ -6,15 +6,27 @@
<p>
There are site-wide settings. They will be used through-out your application.
</p>
+ <p>
+ To add new settings, simple create a migration (see migration #006 for an example), and add your
+ settings code here to allow the administrator to edit it.
+ </p>
<% end -%>
-<% @settingsgroups.each do |sg| %>
- <h3><%= sg.name %></h3>
+<% form_tag :action => 'update' do %>
+
+<h3>General</h3>
- <% # TODO: (base_app) Find a good way to manage settings of different types. %>
- <table class="overview">
- <% sg.settings.each do |s| %>
- <%= cell(s.label, s.value) %>
- <% end %>
- </table>
+<%= render :partial => 'textfield', :locals => {:setting => Setting.load(:site_name)} %>
+<%= render :partial => 'textfield', :locals => {:setting => Setting.load(:company_name)} %>
+<%= render :partial => 'textfield', :locals => {:setting => Setting.load(:site_url)} %>
+
+<h3>Support</h3>
+
+<%= render :partial => 'textfield', :locals => {:setting => Setting.load(:support_name)} %>
+<%= render :partial => 'textfield', :locals => {:setting => Setting.load(:support_email)} %>
+
+<div id="submitbutton">
+ <p><%= submit_tag 'Save Settings' %></p>
+</div>
+
<% end %>
@@ -1,10 +1,9 @@
class CreateSettings < ActiveRecord::Migration
def self.up
create_table :settings, :force => true do |t|
- t.belongs_to :settings_group
t.string :label
t.string :identifier
- t.string :description
+ t.text :description
t.text :value
t.timestamps
@@ -1,14 +0,0 @@
-class CreateSettingsGroups < ActiveRecord::Migration
- def self.up
- create_table :settings_groups, :force => true do |t|
- t.string :name
- t.integer :position, :default => 0
- end
-
- SettingsGroup.create(:name => 'General')
- end
-
- def self.down
- drop_table :settings_groups
- end
-end
@@ -0,0 +1,42 @@
+class CreateDefaultSettings < ActiveRecord::Migration
+ def self.up
+ Setting.create(
+ :label => "Site name",
+ :identifier => 'site_name',
+ :description => 'The name of your application. This name will be used to brand your entire application.',
+ :value => "BaseAPP"
+ )
+
+ Setting.create(
+ :label => "Company name",
+ :identifier => 'company_name',
+ :description => "Your company's name. It will be used to identify you as the owner of the application.",
+ :value => "Ariejan.net"
+ )
+
+ Setting.create(
+ :label => "Site URL",
+ :identifier => 'site_url',
+ :description => "The FQDN of your site. Don't include http:// or a trailing slash. If your application uses more than one FQDN, specify the primary one.",
+ :value => "baseapp.com"
+ )
+
+ Setting.create(
+ :label => "Support name",
+ :identifier => 'support_name',
+ :description => 'How do you reference to your support department? This will be used to direct users to your support department.',
+ :value => "Support"
+ )
+
+ Setting.create(
+ :label => "Support email address",
+ :identifier => 'support_email',
+ :description => 'What is your support email address? This address will be shown to users for them to email support requests. ',
+ :value => "support@baseapp.com"
+ )
+ end
+
+ def self.down
+ Setting.delete_all
+ end
+end
@@ -1,51 +0,0 @@
-class CreateDefaultSettings < ActiveRecord::Migration
- def self.up
- @sg = SettingsGroup.find_by_name('General')
-
- Setting.create(
- :label => "Site name",
- :identifier => 'site_name',
- :description => 'The name of your application',
- :value => "BaseAPP",
- :settings_group => @sg
- )
-
- Setting.create(
- :label => "Company name",
- :identifier => 'company_name',
- :description => "Your company's name",
- :value => "Ariejan.net",
- :settings_group => @sg
- )
-
- Setting.create(
- :label => "Site URL",
- :identifier => 'site_url',
- :description => "The FQDN of your site. Don't include http:// or a trailing slash",
- :value => "baseapp.com",
- :settings_group => @sg
- )
-
- @ss = SettingsGroup.create(:name => 'Support')
-
- Setting.create(
- :label => "Support name",
- :identifier => 'support_name',
- :description => 'What is the support department called',
- :value => "BaseAPP Support",
- :settings_group => @ss
- )
-
- Setting.create(
- :label => "Support email address",
- :identifier => 'support_email',
- :description => 'The email address users can use to send support questions to',
- :value => "support@baseapp.com",
- :settings_group => @ss
- )
- end
-
- def self.down
- Setting.delete_all
- end
-end
@@ -408,9 +408,15 @@ table.overview td.label {
text-align: right;
padding: 8px 12px 8px 0;
}
+table.overview td.label small {
+ font-weight: normal;
+}
table.overview td.value {
width: 100%;
}
+table.overview td.value input[type="text"] {
+ width: 250px;
+}
table.index td.actions {
width: auto;
}
@@ -633,4 +639,8 @@ div.page_header {
margin: 2px;
border: 1px solid #eee;
color: #ddd;
+}
+#submitbutton {
+ margin: 0.8em;
+ text-align: right;
}
@@ -1,7 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-# one:
-# column: value
-#
-# two:
-# column: value
@@ -1,8 +0,0 @@
-require File.dirname(__FILE__) + '/../test_helper'
-
-class SettingsGroupTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- def test_truth
- assert true
- end
-end

0 comments on commit 4600799

Please sign in to comment.