configvars_rails 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/configvars_rails.gemspec +8 -3
- data/lib/configvars_rails.rb +1 -0
- data/lib/configvars_rails/controller.rb +14 -14
- data/lib/configvars_rails/descriptor.rb +62 -0
- data/lib/configvars_rails/generators/all_generator.rb +5 -2
- data/lib/configvars_rails/generators/templates/config_vars/edit.html.erb +0 -1
- data/lib/configvars_rails/generators/templates/config_vars/index.html.erb +33 -6
- data/lib/configvars_rails/generators/templates/config_vars/new.html.erb +1 -1
- data/lib/configvars_rails/generators/templates/config_vars_controller_test.rb +2 -3
- data/lib/configvars_rails/generators/templates/config_vars_initializer.rb +4 -0
- data/lib/configvars_rails/model.rb +11 -4
- data/test/config_var_test.rb +4 -0
- data/test/config_vars_controller_api_test.rb +22 -10
- data/test/descriptor_test.rb +22 -0
- data/test/helpers/initializers.rb +1 -0
- data/test/test_helper.rb +1 -0
- metadata +10 -5
- data/lib/configvars_rails/generators/templates/config_vars/show.html.erb +0 -15
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/configvars_rails.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{configvars_rails}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Victor Costan"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-09}
|
13
13
|
s.description = %q{This gem provides a model and simple controller for
|
14
14
|
storing global application configuration in a database. This allows the
|
15
15
|
configuration to change without source code modifications.}
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"configvars_rails.gemspec",
|
30
30
|
"lib/configvars_rails.rb",
|
31
31
|
"lib/configvars_rails/controller.rb",
|
32
|
+
"lib/configvars_rails/descriptor.rb",
|
32
33
|
"lib/configvars_rails/engine.rb",
|
33
34
|
"lib/configvars_rails/generators/all_generator.rb",
|
34
35
|
"lib/configvars_rails/generators/templates/001_create_config_vars.rb",
|
@@ -38,14 +39,16 @@ Gem::Specification.new do |s|
|
|
38
39
|
"lib/configvars_rails/generators/templates/config_vars/edit.html.erb",
|
39
40
|
"lib/configvars_rails/generators/templates/config_vars/index.html.erb",
|
40
41
|
"lib/configvars_rails/generators/templates/config_vars/new.html.erb",
|
41
|
-
"lib/configvars_rails/generators/templates/config_vars/show.html.erb",
|
42
42
|
"lib/configvars_rails/generators/templates/config_vars_controller.rb",
|
43
43
|
"lib/configvars_rails/generators/templates/config_vars_controller_test.rb",
|
44
|
+
"lib/configvars_rails/generators/templates/config_vars_initializer.rb",
|
44
45
|
"lib/configvars_rails/model.rb",
|
45
46
|
"test/config_var_test.rb",
|
46
47
|
"test/config_vars_controller_api_test.rb",
|
48
|
+
"test/descriptor_test.rb",
|
47
49
|
"test/helpers/application_controller.rb",
|
48
50
|
"test/helpers/db_setup.rb",
|
51
|
+
"test/helpers/initializers.rb",
|
49
52
|
"test/helpers/routes.rb",
|
50
53
|
"test/helpers/view_helpers.rb",
|
51
54
|
"test/test_helper.rb"
|
@@ -58,7 +61,9 @@ Gem::Specification.new do |s|
|
|
58
61
|
s.test_files = [
|
59
62
|
"test/test_helper.rb",
|
60
63
|
"test/config_vars_controller_api_test.rb",
|
64
|
+
"test/descriptor_test.rb",
|
61
65
|
"test/helpers/application_controller.rb",
|
66
|
+
"test/helpers/initializers.rb",
|
62
67
|
"test/helpers/routes.rb",
|
63
68
|
"test/helpers/db_setup.rb",
|
64
69
|
"test/helpers/view_helpers.rb",
|
data/lib/configvars_rails.rb
CHANGED
@@ -30,27 +30,27 @@ module ControllerInstanceMethods
|
|
30
30
|
# GET /config_vars
|
31
31
|
# GET /config_vars.xml
|
32
32
|
def index
|
33
|
-
@config_vars = ConfigVar.all
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
@config_vars = ConfigVar.order(:name).all
|
34
|
+
|
35
|
+
defined_names = ConfigvarsRails.variable_names
|
36
|
+
default_names = defined_names - @config_vars.map { |var| var.name.to_sym }
|
37
|
+
@default_vars = {}
|
38
|
+
default_names.map(&:to_s).sort.each do |name|
|
39
|
+
@default_vars[name] = ConfigvarsRails.variable_descriptor(name)
|
37
40
|
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# GET /config_vars/1
|
41
|
-
# GET /config_vars/1.xml
|
42
|
-
def show
|
43
|
-
@config_var = ConfigVar.find(params[:id])
|
44
41
|
|
45
42
|
respond_to do |format|
|
46
|
-
format.html #
|
43
|
+
format.html # index.html.erb
|
47
44
|
end
|
48
45
|
end
|
49
46
|
|
50
47
|
# GET /config_vars/new
|
51
48
|
# GET /config_vars/new.xml
|
52
49
|
def new
|
53
|
-
@config_var = ConfigVar.new
|
50
|
+
@config_var = ConfigVar.new :name => params[:name]
|
51
|
+
if params[:name] and descriptor = ConfigvarsRails.variable_descriptor(params[:name])
|
52
|
+
@config_var.value = descriptor.default_value
|
53
|
+
end
|
54
54
|
|
55
55
|
respond_to do |format|
|
56
56
|
format.html # new.html.erb
|
@@ -69,7 +69,7 @@ module ControllerInstanceMethods
|
|
69
69
|
|
70
70
|
respond_to do |format|
|
71
71
|
if @config_var.save!
|
72
|
-
format.html { redirect_to(
|
72
|
+
format.html { redirect_to(config_vars_url, :notice => 'Configuration variable was successfully created.') }
|
73
73
|
else
|
74
74
|
format.html { render :action => :new }
|
75
75
|
end
|
@@ -83,7 +83,7 @@ module ControllerInstanceMethods
|
|
83
83
|
|
84
84
|
respond_to do |format|
|
85
85
|
if @config_var.update_attributes(params[:config_var])
|
86
|
-
format.html { redirect_to(
|
86
|
+
format.html { redirect_to(config_vars_url, :notice => 'Configuration variable was successfully updated.') }
|
87
87
|
else
|
88
88
|
format.html { render :action => :edit }
|
89
89
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# :nodoc: namespace
|
2
|
+
module ConfigvarsRails
|
3
|
+
@vars = {}
|
4
|
+
|
5
|
+
# Configuration variable descriptor.
|
6
|
+
class Descriptor
|
7
|
+
# Creates a new variable descriptor.
|
8
|
+
#
|
9
|
+
# Args:
|
10
|
+
# type:: the type of values the variable can take
|
11
|
+
# default_value:: a Proc or default value for the configuration variable
|
12
|
+
def initialize(value_type, default_value)
|
13
|
+
@value_type = value_type.to_sym
|
14
|
+
@default = default_value.dup
|
15
|
+
end
|
16
|
+
|
17
|
+
# The default value to be used if the variable doesn't have an explicit
|
18
|
+
# value.
|
19
|
+
def default_value
|
20
|
+
if @default.kind_of? Proc
|
21
|
+
@default.call
|
22
|
+
else
|
23
|
+
@default.dup
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# The type of values that the variable takes.
|
28
|
+
attr_reader :value_type
|
29
|
+
end
|
30
|
+
|
31
|
+
# Registers a new configuration variable.
|
32
|
+
def self.add_variable(name, descriptor)
|
33
|
+
@vars[name.to_sym] = descriptor
|
34
|
+
end
|
35
|
+
|
36
|
+
# The descriptor for a configuration variable.
|
37
|
+
def self.variable_descriptor(name)
|
38
|
+
@vars[name.to_sym]
|
39
|
+
end
|
40
|
+
|
41
|
+
# The names for all the configuration variables.
|
42
|
+
def self.variable_names
|
43
|
+
@vars.keys
|
44
|
+
end
|
45
|
+
end # namespace ConfigvarsRails
|
46
|
+
|
47
|
+
|
48
|
+
#
|
49
|
+
module ConfigVars
|
50
|
+
# Declares a configuration variable with string values.
|
51
|
+
#
|
52
|
+
# Args:
|
53
|
+
# name:: the variable's name
|
54
|
+
# default_value:: a string
|
55
|
+
#
|
56
|
+
# Optionally, takes a block that will be called when the variable's default
|
57
|
+
# value is requested.
|
58
|
+
def self.string(name, default_value = nil, &block)
|
59
|
+
descriptor = ConfigvarsRails::Descriptor.new :string, default_value || block
|
60
|
+
ConfigvarsRails.add_variable name, descriptor
|
61
|
+
end
|
62
|
+
end # namespace ConfigVars
|
@@ -21,13 +21,16 @@ class AllGenerator < Rails::Generators::Base
|
|
21
21
|
copy_file File.join('config_vars_controller_test.rb'),
|
22
22
|
File.join('test', 'functional', 'config_vars_controller_test.rb')
|
23
23
|
[
|
24
|
-
'_form.html.erb', 'edit.html.erb', 'index.html.erb', 'new.html.erb'
|
25
|
-
'show.html.erb'
|
24
|
+
'_form.html.erb', 'edit.html.erb', 'index.html.erb', 'new.html.erb'
|
26
25
|
].each do |view_name|
|
27
26
|
copy_file File.join('config_vars', view_name),
|
28
27
|
File.join('app', 'views', 'config_vars', view_name)
|
29
28
|
end
|
30
29
|
route 'resources :config_vars'
|
30
|
+
|
31
|
+
copy_file 'config_vars_initializer.rb',
|
32
|
+
File.join('config', 'initializers', 'config_vars.rb')
|
33
|
+
|
31
34
|
end
|
32
35
|
end # class ConfigvarsRails::ConfigVarsGenerator
|
33
36
|
|
@@ -1,9 +1,11 @@
|
|
1
|
-
<h1>
|
1
|
+
<h1>Set Configuration Variables</h1>
|
2
2
|
|
3
|
-
<table>
|
3
|
+
<table class="configuration_variables">
|
4
4
|
<tr>
|
5
5
|
<th>Name</th>
|
6
|
+
<th>Type</th>
|
6
7
|
<th>Value</th>
|
8
|
+
<th>Default Value</th>
|
7
9
|
<th></th>
|
8
10
|
<th></th>
|
9
11
|
<th></th>
|
@@ -12,14 +14,39 @@
|
|
12
14
|
<% @config_vars.each do |config_var| %>
|
13
15
|
<tr>
|
14
16
|
<td><%= config_var.name %></td>
|
17
|
+
<td>
|
18
|
+
<%= config_var.descriptor ? config_var.descriptor.value_type :
|
19
|
+
'undefined' %>
|
20
|
+
</td>
|
15
21
|
<td><%= config_var.value %></td>
|
16
|
-
<td
|
22
|
+
<td>
|
23
|
+
<%= config_var.descriptor ? config_var.descriptor.default_value :
|
24
|
+
'undefined' %>
|
25
|
+
</td>
|
17
26
|
<td><%= link_to 'Edit', edit_config_var_path(config_var) %></td>
|
18
|
-
<td><%= link_to '
|
27
|
+
<td><%= link_to 'Reset to default', config_var, :method => :delete %></td>
|
19
28
|
</tr>
|
20
29
|
<% end %>
|
21
30
|
</table>
|
22
31
|
|
23
|
-
<
|
32
|
+
<h1>Available Configuration Variables</h1>
|
24
33
|
|
25
|
-
|
34
|
+
<table class="configuration_variables">
|
35
|
+
<tr>
|
36
|
+
<th>Name</th>
|
37
|
+
<th>Type</th>
|
38
|
+
<th>Default Value</th>
|
39
|
+
<th></th>
|
40
|
+
<th></th>
|
41
|
+
<th></th>
|
42
|
+
</tr>
|
43
|
+
|
44
|
+
<% @default_vars.each do |name, descriptor| %>
|
45
|
+
<tr>
|
46
|
+
<td><%= name %></td>
|
47
|
+
<td><%= descriptor.value_type %></td>
|
48
|
+
<td><%= descriptor.default_value %></td>
|
49
|
+
<td><%= link_to 'Edit', new_config_var_path(:name => name) %></td>
|
50
|
+
</tr>
|
51
|
+
<% end %>
|
52
|
+
</table>
|
@@ -5,9 +5,8 @@ class ConfigVarsControllerTest < ActionController::TestCase
|
|
5
5
|
@config_var = config_vars(:app_uri)
|
6
6
|
end
|
7
7
|
|
8
|
-
test "cannot access config
|
9
|
-
get :
|
10
|
-
|
8
|
+
test "cannot access config vars without authentication" do
|
9
|
+
get :index
|
11
10
|
assert_response :unauthorized
|
12
11
|
end
|
13
12
|
end
|
@@ -35,10 +35,13 @@ end # module ConfigvarsRails::Model::ModelClassMethods
|
|
35
35
|
module ModelMetaclassMethods
|
36
36
|
# Access configuration flags by ConfigVar['flag_name'].
|
37
37
|
def [](name)
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
var = where(:name => name).first
|
39
|
+
return var.value if var
|
40
|
+
|
41
|
+
descriptor = ConfigvarsRails.variable_descriptor name
|
42
|
+
return descriptor.default_value if descriptor
|
43
|
+
|
44
|
+
raise IndexError, "Configuration variable #{name} not found"
|
42
45
|
end
|
43
46
|
|
44
47
|
# Set configuration flags by ConfigVar['flag_name'] = 'flag_value'.
|
@@ -54,6 +57,10 @@ end # module ConfigvarsRails::Model::ModelMetaclassMethods
|
|
54
57
|
|
55
58
|
# Included in models that call config_vars_model.
|
56
59
|
module ModelInstanceMethods
|
60
|
+
# The descriptor for this variable, or nil if no descriptor was defined.
|
61
|
+
def descriptor
|
62
|
+
ConfigvarsRails.variable_descriptor name
|
63
|
+
end
|
57
64
|
end # module ConfigvarsRails::Model::ModelInstanceMethods
|
58
65
|
|
59
66
|
ActiveRecord::Base.send :include, ModelMixin
|
data/test/config_var_test.rb
CHANGED
@@ -17,6 +17,10 @@ class ConfigVarTest < ActiveSupport::TestCase
|
|
17
17
|
test "convenience get" do
|
18
18
|
@var.save!
|
19
19
|
assert_equal 'var_value', ConfigVar['var']
|
20
|
+
|
21
|
+
assert_raise IndexError do
|
22
|
+
assert_equal 'failure', ConfigVar['undefined']
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
test "convenience set" do
|
@@ -11,19 +11,36 @@ class ConfigVarsControllerApiTest < ActionController::TestCase
|
|
11
11
|
|
12
12
|
setup do
|
13
13
|
@config_var = config_vars(:app_uri)
|
14
|
-
request.env['HTTP_AUTHORIZATION'] = encode_credentials('config', 'vars')
|
14
|
+
request.env['HTTP_AUTHORIZATION'] = encode_credentials('config', 'vars')
|
15
15
|
end
|
16
16
|
|
17
17
|
test "should get index" do
|
18
18
|
get :index
|
19
19
|
assert_response :success
|
20
|
-
|
20
|
+
|
21
|
+
assert assigns(:config_vars).include?(config_vars(:app_uri)),
|
22
|
+
"@config_vars doesn't have a database fixture"
|
23
|
+
assert assigns(:default_vars).has_key?('simple_var'),
|
24
|
+
"@default_vars doesn't have a simple_var"
|
21
25
|
end
|
22
26
|
|
23
27
|
test "should get new" do
|
24
28
|
get :new
|
25
29
|
assert_response :success
|
26
30
|
end
|
31
|
+
|
32
|
+
test "new with preset name" do
|
33
|
+
get :new, :name => 'undefined'
|
34
|
+
assert_response :success
|
35
|
+
assert_equal 'undefined', assigns(:config_var).name
|
36
|
+
end
|
37
|
+
|
38
|
+
test "new with preset name and default value" do
|
39
|
+
get :new, :name => 'simple_var'
|
40
|
+
assert_response :success
|
41
|
+
assert_equal 'simple_var', assigns(:config_var).name
|
42
|
+
assert_equal 'simple_val', assigns(:config_var).value
|
43
|
+
end
|
27
44
|
|
28
45
|
test "should create config_var" do
|
29
46
|
attributes = @config_var.attributes.merge 'name' => 'other_uri'
|
@@ -31,12 +48,7 @@ class ConfigVarsControllerApiTest < ActionController::TestCase
|
|
31
48
|
post :create, :config_var => attributes
|
32
49
|
end
|
33
50
|
|
34
|
-
assert_redirected_to
|
35
|
-
end
|
36
|
-
|
37
|
-
test "should show config_var" do
|
38
|
-
get :show, :id => @config_var.to_param
|
39
|
-
assert_response :success
|
51
|
+
assert_redirected_to config_vars_url
|
40
52
|
end
|
41
53
|
|
42
54
|
test "should get edit" do
|
@@ -47,7 +59,7 @@ class ConfigVarsControllerApiTest < ActionController::TestCase
|
|
47
59
|
test "should update config_var" do
|
48
60
|
put :update, :id => @config_var.to_param,
|
49
61
|
:config_var => @config_var.attributes
|
50
|
-
assert_redirected_to
|
62
|
+
assert_redirected_to config_vars_url
|
51
63
|
end
|
52
64
|
|
53
65
|
test "should destroy config_var" do
|
@@ -55,7 +67,7 @@ class ConfigVarsControllerApiTest < ActionController::TestCase
|
|
55
67
|
delete :destroy, :id => @config_var.to_param
|
56
68
|
end
|
57
69
|
|
58
|
-
assert_redirected_to
|
70
|
+
assert_redirected_to config_vars_url
|
59
71
|
end
|
60
72
|
|
61
73
|
# Verbatim, from ActiveController's own unit tests.
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class DescriptorTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
@simple_desc = ConfigvarsRails.variable_descriptor 'simple_var'
|
6
|
+
@block_desc = ConfigvarsRails.variable_descriptor 'block_var'
|
7
|
+
end
|
8
|
+
|
9
|
+
test 'default_value' do
|
10
|
+
assert_equal 'simple_val', @simple_desc.default_value
|
11
|
+
assert_in_delta Time.now, @block_desc.default_value, 0.1,
|
12
|
+
'first block call'
|
13
|
+
end
|
14
|
+
|
15
|
+
test 'value_type' do
|
16
|
+
assert_equal :string, @simple_desc.value_type
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'default_value fallback in ConfigVar[]' do
|
20
|
+
assert_equal 'simple_val', ConfigVar['simple_var']
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'configvars_rails/generators/templates/config_vars_initializer.rb'
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configvars_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Victor Costan
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-09 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- configvars_rails.gemspec
|
75
75
|
- lib/configvars_rails.rb
|
76
76
|
- lib/configvars_rails/controller.rb
|
77
|
+
- lib/configvars_rails/descriptor.rb
|
77
78
|
- lib/configvars_rails/engine.rb
|
78
79
|
- lib/configvars_rails/generators/all_generator.rb
|
79
80
|
- lib/configvars_rails/generators/templates/001_create_config_vars.rb
|
@@ -83,14 +84,16 @@ files:
|
|
83
84
|
- lib/configvars_rails/generators/templates/config_vars/edit.html.erb
|
84
85
|
- lib/configvars_rails/generators/templates/config_vars/index.html.erb
|
85
86
|
- lib/configvars_rails/generators/templates/config_vars/new.html.erb
|
86
|
-
- lib/configvars_rails/generators/templates/config_vars/show.html.erb
|
87
87
|
- lib/configvars_rails/generators/templates/config_vars_controller.rb
|
88
88
|
- lib/configvars_rails/generators/templates/config_vars_controller_test.rb
|
89
|
+
- lib/configvars_rails/generators/templates/config_vars_initializer.rb
|
89
90
|
- lib/configvars_rails/model.rb
|
90
91
|
- test/config_var_test.rb
|
91
92
|
- test/config_vars_controller_api_test.rb
|
93
|
+
- test/descriptor_test.rb
|
92
94
|
- test/helpers/application_controller.rb
|
93
95
|
- test/helpers/db_setup.rb
|
96
|
+
- test/helpers/initializers.rb
|
94
97
|
- test/helpers/routes.rb
|
95
98
|
- test/helpers/view_helpers.rb
|
96
99
|
- test/test_helper.rb
|
@@ -131,7 +134,9 @@ summary: Global configuration variables for Rails 3 applications.
|
|
131
134
|
test_files:
|
132
135
|
- test/test_helper.rb
|
133
136
|
- test/config_vars_controller_api_test.rb
|
137
|
+
- test/descriptor_test.rb
|
134
138
|
- test/helpers/application_controller.rb
|
139
|
+
- test/helpers/initializers.rb
|
135
140
|
- test/helpers/routes.rb
|
136
141
|
- test/helpers/db_setup.rb
|
137
142
|
- test/helpers/view_helpers.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
<p id="notice"><%= notice %></p>
|
2
|
-
|
3
|
-
<p>
|
4
|
-
<b>Name:</b>
|
5
|
-
<%= @config_var.name %>
|
6
|
-
</p>
|
7
|
-
|
8
|
-
<p>
|
9
|
-
<b>Value:</b>
|
10
|
-
<%= @config_var.value %>
|
11
|
-
</p>
|
12
|
-
|
13
|
-
|
14
|
-
<%= link_to 'Edit', edit_config_var_path(@config_var) %> |
|
15
|
-
<%= link_to 'Back', config_vars_path %>
|