@@ -1,14 +1,18 @@ | |||||
require 'calefaction/settings' | |||||
class AdminController < ApplicationController | class AdminController < ApplicationController | ||||
def index | def index | ||||
end | end | ||||
def update | def update | ||||
if params[:settings].is_a? Hash | if params[:settings].is_a? Hash | ||||
params[:settings].each do |key, value| | |||||
AdminSetting.set(key, value) | |||||
end | |||||
Calefaction::Settings.update(params[:settings]) | |||||
flash.now[:notice] = 'Admin settings updated.' | |||||
else | |||||
# TODO: can we make a better error message for this? | |||||
flash.now[:alert] = 'Something is wrong with the settings you submitted.' | |||||
end | end | ||||
flash.now[:notice] = 'Admin settings updated.' | |||||
render 'index' | render 'index' | ||||
end | end | ||||
end | end |
@@ -1,9 +1,18 @@ | |||||
module AdminHelper | module AdminHelper | ||||
SETTINGS = [ | |||||
{:key => 'site_name', :label => 'Site name'}, | |||||
{:key => 'corp_name', :label => 'Corporation name'}, | |||||
{:key => 'corp_id', :label => 'Corporation ID', :size => 10}, | |||||
{:key => 'description', :label => 'Welcome message', :textarea => true}, | |||||
{:key => 'copyright', :label => 'Copyright'}, | |||||
] | |||||
def setting_tag(setting) | |||||
key = "settings[#{setting[:key]}]" | |||||
value = AdminSetting.get(setting[:key]) | |||||
case setting[:type] | |||||
when :string | |||||
text_field_tag(key, value, size: '40') | |||||
when :integer | |||||
text_field_tag(key, value, size: '10') | |||||
when :markup | |||||
text_area_tag(key, value, size: '60x5') | |||||
when :boolean | |||||
check_box_tag(key, '1', value == 't') | |||||
end | |||||
end | |||||
end | end |
@@ -3,13 +3,13 @@ require 'calefaction/version' | |||||
module ApplicationHelper | module ApplicationHelper | ||||
def get_title(title = '') | def get_title(title = '') | ||||
base = AdminSetting.get('site_name') | |||||
base = AdminSetting.get(:site_name) | |||||
title.empty? ? base : "#{title} : #{base}" | title.empty? ? base : "#{title} : #{base}" | ||||
end | end | ||||
def corp_logo_tag | def corp_logo_tag | ||||
corp_id = AdminSetting.get('corp_id') | |||||
corp_name = AdminSetting.get('corp_name') | |||||
corp_id = AdminSetting.get(:corp_id) | |||||
corp_name = AdminSetting.get(:corp_name) | |||||
image_tag("https://image.eveonline.com/Corporation/#{corp_id}_256.png", | image_tag("https://image.eveonline.com/Corporation/#{corp_id}_256.png", | ||||
id: 'corp-logo', title: corp_name, alt: "#{corp_name} Logo") | id: 'corp-logo', title: corp_name, alt: "#{corp_name} Logo") | ||||
end | end | ||||
@@ -1,6 +1,15 @@ | |||||
class AdminSetting < ActiveRecord::Base | class AdminSetting < ActiveRecord::Base | ||||
def self.to_bool(value) | |||||
value == 't' | |||||
end | |||||
def self.from_bool(value) | |||||
value ? 't' : 'f' | |||||
end | |||||
def self.add(key, value) | def self.add(key, value) | ||||
value = self.from_bool(value) if value == true || value == false | |||||
self.create(key: key, value: value) | self.create(key: key, value: value) | ||||
end | end | ||||
@@ -13,10 +22,18 @@ class AdminSetting < ActiveRecord::Base | |||||
setting.value | setting.value | ||||
end | end | ||||
def self.get_bool(key) | |||||
self.to_bool(self.get(key)) | |||||
end | |||||
def self.set(key, value) | def self.set(key, value) | ||||
existing = self.get(key) | existing = self.get(key) | ||||
return if existing.nil? || existing == value | 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 | ||||
def self.set_bool(key, value) | |||||
self.set(key, self.from_bool(value)) | |||||
end | |||||
end | end |
@@ -2,6 +2,8 @@ require 'eaal' | |||||
class User < ActiveRecord::Base | class User < ActiveRecord::Base | ||||
has_secure_password | has_secure_password | ||||
alias_attribute :admin?, :is_admin | |||||
alias_attribute :corp?, :is_corp | |||||
def char_names | def char_names | ||||
ensure_api_user | ensure_api_user | ||||
@@ -4,14 +4,10 @@ | |||||
<%= form_tag do %> | <%= form_tag do %> | ||||
<table> | <table> | ||||
<% AdminHelper::SETTINGS.each do |s| %> | |||||
<% Calefaction::Settings::SETTINGS.each do |s| %> | |||||
<tr> | <tr> | ||||
<td><%= label_tag("settings[#{s[:key]}]", s[:label]) %></td> | <td><%= label_tag("settings[#{s[:key]}]", s[:label]) %></td> | ||||
<% if s[:textarea] %> | |||||
<td><%= text_area_tag("settings[#{s[:key]}]", AdminSetting.get(s[:key]), size: "60x5") %></td> | |||||
<% else %> | |||||
<td><%= text_field_tag("settings[#{s[:key]}]", AdminSetting.get(s[:key]), size: s[:size] || 40) %></td> | |||||
<% end %> | |||||
<td><%= setting_tag s %></td> | |||||
</tr> | </tr> | ||||
<% end %> | <% end %> | ||||
<tr> | <tr> | ||||
@@ -1,6 +1,6 @@ | |||||
<footer> | <footer> | ||||
<p> | <p> | ||||
Copyright © <%= copyright_year %> <%= AdminSetting.get('copyright') %> | |||||
Copyright © <%= copyright_year %> <%= AdminSetting.get(:copyright) %> | |||||
• | • | ||||
Running <a href="//github.com/earwig/calefaction">Calefaction</a> <%= Calefaction::Version::VERSION_STRING %> | Running <a href="//github.com/earwig/calefaction">Calefaction</a> <%= Calefaction::Version::VERSION_STRING %> | ||||
• | • | ||||
@@ -2,7 +2,7 @@ | |||||
<%= link_to(root_url, id: 'home-link') do %> | <%= link_to(root_url, id: 'home-link') do %> | ||||
<%= corp_logo_tag %> | <%= corp_logo_tag %> | ||||
<span id="corp-name"> | <span id="corp-name"> | ||||
<%= AdminSetting.get('corp_name') %> | |||||
<%= AdminSetting.get(:corp_name) %> | |||||
</span> | </span> | ||||
<% end %> | <% end %> | ||||
<span id="user-links"> | <span id="user-links"> | ||||
@@ -1 +1 @@ | |||||
<p><%= AdminSetting.get('description') %></p> | |||||
<p><%= AdminSetting.get(:description) %></p> |
@@ -6,9 +6,10 @@ | |||||
# 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.add('site_name', 'Your Corporation\'s Site') | |||||
AdminSetting.add('corp_name', 'Your Corporation') | |||||
AdminSetting.add('corp_id', '1') | |||||
AdminSetting.add('copyright', 'Your Corporation/Your Name') | |||||
AdminSetting.add('description', | |||||
AdminSetting.add(:site_name, 'Your Corporation\'s Site') | |||||
AdminSetting.add(:corp_name, 'Your Corporation') | |||||
AdminSetting.add(:corp_id, '1') | |||||
AdminSetting.add(:copyright, 'Your Corporation/Your Name') | |||||
AdminSetting.add(:description, | |||||
'Welcome to your corporation\'s website! This message can be changed in the admin settings panel.') | 'Welcome to your corporation\'s website! This message can be changed in the admin settings panel.') | ||||
AdminSetting.add(:allow_non_corp, false) |
@@ -0,0 +1,24 @@ | |||||
module Calefaction::Settings | |||||
extend self | |||||
SETTINGS = [ | |||||
{key: :site_name, type: :string, label: 'Site name'}, | |||||
{key: :corp_name, type: :string, label: 'Corporation name'}, | |||||
{key: :corp_id, type: :integer, label: 'Corporation ID'}, | |||||
{key: :description, type: :markup, label: 'Welcome message'}, | |||||
{key: :copyright, type: :string, label: 'Copyright'}, | |||||
{key: :allow_non_corp, type: :boolean, label: 'Allow non-corp members'} | |||||
] | |||||
def update(params) | |||||
SETTINGS.each do |setting| | |||||
key = setting[:key] | |||||
case setting[:type] | |||||
when :string, :integer, :markup | |||||
AdminSetting.set(key, params[key]) if params.has_key?(key) | |||||
when :boolean | |||||
AdminSetting.set_bool(key, params.has_key?(key)) | |||||
end | |||||
end | |||||
end | |||||
end |