Skip to content

Commit

Permalink
Moved global settings file to a key-value store and modified contents…
Browse files Browse the repository at this point in the history
… controller and test units accordingly
  • Loading branch information
August committed Apr 23, 2012
1 parent 98acea1 commit 9308f1a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 18 deletions.
6 changes: 3 additions & 3 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def show
# If the object isn't valid (FooBar) or isn't a
# child of Content (Feed) a 400 error is thrown.
def new
#The default content type is defined in as default_upload_type in the settings.
if params[:type].nil?
@content_const = Concerto::Application.config.default_upload_type.camelize.constantize
#The default content type is defined in the Configuration model as default_upload_type
if params[:type].nil? && ConcertoConfig[:default_upload_type] != false
@content_const = ConcertoConfig[:default_upload_type].camelize.constantize
end

#We don't recognize the content type, or
Expand Down
34 changes: 34 additions & 0 deletions app/models/concerto_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#Current configuration keys:
#:public_concerto
#:default_upload_type

class ConcertoConfig < ActiveRecord::Base

TRUE = "t"
FALSE = "f"

validates_presence_of :key
validates_uniqueness_of :key

# Enable hash-like access to table for ease of use
# Returns false if key isn't found
# Example: ConcertoConfig[:public_concerto] => "true"
def self.[](key)
rec = self.find_by_key(key.to_s)
if rec.nil?
return false
end
rec.value
end

# Override self.method_missing to allow
# instance attribute type access to Configuration
# table. This helps with forms.
def self.method_missing(method, *args)
unless method.to_s.include?('find') # skip AR find methods
value = self[method]
return value unless value.nil?
end
super(method, args)
end
end
4 changes: 0 additions & 4 deletions config/initializers/settings.rb

This file was deleted.

10 changes: 10 additions & 0 deletions db/migrate/20120423022536_add_concerto_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddConcertoConfig < ActiveRecord::Migration
def change
create_table :concerto_configs do |t|
t.string :key
t.string :value
t.string :value_type
t.string :value_default
end
end
end
29 changes: 18 additions & 11 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120418142310) do
ActiveRecord::Schema.define(:version => 20120423022536) do

create_table "concerto_configs", :force => true do |t|
t.string "key"
t.string "value"
t.string "value_type"
t.string "value_default"
end

create_table "contents", :force => true do |t|
t.string "name"
Expand All @@ -21,8 +28,8 @@
t.text "data"
t.integer "user_id"
t.integer "kind_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "type"
end

Expand All @@ -31,8 +38,8 @@
t.text "description"
t.integer "parent_id"
t.integer "group_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "is_viewable", :default => true
t.boolean "is_submittable", :default => true
end
Expand All @@ -46,14 +53,14 @@

create_table "groups", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "kinds", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "media", :force => true do |t|
Expand Down Expand Up @@ -107,8 +114,8 @@
t.boolean "moderation_flag"
t.integer "moderator_id"
t.integer "duration"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "moderation_reason"
end

Expand Down
3 changes: 3 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#Associate user with initial group
Membership.create(:user_id => 1, :group_id => 1, :level => 9)

#Put in default configuration parameters
Configuration.create(:key => "default_upload_type", :value => "graphic")

#Create an initial feed
Feed.find_or_create_by_name(:name => "Concerto", :description => "Initial Concerto Feed", :group_id => 1, :is_viewable => 1, :is_submittable => 1)

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/concerto_configs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

upload_type:
key: default_upload_type
value: graphic
9 changes: 9 additions & 0 deletions test/unit/concerto_config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class ConcertoConfigTest < ActiveSupport::TestCase
#Test the fields that should be required
test "Config must return a default upload type of graphic" do
assert_equal(ConcertoConfig[:default_upload_type], "graphic")
end

end

0 comments on commit 9308f1a

Please sign in to comment.