Ver código fonte

Add an allow_non_corp setting; refactor.

old-ruby
Ben Kurtovic 9 anos atrás
pai
commit
73ba7ff836
11 arquivos alterados com 81 adições e 28 exclusões
  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 Ver arquivo

@@ -1,14 +1,18 @@
require 'calefaction/settings'

class AdminController < ApplicationController

def index
end

def update
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
flash.now[:notice] = 'Admin settings updated.'
render 'index'
end
end

+ 16
- 7
app/helpers/admin_helper.rb Ver arquivo

@@ -1,9 +1,18 @@
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

+ 3
- 3
app/helpers/application_helper.rb Ver arquivo

@@ -3,13 +3,13 @@ require 'calefaction/version'
module ApplicationHelper

def get_title(title = '')
base = AdminSetting.get('site_name')
base = AdminSetting.get(:site_name)
title.empty? ? base : "#{title} : #{base}"
end

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",
id: 'corp-logo', title: corp_name, alt: "#{corp_name} Logo")
end


+ 17
- 0
app/models/admin_setting.rb Ver arquivo

@@ -1,6 +1,15 @@
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)
value = self.from_bool(value) if value == true || value == false
self.create(key: key, value: value)
end

@@ -13,10 +22,18 @@ class AdminSetting < ActiveRecord::Base
setting.value
end

def self.get_bool(key)
self.to_bool(self.get(key))
end

def self.set(key, value)
existing = self.get(key)
return if existing.nil? || existing == value
self.find_by(key: key).update(value: value)
Rails.cache.write("admin_setting/#{key}", value)
end

def self.set_bool(key, value)
self.set(key, self.from_bool(value))
end
end

+ 2
- 0
app/models/user.rb Ver arquivo

@@ -2,6 +2,8 @@ require 'eaal'

class User < ActiveRecord::Base
has_secure_password
alias_attribute :admin?, :is_admin
alias_attribute :corp?, :is_corp

def char_names
ensure_api_user


+ 2
- 6
app/views/admin/index.html.erb Ver arquivo

@@ -4,14 +4,10 @@

<%= form_tag do %>
<table>
<% AdminHelper::SETTINGS.each do |s| %>
<% Calefaction::Settings::SETTINGS.each do |s| %>
<tr>
<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>
<% end %>
<tr>


+ 1
- 1
app/views/shared/_footer.html.erb Ver arquivo

@@ -1,6 +1,6 @@
<footer>
<p>
Copyright &copy; <%= copyright_year %> <%= AdminSetting.get('copyright') %>
Copyright &copy; <%= copyright_year %> <%= AdminSetting.get(:copyright) %>
&bull;
Running <a href="//github.com/earwig/calefaction">Calefaction</a> <%= Calefaction::Version::VERSION_STRING %>
&bull;


+ 1
- 1
app/views/shared/_header.html.erb Ver arquivo

@@ -2,7 +2,7 @@
<%= link_to(root_url, id: 'home-link') do %>
<%= corp_logo_tag %>
<span id="corp-name">
<%= AdminSetting.get('corp_name') %>
<%= AdminSetting.get(:corp_name) %>
</span>
<% end %>
<span id="user-links">


+ 1
- 1
app/views/tools/index.html.erb Ver arquivo

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

+ 6
- 5
db/seeds.rb Ver arquivo

@@ -6,9 +6,10 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# 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.')
AdminSetting.add(:allow_non_corp, false)

+ 24
- 0
lib/calefaction/settings.rb Ver arquivo

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

Carregando…
Cancelar
Salvar