configvars_rails 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{configvars_rails}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.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-10}
12
+ s.date = %q{2010-08-12}
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.}
@@ -35,14 +35,13 @@ Gem::Specification.new do |s|
35
35
  "lib/configvars_rails/generators/templates/001_create_config_vars.rb",
36
36
  "lib/configvars_rails/generators/templates/config_var.rb",
37
37
  "lib/configvars_rails/generators/templates/config_vars.yml",
38
- "lib/configvars_rails/generators/templates/config_vars/_form.html.erb",
39
38
  "lib/configvars_rails/generators/templates/config_vars/edit.html.erb",
40
39
  "lib/configvars_rails/generators/templates/config_vars/index.html.erb",
41
- "lib/configvars_rails/generators/templates/config_vars/new.html.erb",
42
40
  "lib/configvars_rails/generators/templates/config_vars_controller.rb",
43
41
  "lib/configvars_rails/generators/templates/config_vars_controller_test.rb",
44
42
  "lib/configvars_rails/generators/templates/config_vars_initializer.rb",
45
43
  "lib/configvars_rails/model.rb",
44
+ "lib/configvars_rails/routes.rb",
46
45
  "test/config_var_test.rb",
47
46
  "test/config_vars_controller_api_test.rb",
48
47
  "test/descriptor_test.rb",
@@ -51,6 +50,7 @@ Gem::Specification.new do |s|
51
50
  "test/helpers/initializers.rb",
52
51
  "test/helpers/routes.rb",
53
52
  "test/helpers/view_helpers.rb",
53
+ "test/routes_test.rb",
54
54
  "test/test_helper.rb"
55
55
  ]
56
56
  s.homepage = %q{http://github.com/pwnall/configvars_rails}
@@ -59,15 +59,16 @@ Gem::Specification.new do |s|
59
59
  s.rubygems_version = %q{1.3.7}
60
60
  s.summary = %q{Global configuration variables for Rails 3 applications.}
61
61
  s.test_files = [
62
- "test/test_helper.rb",
62
+ "test/config_var_test.rb",
63
63
  "test/config_vars_controller_api_test.rb",
64
64
  "test/descriptor_test.rb",
65
65
  "test/helpers/application_controller.rb",
66
+ "test/helpers/db_setup.rb",
66
67
  "test/helpers/initializers.rb",
67
68
  "test/helpers/routes.rb",
68
- "test/helpers/db_setup.rb",
69
69
  "test/helpers/view_helpers.rb",
70
- "test/config_var_test.rb"
70
+ "test/routes_test.rb",
71
+ "test/test_helper.rb"
71
72
  ]
72
73
 
73
74
  if s.respond_to? :specification_version then
@@ -42,47 +42,43 @@ module ControllerInstanceMethods
42
42
  format.html # index.html.erb
43
43
  end
44
44
  end
45
-
46
- # GET /config_vars/new
47
- # GET /config_vars/new.xml
48
- def new
49
- @config_var = ConfigVar.new :name => params[:name]
50
- if params[:name] and descriptor = ConfigvarsRails.variable_descriptor(params[:name])
51
- @config_var.value = descriptor.default_value
52
- end
53
-
54
- respond_to do |format|
55
- format.html # new.html.erb
56
- end
45
+
46
+ # GET /config_vars/http_user
47
+ def show
48
+ edit
49
+ render :text => @config_var.value
57
50
  end
58
51
 
59
- # GET /config_vars/1/edit
52
+ # GET /config_vars/http_user/edit
60
53
  def edit
61
- @config_var = ConfigVar.find(params[:id])
62
- end
63
-
64
- # POST /config_vars
65
- # POST /config_vars.xml
66
- def create
67
- @config_var = ConfigVar.new(params[:config_var])
68
-
69
- respond_to do |format|
70
- if @config_var.save!
71
- format.html { redirect_to(config_vars_url, :notice => 'Configuration variable was successfully created.') }
72
- else
73
- format.html { render :action => :new }
54
+ @config_var = ConfigVar.where(:name => params[:name]).first
55
+ unless @config_var
56
+ @config_var = ConfigVar.new :name => params[:name]
57
+ if descriptor = ConfigvarsRails.variable_descriptor(params[:name])
58
+ @config_var.value = descriptor.default_value
74
59
  end
75
- end
60
+ end
76
61
  end
77
62
 
78
- # PUT /config_vars/1
79
- # PUT /config_vars/1.xml
63
+ # PUT /config_vars/http_user
80
64
  def update
81
- @config_var = ConfigVar.find(params[:id])
65
+ @config_var = ConfigVar.where(:name => params[:name]).first
66
+ unless @config_var
67
+ @config_var = ConfigVar.new params[:config_var]
68
+ @config_var.name = params[:name]
69
+ end
82
70
 
83
71
  respond_to do |format|
84
- if @config_var.update_attributes(params[:config_var])
85
- format.html { redirect_to(config_vars_url, :notice => 'Configuration variable was successfully updated.') }
72
+ success = if @config_var.new_record?
73
+ @config_var.save
74
+ else
75
+ @config_var.update_attributes(params[:config_var])
76
+ end
77
+ if success
78
+ format.html do
79
+ redirect_to config_vars_url,
80
+ :notice => 'Configuration variable was successfully updated.'
81
+ end
86
82
  else
87
83
  format.html { render :action => :edit }
88
84
  end
@@ -92,7 +88,7 @@ module ControllerInstanceMethods
92
88
  # DELETE /config_vars/1
93
89
  # DELETE /config_vars/1.xml
94
90
  def destroy
95
- @config_var = ConfigVar.find(params[:id])
91
+ @config_var = ConfigVar.where(:name => params[:name]).first
96
92
  @config_var.destroy
97
93
 
98
94
  respond_to do |format|
@@ -20,13 +20,11 @@ class AllGenerator < Rails::Generators::Base
20
20
  File.join('app', 'controllers', 'config_vars_controller.rb')
21
21
  copy_file File.join('config_vars_controller_test.rb'),
22
22
  File.join('test', 'functional', 'config_vars_controller_test.rb')
23
- [
24
- '_form.html.erb', 'edit.html.erb', 'index.html.erb', 'new.html.erb'
25
- ].each do |view_name|
23
+ ['edit.html.erb', 'index.html.erb'].each do |view_name|
26
24
  copy_file File.join('config_vars', view_name),
27
25
  File.join('app', 'views', 'config_vars', view_name)
28
26
  end
29
- route 'resources :config_vars'
27
+ route 'config_vars'
30
28
 
31
29
  copy_file 'config_vars_initializer.rb',
32
30
  File.join('config', 'initializers', 'config_vars.rb')
@@ -1,5 +1,30 @@
1
1
  <h1>Editing Configuration Variable</h1>
2
2
 
3
- <%= render 'form' %>
3
+ <%= form_for(@config_var, :url => config_var_path(@config_var),
4
+ :html => { :method => :put }) do |f| %>
5
+ <% if @config_var.errors.any? %>
6
+ <div id="error_explanation">
7
+ <h2><%= pluralize(@config_var.errors.count, "error") %> prohibited this config_var from being saved:</h2>
8
+
9
+ <ul>
10
+ <% @config_var.errors.full_messages.each do |msg| %>
11
+ <li><%= msg %></li>
12
+ <% end %>
13
+ </ul>
14
+ </div>
15
+ <% end %>
16
+
17
+ <div class="field">
18
+ <%= f.label :name %><br />
19
+ <%= f.text_field :name %>
20
+ </div>
21
+ <div class="field">
22
+ <%= f.label :value %><br />
23
+ <%= f.text_field :value %>
24
+ </div>
25
+ <div class="actions">
26
+ <%= f.submit %>
27
+ </div>
28
+ <% end %>
4
29
 
5
30
  <%= link_to 'Back', config_vars_path %>
@@ -46,7 +46,7 @@
46
46
  <td><%= name %></td>
47
47
  <td><%= descriptor.value_type %></td>
48
48
  <td><%= descriptor.default_value %></td>
49
- <td><%= link_to 'Edit', new_config_var_path(:name => name) %></td>
49
+ <td><%= link_to 'Edit', edit_config_var_path(:name => name) %></td>
50
50
  </tr>
51
51
  <% end %>
52
52
  </table>
@@ -61,6 +61,11 @@ module ModelInstanceMethods
61
61
  def descriptor
62
62
  ConfigvarsRails.variable_descriptor name
63
63
  end
64
+
65
+ # Use name instead of ID on all URLs.
66
+ def to_param
67
+ name
68
+ end
64
69
  end # module ConfigvarsRails::Model::ModelInstanceMethods
65
70
 
66
71
  ActiveRecord::Base.send :include, ModelMixin
@@ -0,0 +1,28 @@
1
+ require 'action_pack'
2
+
3
+ # :nodoc: namespace
4
+ module ConfigvarsRails
5
+
6
+ # :nodoc: namespace
7
+ module Routes
8
+
9
+ # :nodoc: mixed into ActionPack's route mapper.
10
+ module MapperMixin
11
+ def config_vars
12
+ get 'config_vars/:name/edit' => 'config_vars#edit', :as => :edit_config_var,
13
+ :constraints => { :name => /[^\/]+/ }
14
+ get 'config_vars/:name' => 'config_vars#show', :as => :config_var,
15
+ :constraints => { :name => /[^\/]+/ }
16
+ put 'config_vars/:name' => 'config_vars#update',
17
+ :constraints => { :name => /[^\/]+/ }
18
+ delete 'config_vars/:name' => 'config_vars#destroy',
19
+ :constraints => { :name => /[^\/]+/ }
20
+ get 'config_vars' => 'config_vars#index', :as => :config_vars
21
+ end
22
+ end
23
+
24
+ ActionDispatch::Routing::Mapper.send :include, MapperMixin
25
+
26
+ end # namespace ConfigvarsRails::Routes
27
+
28
+ end # namespace ConfigvarsRails
@@ -5,6 +5,7 @@ end
5
5
  require 'configvars_rails/controller.rb'
6
6
  require 'configvars_rails/descriptor.rb'
7
7
  require 'configvars_rails/model.rb'
8
+ require 'configvars_rails/routes.rb'
8
9
 
9
10
  if defined?(Rails)
10
11
  require 'configvars_rails/engine.rb'
@@ -25,19 +25,14 @@ class ConfigVarsControllerApiTest < ActionController::TestCase
25
25
  "@default_vars doesn't have config_vars:http_user"
26
26
  end
27
27
 
28
- test "should get new" do
29
- get :new
28
+ test "should get variable value" do
29
+ get :show, :name => @config_var.to_param
30
30
  assert_response :success
31
+ assert_equal @config_var.value, @response.body
31
32
  end
32
33
 
33
- test "new with preset name" do
34
- get :new, :name => 'undefined'
35
- assert_response :success
36
- assert_equal 'undefined', assigns(:config_var).name
37
- end
38
-
39
- test "new with preset name and default value" do
40
- get :new, :name => 'config_vars.http_user'
34
+ test "edit with preset name and default value" do
35
+ get :edit, :name => 'config_vars.http_user'
41
36
  assert_response :success
42
37
  assert_equal 'config_vars.http_user', assigns(:config_var).name
43
38
  assert_equal 'config', assigns(:config_var).value
@@ -46,26 +41,27 @@ class ConfigVarsControllerApiTest < ActionController::TestCase
46
41
  test "should create config_var" do
47
42
  attributes = @config_var.attributes.merge 'name' => 'other_uri'
48
43
  assert_difference('ConfigVar.count') do
49
- post :create, :config_var => attributes
44
+ put :update, :config_var => attributes, :name => 'other_uri'
50
45
  end
51
46
 
52
47
  assert_redirected_to config_vars_url
48
+ assert_equal ConfigVar['other_uri'], @config_var.value
53
49
  end
54
50
 
55
51
  test "should get edit" do
56
- get :edit, :id => @config_var.to_param
52
+ get :edit, :name => @config_var.to_param
57
53
  assert_response :success
58
54
  end
59
55
 
60
56
  test "should update config_var" do
61
- put :update, :id => @config_var.to_param,
57
+ put :update, :name => @config_var.to_param,
62
58
  :config_var => @config_var.attributes
63
59
  assert_redirected_to config_vars_url
64
60
  end
65
61
 
66
62
  test "should destroy config_var" do
67
63
  assert_difference('ConfigVar.count', -1) do
68
- delete :destroy, :id => @config_var.to_param
64
+ delete :destroy, :name => @config_var.to_param
69
65
  end
70
66
 
71
67
  assert_redirected_to config_vars_url
@@ -4,7 +4,7 @@ class ActionController::TestCase
4
4
  @routes = ActionController::Routing::RouteSet.new
5
5
  @routes.draw do
6
6
  # NOTE: this route should be kept in sync with the config_vars template.
7
- resources :config_vars
7
+ config_vars
8
8
  end
9
9
  ApplicationController.send :include, @routes.url_helpers
10
10
  end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ require 'configvars_rails/generators/templates/config_vars_controller.rb'
4
+
5
+
6
+ class RoutesTest < ActionController::TestCase
7
+ tests ConfigVarsController
8
+
9
+ test "config_vars routes" do
10
+ assert_routing({:path => "/config_vars", :method => :get},
11
+ {:controller => 'config_vars', :action => 'index'})
12
+ assert_routing({:path => "/config_vars/http.user", :method => :get},
13
+ {:controller => 'config_vars', :action => 'show',
14
+ :name => 'http.user'})
15
+ assert_routing({:path => "/config_vars/http.user", :method => :put},
16
+ {:controller => 'config_vars', :action => 'update',
17
+ :name => 'http.user'})
18
+ assert_routing({:path => "/config_vars/http.user", :method => :delete},
19
+ {:controller => 'config_vars', :action => 'destroy',
20
+ :name => 'http.user'})
21
+ assert_routing({:path => "/config_vars/http.user/edit", :method => :get},
22
+ {:controller => 'config_vars', :action => 'edit',
23
+ :name => 'http.user'})
24
+ end
25
+ end
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: 17
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
8
  - 3
10
- version: 0.2.3
9
+ - 0
10
+ version: 0.3.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-10 00:00:00 -04:00
18
+ date: 2010-08-12 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -80,14 +80,13 @@ files:
80
80
  - lib/configvars_rails/generators/templates/001_create_config_vars.rb
81
81
  - lib/configvars_rails/generators/templates/config_var.rb
82
82
  - lib/configvars_rails/generators/templates/config_vars.yml
83
- - lib/configvars_rails/generators/templates/config_vars/_form.html.erb
84
83
  - lib/configvars_rails/generators/templates/config_vars/edit.html.erb
85
84
  - lib/configvars_rails/generators/templates/config_vars/index.html.erb
86
- - lib/configvars_rails/generators/templates/config_vars/new.html.erb
87
85
  - lib/configvars_rails/generators/templates/config_vars_controller.rb
88
86
  - lib/configvars_rails/generators/templates/config_vars_controller_test.rb
89
87
  - lib/configvars_rails/generators/templates/config_vars_initializer.rb
90
88
  - lib/configvars_rails/model.rb
89
+ - lib/configvars_rails/routes.rb
91
90
  - test/config_var_test.rb
92
91
  - test/config_vars_controller_api_test.rb
93
92
  - test/descriptor_test.rb
@@ -96,6 +95,7 @@ files:
96
95
  - test/helpers/initializers.rb
97
96
  - test/helpers/routes.rb
98
97
  - test/helpers/view_helpers.rb
98
+ - test/routes_test.rb
99
99
  - test/test_helper.rb
100
100
  has_rdoc: true
101
101
  homepage: http://github.com/pwnall/configvars_rails
@@ -132,12 +132,13 @@ signing_key:
132
132
  specification_version: 3
133
133
  summary: Global configuration variables for Rails 3 applications.
134
134
  test_files:
135
- - test/test_helper.rb
135
+ - test/config_var_test.rb
136
136
  - test/config_vars_controller_api_test.rb
137
137
  - test/descriptor_test.rb
138
138
  - test/helpers/application_controller.rb
139
+ - test/helpers/db_setup.rb
139
140
  - test/helpers/initializers.rb
140
141
  - test/helpers/routes.rb
141
- - test/helpers/db_setup.rb
142
142
  - test/helpers/view_helpers.rb
143
- - test/config_var_test.rb
143
+ - test/routes_test.rb
144
+ - test/test_helper.rb
@@ -1,25 +0,0 @@
1
- <%= form_for(@config_var) do |f| %>
2
- <% if @config_var.errors.any? %>
3
- <div id="error_explanation">
4
- <h2><%= pluralize(@config_var.errors.count, "error") %> prohibited this config_var from being saved:</h2>
5
-
6
- <ul>
7
- <% @config_var.errors.full_messages.each do |msg| %>
8
- <li><%= msg %></li>
9
- <% end %>
10
- </ul>
11
- </div>
12
- <% end %>
13
-
14
- <div class="field">
15
- <%= f.label :name %><br />
16
- <%= f.text_field :name %>
17
- </div>
18
- <div class="field">
19
- <%= f.label :value %><br />
20
- <%= f.text_field :value %>
21
- </div>
22
- <div class="actions">
23
- <%= f.submit %>
24
- </div>
25
- <% end %>
@@ -1,5 +0,0 @@
1
- <h1>Editing Configuration Variable</h1>
2
-
3
- <%= render 'form' %>
4
-
5
- <%= link_to 'Back', config_vars_path %>