Browse Source

Add an allow_non_corp setting; refactor.

old-ruby
Ben Kurtovic 9 years ago
parent
commit
73ba7ff836
11 changed files with 81 additions and 28 deletions
  1. +8
    -4
      app/controllers/admin_controller.rb
  2. +16
    -7
      app/helpers/admin_helper.rb
  3. +3
    -3
      app/helpers/application_helper.rb
  4. +17
    -0
      app/models/admin_setting.rb
  5. +2
    -0
      app/models/user.rb
  6. +2
    -6
      app/views/admin/index.html.erb
  7. +1
    -1
      app/views/shared/_footer.html.erb
  8. +1
    -1
      app/views/shared/_header.html.erb
  9. +1
    -1
      app/views/tools/index.html.erb
  10. +6
    -5
      db/seeds.rb
  11. +24
    -0
      lib/calefaction/settings.rb

+ 8
- 4
app/controllers/admin_controller.rb View File

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

+ 16
- 7
app/helpers/admin_helper.rb View File

@@ -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
- 3
app/helpers/application_helper.rb View File

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


+ 17
- 0
app/models/admin_setting.rb View File

@@ -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
- 0
app/models/user.rb View File

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


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

@@ -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
- 1
app/views/shared/_footer.html.erb View File

@@ -1,6 +1,6 @@
<footer> <footer>
<p> <p>
Copyright &copy; <%= copyright_year %> <%= AdminSetting.get('copyright') %>
Copyright &copy; <%= copyright_year %> <%= AdminSetting.get(:copyright) %>
&bull; &bull;
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 %>
&bull; &bull;


+ 1
- 1
app/views/shared/_header.html.erb View File

@@ -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
app/views/tools/index.html.erb View File

@@ -1 +1 @@
<p><%= AdminSetting.get('description') %></p>
<p><%= AdminSetting.get(:description) %></p>

+ 6
- 5
db/seeds.rb View File

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

+ 24
- 0
lib/calefaction/settings.rb View File

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

Loading…
Cancel
Save