Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Settings

  • Loading branch information...
commit 48f649bf8ee3bd18923da00e4b96581017535cbd 1 parent 16565b0
@iain authored
View
5 Gemfile
@@ -32,3 +32,8 @@ end
group :test do
gem 'shoulda-matchers'
end
+
+group :development, :production do
+ gem 'hirb', :require => false
+ gem 'wirb', :require => false
+end
View
4 Gemfile.lock
@@ -71,6 +71,7 @@ GEM
haml (3.1.4)
has_scope (0.5.1)
hike (1.2.1)
+ hirb (0.6.0)
i18n (0.6.0)
inherited_resources (1.3.0)
has_scope (~> 0.5.0)
@@ -152,6 +153,7 @@ GEM
uglifier (1.2.2)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ wirb (0.4.1)
PLATFORMS
ruby
@@ -164,6 +166,7 @@ DEPENDENCIES
faraday_middleware
friendly_id
haml
+ hirb
inherited_resources
jquery-rails
nokogiri
@@ -176,3 +179,4 @@ DEPENDENCIES
sqlite3
typhoeus
uglifier
+ wirb
View
13 app/controllers/settings_controller.rb
@@ -0,0 +1,13 @@
+class SettingsController < ApplicationController
+
+ def edit
+ @settings = Settings.new
+ end
+
+ def update
+ @settings = Settings.new
+ @settings.save(params[:settings])
+ redirect_to settings_path, :notice => "Settings successfully updated."
+ end
+
+end
View
32 app/models/setting.rb
@@ -0,0 +1,32 @@
+class Setting < ActiveRecord::Base
+
+ validates_presence_of :group
+
+ validates_presence_of :key
+ validates_uniqueness_of :key, :scope => :group, :case_sensitive => false
+
+ def self.group(group)
+ where(:group => group)
+ end
+
+ def self.key(key)
+ where(:key => key)
+ end
+
+ def self.value
+ pluck(:value).first
+ end
+
+ def self.value_of(group, key)
+ group(group).key(key).value
+ end
+
+ def reader_name
+ "#{group}_#{key}"
+ end
+
+ def writer_name
+ "#{reader_name}="
+ end
+
+end
View
55 app/models/settings.rb
@@ -0,0 +1,55 @@
+class Settings
+
+ include ActiveModel::Conversion
+ include ActiveModel::Validations
+
+ def self.fields
+ Setting.all.map { |setting| "#{setting.group}_#{setting.key}" }
+ end
+
+ def self.new(*)
+ attr_accessor *Setting.all.map(&:reader_name)
+ super
+ end
+
+ def persisted?
+ true
+ end
+
+ def id
+ 1
+ end
+
+ def initialize
+ settings.each do |setting|
+ send(setting.writer_name, setting.value)
+ end
+ end
+
+ def save(attributes)
+ Setting.update attributes.keys, attributes.values
+ end
+
+ def each
+ settings.group_by(&:group).each do |group, settings|
+ yield FormValues.new(group, settings)
+ end
+ end
+
+ private
+
+ def settings
+ @settings ||= Setting.all
+ end
+
+ FormValues = Struct.new(:name, :settings) do
+
+ def each
+ settings.each do |setting|
+ yield "#{name}_#{setting.key}", "settings[#{setting.id}][value]"
+ end
+ end
+
+ end
+
+end
View
13 app/views/settings/edit.html.haml
@@ -0,0 +1,13 @@
+:javascript
+ activateNavigation("settings");
+
+%h1.well Settings
+
+= simple_form_for @settings, :url => settings_path do |f|
+
+ - @settings.each do |group|
+ %legend= Settings.human_attribute_name group.name
+ - group.each do |id, name|
+ = f.input id, :input_html => { :name => name }
+
+ = render "shared/actions", :f => f
View
2  app/views/shared/_navigation.html.haml
@@ -1,6 +1,6 @@
%ul.nav.pull-right
- %li= link_to "Settings", "#"
+ %li.project_nav#project_nav_settings= link_to "Settings", settings_path
%li.vertical-divider
View
14 config/locales/airbrake.yml
@@ -0,0 +1,14 @@
+en:
+ activemodel:
+ attributes:
+ settings:
+ airbrake_api_key: "API Key"
+ airbrake_account_url: "Account URL"
+ simple_form:
+ hints:
+ settings:
+ airbrake_api_key: "Log into Airbrake to get your API key"
+ airbrake_account_url: "Don't forget 'https' is you have SSL enabled in your account."
+ placeholders:
+ settings:
+ airbrake_account_url: "https://your_account.airbrake.io"
View
11 config/locales/settings.yml
@@ -0,0 +1,11 @@
+en:
+ activemodel:
+ attributes:
+ settings:
+ global_site_url: Site URL
+ global: Global Settings
+
+ simple_form:
+ hints:
+ settings:
+ global_site_url: The root URL of this site, needed for daemons to store their data.
View
3  config/routes.rb
@@ -2,6 +2,9 @@
root :to => "projects#index"
+ get "/settings" => "settings#edit"
+ put "/settings" => "settings#update"
+
resources :projects do
resources :iframes, :except => [ :index ]
View
17 db/migrate/20120118185849_create_settings.rb
@@ -0,0 +1,17 @@
+class CreateSettings < ActiveRecord::Migration
+ def up
+ create_table :settings do |t|
+ t.string :key
+ t.string :group
+ t.string :value
+
+ t.timestamps
+ end
+ add_index :settings, :key
+ add_index :settings, :group
+ end
+
+ def down
+ drop_table :settings
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120117184312) do
+ActiveRecord::Schema.define(:version => 20120118185849) do
create_table "airbrake_deploys", :force => true do |t|
t.integer "airbrake_project_id"
@@ -73,4 +73,15 @@
add_index "projects", ["slug"], :name => "index_projects_on_slug"
+ create_table "settings", :force => true do |t|
+ t.string "key"
+ t.string "group"
+ t.string "value"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "settings", ["group"], :name => "index_settings_on_group"
+ add_index "settings", ["key"], :name => "index_settings_on_key"
+
end
View
15 db/seeds.rb
@@ -1,7 +1,8 @@
-# This file should contain all the record creation needed to seed the database with its default values.
-# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
-#
-# Examples:
-#
-# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
-# Mayor.create(name: 'Emanuel', city: cities.first)
+# Setting.delete_all
+
+global = Setting.group("global")
+global.key("site_url").create
+
+airbrake = Setting.group("airbrake")
+airbrake.key("api_key").create
+airbrake.key("account_url").create
View
2  lib/charts/errors.rb
@@ -51,7 +51,7 @@ def self.plotbands(project)
:to => js_timestamp(deploy.deployed_at + 5.hours),
:color => 'rgba(40, 210, 40, 0.5)'
}
- end.tap { |x| Rails.logger.debug x.inspect }
+ end
end
def self.js_timestamp(time)
View
5 spec/controllers/settings_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe SettingsController do
+
+end
View
9 spec/factories/settings.rb
@@ -0,0 +1,9 @@
+# Read about factories at http://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :setting do
+ key "MyString"
+ group "MyString"
+ value "MyString"
+ end
+end
View
17 spec/models/setting_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe Setting do
+
+ describe "#group" do
+ it { should validate_presence_of :group }
+ end
+
+ describe "#key" do
+ it { should validate_presence_of :key }
+ specify do
+ create :setting
+ should validate_uniqueness_of(:key).case_insensitive.scoped_to(:group)
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.