configvars_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/.gitignore +24 -0
  2. data/.project +12 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +34 -0
  5. data/Rakefile +56 -0
  6. data/VERSION +1 -0
  7. data/app/helpers/config_vars_helper.rb +6 -0
  8. data/configvars_rails.gemspec +84 -0
  9. data/lib/configvars_rails.rb +15 -0
  10. data/lib/configvars_rails/controller.rb +109 -0
  11. data/lib/configvars_rails/engine.rb +25 -0
  12. data/lib/configvars_rails/generators/all_generator.rb +34 -0
  13. data/lib/configvars_rails/generators/templates/001_create_config_vars.rb +13 -0
  14. data/lib/configvars_rails/generators/templates/config_var.rb +6 -0
  15. data/lib/configvars_rails/generators/templates/config_vars.yml +3 -0
  16. data/lib/configvars_rails/generators/templates/config_vars/_form.html.erb +25 -0
  17. data/lib/configvars_rails/generators/templates/config_vars/edit.html.erb +6 -0
  18. data/lib/configvars_rails/generators/templates/config_vars/index.html.erb +25 -0
  19. data/lib/configvars_rails/generators/templates/config_vars/new.html.erb +5 -0
  20. data/lib/configvars_rails/generators/templates/config_vars/show.html.erb +15 -0
  21. data/lib/configvars_rails/generators/templates/config_vars_controller.rb +15 -0
  22. data/lib/configvars_rails/generators/templates/config_vars_controller_test.rb +13 -0
  23. data/lib/configvars_rails/model.rb +63 -0
  24. data/test/config_var_test.rb +27 -0
  25. data/test/config_vars_controller_api_test.rb +65 -0
  26. data/test/helpers/application_controller.rb +5 -0
  27. data/test/helpers/db_setup.rb +23 -0
  28. data/test/helpers/routes.rb +13 -0
  29. data/test/helpers/view_helpers.rb +2 -0
  30. data/test/test_helper.rb +16 -0
  31. metadata +138 -0
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## ECLIPSE
9
+ .loadpath
10
+
11
+ ## EMACS
12
+ *~
13
+ \#*
14
+ .\#*
15
+
16
+ ## VIM
17
+ *.swp
18
+
19
+ ## PROJECT::GENERAL
20
+ coverage
21
+ rdoc
22
+ pkg
23
+
24
+ ## PROJECT::SPECIFIC
data/.project ADDED
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>configvars_rails</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ </buildSpec>
9
+ <natures>
10
+ <nature>org.radrails.rails.core.railsnature</nature>
11
+ </natures>
12
+ </projectDescription>
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Victor Costan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,34 @@
1
+ = configvars_rails
2
+
3
+ Global configuration variables for a Ruby on Rails 3 application.
4
+
5
+ Configuration for Rails application is idiomatically stored in the source code,
6
+ under the config/ directory. This approach has many upsides, such as having the
7
+ configuration go under version control together with the rest of the code.
8
+
9
+ However, it also means that the source code has to be modified to make any
10
+ configuration change. This makes it unlikely that the same code can be used in
11
+ different deployments.
12
+
13
+ == Integration
14
+
15
+ Scaffold the model, controller, and views.
16
+ rails g configvars_rails:all
17
+
18
+ Note: the code inside the models and controllers is tucked away in the plug-in.
19
+ The scaffold model and controller is there as an point. You will be
20
+ able to update the plug-in without regenerating the scaffolds.
21
+
22
+ == Note on Patches/Pull Requests
23
+
24
+ * Fork the project.
25
+ * Make your feature addition or bug fix.
26
+ * Add tests for it. This is important so I don't break it in a
27
+ future version unintentionally.
28
+ * Commit, do not mess with rakefile, version, or history.
29
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
30
+ * Send me a pull request. Bonus points for topic branches.
31
+
32
+ == Copyright
33
+
34
+ Copyright (c) 2010 Victor Costan, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "configvars_rails"
8
+ gem.summary = %Q{Global configuration variables for Rails 3 applications.}
9
+ gem.description = %Q{This gem provides a model and simple controller for
10
+ storing global application configuration in a database. This allows the
11
+ configuration to change without source code modifications.}
12
+ gem.email = "victor@costan.us"
13
+ gem.homepage = "http://github.com/pwnall/configvars_rails"
14
+ gem.authors = ["Victor Costan"]
15
+ gem.add_runtime_dependency "rails", ">= 3.0.0.rc"
16
+ gem.add_development_dependency "sqlite3-ruby", ">= 1.3.0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "configvars_rails #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ module ConfigVarsHelper
2
+ # More syntactic sugar for accessing a configuration variable.
3
+ def config_var(name)
4
+ ConfigVar[name]
5
+ end
6
+ end
@@ -0,0 +1,84 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{configvars_rails}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Victor Costan"]
12
+ s.date = %q{2010-08-08}
13
+ s.description = %q{This gem provides a model and simple controller for
14
+ storing global application configuration in a database. This allows the
15
+ configuration to change without source code modifications.}
16
+ s.email = %q{victor@costan.us}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ ".project",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "app/helpers/config_vars_helper.rb",
29
+ "configvars_rails.gemspec",
30
+ "lib/configvars_rails.rb",
31
+ "lib/configvars_rails/controller.rb",
32
+ "lib/configvars_rails/engine.rb",
33
+ "lib/configvars_rails/generators/all_generator.rb",
34
+ "lib/configvars_rails/generators/templates/001_create_config_vars.rb",
35
+ "lib/configvars_rails/generators/templates/config_var.rb",
36
+ "lib/configvars_rails/generators/templates/config_vars.yml",
37
+ "lib/configvars_rails/generators/templates/config_vars/_form.html.erb",
38
+ "lib/configvars_rails/generators/templates/config_vars/edit.html.erb",
39
+ "lib/configvars_rails/generators/templates/config_vars/index.html.erb",
40
+ "lib/configvars_rails/generators/templates/config_vars/new.html.erb",
41
+ "lib/configvars_rails/generators/templates/config_vars/show.html.erb",
42
+ "lib/configvars_rails/generators/templates/config_vars_controller.rb",
43
+ "lib/configvars_rails/generators/templates/config_vars_controller_test.rb",
44
+ "lib/configvars_rails/model.rb",
45
+ "test/config_var_test.rb",
46
+ "test/config_vars_controller_api_test.rb",
47
+ "test/helpers/application_controller.rb",
48
+ "test/helpers/db_setup.rb",
49
+ "test/helpers/routes.rb",
50
+ "test/helpers/view_helpers.rb",
51
+ "test/test_helper.rb"
52
+ ]
53
+ s.homepage = %q{http://github.com/pwnall/configvars_rails}
54
+ s.rdoc_options = ["--charset=UTF-8"]
55
+ s.require_paths = ["lib"]
56
+ s.rubygems_version = %q{1.3.7}
57
+ s.summary = %q{Global configuration variables for Rails 3 applications.}
58
+ s.test_files = [
59
+ "test/test_helper.rb",
60
+ "test/config_vars_controller_api_test.rb",
61
+ "test/helpers/application_controller.rb",
62
+ "test/helpers/routes.rb",
63
+ "test/helpers/db_setup.rb",
64
+ "test/helpers/view_helpers.rb",
65
+ "test/config_var_test.rb"
66
+ ]
67
+
68
+ if s.respond_to? :specification_version then
69
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
70
+ s.specification_version = 3
71
+
72
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
73
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0.rc"])
74
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.0"])
75
+ else
76
+ s.add_dependency(%q<rails>, [">= 3.0.0.rc"])
77
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.0"])
78
+ end
79
+ else
80
+ s.add_dependency(%q<rails>, [">= 3.0.0.rc"])
81
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.0"])
82
+ end
83
+ end
84
+
@@ -0,0 +1,15 @@
1
+ # :nodoc: namespace
2
+ module ConfigvarsRails
3
+ end
4
+
5
+ require 'configvars_rails/controller.rb'
6
+ require 'configvars_rails/model.rb'
7
+
8
+ if defined?(Rails)
9
+ require 'configvars_rails/engine.rb'
10
+
11
+ # HACK(costan): this works around a known Rails bug
12
+ # https://rails.lighthouseapp.com/projects/8994/tickets/1905-apphelpers-within-plugin-not-being-mixed-in
13
+ require File.expand_path('../../app/helpers/config_vars_helper.rb', __FILE__)
14
+ ActionController::Base.helper ConfigVarsHelper
15
+ end
@@ -0,0 +1,109 @@
1
+ require 'action_controller'
2
+
3
+ # :nodoc: namespace
4
+ module ConfigvarsRails
5
+
6
+ # :nodoc: namespace
7
+ module Session
8
+
9
+ # Mixed into ActiveController::Base
10
+ module ControllerMixin
11
+ def self.included(base)
12
+ base.send :extend, ControllerClassMethods
13
+ end
14
+ end
15
+
16
+ # Methods here become ActiveController::Base class methods.
17
+ module ControllerClassMethods
18
+
19
+ # Turns the current controller into the config vars management controller.
20
+ #
21
+ # Right now, this should be called from ConfigVarsController. The controller
22
+ # name is hardwired in other parts of the implementation.
23
+ def config_vars_controller
24
+ include ControllerInstanceMethods
25
+ end
26
+ end
27
+
28
+ # Included in controllers that call config_vars_controller.
29
+ module ControllerInstanceMethods
30
+ # GET /config_vars
31
+ # GET /config_vars.xml
32
+ def index
33
+ @config_vars = ConfigVar.all
34
+
35
+ respond_to do |format|
36
+ format.html # index.html.erb
37
+ 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
+
45
+ respond_to do |format|
46
+ format.html # show.html.erb
47
+ end
48
+ end
49
+
50
+ # GET /config_vars/new
51
+ # GET /config_vars/new.xml
52
+ def new
53
+ @config_var = ConfigVar.new
54
+
55
+ respond_to do |format|
56
+ format.html # new.html.erb
57
+ end
58
+ end
59
+
60
+ # GET /config_vars/1/edit
61
+ def edit
62
+ @config_var = ConfigVar.find(params[:id])
63
+ end
64
+
65
+ # POST /config_vars
66
+ # POST /config_vars.xml
67
+ def create
68
+ @config_var = ConfigVar.new(params[:config_var])
69
+
70
+ respond_to do |format|
71
+ if @config_var.save!
72
+ format.html { redirect_to(@config_var, :notice => 'Configuration variable was successfully created.') }
73
+ else
74
+ format.html { render :action => :new }
75
+ end
76
+ end
77
+ end
78
+
79
+ # PUT /config_vars/1
80
+ # PUT /config_vars/1.xml
81
+ def update
82
+ @config_var = ConfigVar.find(params[:id])
83
+
84
+ respond_to do |format|
85
+ if @config_var.update_attributes(params[:config_var])
86
+ format.html { redirect_to(@config_var, :notice => 'Config flag was successfully updated.') }
87
+ else
88
+ format.html { render :action => :edit }
89
+ end
90
+ end
91
+ end
92
+
93
+ # DELETE /config_vars/1
94
+ # DELETE /config_vars/1.xml
95
+ def destroy
96
+ @config_var = ConfigVar.find(params[:id])
97
+ @config_var.destroy
98
+
99
+ respond_to do |format|
100
+ format.html { redirect_to(config_vars_url) }
101
+ end
102
+ end
103
+ end
104
+
105
+ ActionController::Base.send :include, ControllerMixin
106
+
107
+ end # namespace ConfigvarsRails::Session
108
+
109
+ end # namespace ConfigvarsRails
@@ -0,0 +1,25 @@
1
+ require 'configvars_rails'
2
+ require 'rails'
3
+
4
+ # :nodoc: namespace
5
+ module ConfigvarsRails
6
+
7
+ class Engine < Rails::Engine
8
+ paths.app = "app"
9
+ paths.app.controllers = "app/controllers"
10
+ paths.app.helpers = "app/helpers"
11
+ paths.app.models = "app/models"
12
+ paths.app.views = "app/views"
13
+ # paths.lib = "lib"
14
+ # paths.lib.tasks = "lib/tasks"
15
+ # paths.config = "config"
16
+ # paths.config.initializers = "config/initializers"
17
+ # paths.config.locales = "config/locales"
18
+ # paths.config.routes = "config/routes.rb"
19
+
20
+ generators do
21
+ require 'configvars_rails/generators/all_generator.rb'
22
+ end
23
+ end # class ConfigvarsRails::Engine
24
+
25
+ end # namespace ConfigvarsRails
@@ -0,0 +1,34 @@
1
+ require 'rails'
2
+
3
+ # :nodoc: namespace
4
+ module ConfigvarsRails
5
+
6
+
7
+ # Name chosen to get configvars_rails:all
8
+ class AllGenerator < Rails::Generators::Base
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def create_config_vars
12
+ copy_file 'config_var.rb',
13
+ File.join('app', 'models', 'config_var.rb')
14
+ copy_file '001_create_config_vars.rb',
15
+ File.join('db', 'migrate', '20100808000001_create_config_vars.rb')
16
+ copy_file 'config_vars.yml',
17
+ File.join('test', 'fixtures', 'config_vars.yml')
18
+
19
+ copy_file 'config_vars_controller.rb',
20
+ File.join('app', 'controllers', 'config_vars_controller.rb')
21
+ copy_file File.join('config_vars_controller_test.rb'),
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
+ 'show.html.erb'
26
+ ].each do |view_name|
27
+ copy_file File.join('config_vars', view_name),
28
+ File.join('app', 'views', 'config_vars', view_name)
29
+ end
30
+ route 'resources :config_vars'
31
+ end
32
+ end # class ConfigvarsRails::ConfigVarsGenerator
33
+
34
+ end # namespace ConfigvarsRails
@@ -0,0 +1,13 @@
1
+ class CreateConfigVars < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :config_vars do |t|
4
+ t.string :name, :length => 64, :null => false
5
+ t.binary :value, :length => 1024, :null => false
6
+ end
7
+ add_index :config_vars, :name, :unique => true, :null => false
8
+ end
9
+
10
+ def self.down
11
+ drop_table :config_vars
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ # Configuration flags that are global to the installation.
2
+ class ConfigVar < ActiveRecord::Base
3
+ config_vars_model
4
+
5
+ # Add your extensions to the ConfigVar class here.
6
+ end
@@ -0,0 +1,3 @@
1
+ app_uri:
2
+ name: app_uri
3
+ value: "http://localhost:3800/"
@@ -0,0 +1,25 @@
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 %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing Configuration Variable</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @config_var %> |
6
+ <%= link_to 'Back', config_vars_path %>
@@ -0,0 +1,25 @@
1
+ <h1>Listing Configuration Variables</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th>Name</th>
6
+ <th>Value</th>
7
+ <th></th>
8
+ <th></th>
9
+ <th></th>
10
+ </tr>
11
+
12
+ <% @config_vars.each do |config_var| %>
13
+ <tr>
14
+ <td><%= config_var.name %></td>
15
+ <td><%= config_var.value %></td>
16
+ <td><%= link_to 'Show', config_var %></td>
17
+ <td><%= link_to 'Edit', edit_config_var_path(config_var) %></td>
18
+ <td><%= link_to 'Destroy', config_var, :confirm => 'Are you sure?', :method => :delete %></td>
19
+ </tr>
20
+ <% end %>
21
+ </table>
22
+
23
+ <br />
24
+
25
+ <%= link_to 'New Configuration Variable', new_config_var_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New Configuration Variable</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', config_vars_path %>
@@ -0,0 +1,15 @@
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 %>
@@ -0,0 +1,15 @@
1
+ # Viewing and changing configuration variables.
2
+ class ConfigVarsController < ApplicationController
3
+ config_vars_controller
4
+
5
+ # Configuration variables are usually sensitive, so let's put some protection
6
+ # around modifying them. At the very least, change the password here.
7
+ USER, PASSWORD = 'config', 'vars'
8
+ before_filter :http_basic_check
9
+ def http_basic_check
10
+ authenticate_or_request_with_http_basic do |user, password|
11
+ user == USER && password == PASSWORD
12
+ end
13
+ end
14
+ private :http_basic_check
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ class ConfigVarsControllerTest < ActionController::TestCase
4
+ setup do
5
+ @config_var = config_vars(:app_uri)
6
+ end
7
+
8
+ test "cannot access config var without authentication" do
9
+ get :show, :id => @config_var.to_param
10
+
11
+ assert_response :unauthorized
12
+ end
13
+ end
@@ -0,0 +1,63 @@
1
+ require 'active_record'
2
+
3
+ # :nodoc: namespace
4
+ module ConfigvarsRails
5
+
6
+ # :nodoc: namespace
7
+ module Model
8
+
9
+
10
+ # Mixed into ActiveRecord::Base
11
+ module ModelMixin
12
+ def self.included(base)
13
+ base.send :extend, ModelClassMethods
14
+ end
15
+ end
16
+
17
+
18
+ # Methods here become ActiveRecord::Base class methods.
19
+ module ModelClassMethods
20
+ # Extends the model to make it hold configuration variables.
21
+ def config_vars_model
22
+ # The name of the configuration variable.
23
+ validates :name, :uniqueness => true, :length => 1..64, :presence => true
24
+
25
+ # The value of the configuration variable.
26
+ validates :value, :length => 1..1024, :presence => true
27
+
28
+ extend ModelMetaclassMethods
29
+ include ModelInstanceMethods
30
+ end
31
+ end # module ConfigvarsRails::Model::ModelClassMethods
32
+
33
+
34
+ # Included in the metaclass of models that call pwnauth_user_model.
35
+ module ModelMetaclassMethods
36
+ # Access configuration flags by ConfigVar['flag_name'].
37
+ def [](name)
38
+ unless flag = where(:name => name).first
39
+ raise IndexError, "Configuration variable #{name} not found"
40
+ end
41
+ flag.value
42
+ end
43
+
44
+ # Set configuration flags by ConfigVar['flag_name'] = 'flag_value'.
45
+ def []=(name, value)
46
+ flag = where(:name => name).first
47
+ flag ||= new :name => name
48
+ flag.value = value
49
+ flag.save!
50
+ value
51
+ end
52
+ end # module ConfigvarsRails::Model::ModelMetaclassMethods
53
+
54
+
55
+ # Included in models that call config_vars_model.
56
+ module ModelInstanceMethods
57
+ end # module ConfigvarsRails::Model::ModelInstanceMethods
58
+
59
+ ActiveRecord::Base.send :include, ModelMixin
60
+
61
+ end # namespace ConfigvarsRails::Model
62
+
63
+ end # namespace ConfigvarsRails
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class ConfigVarTest < ActiveSupport::TestCase
4
+ def setup
5
+ @var = ConfigVar.new :name => 'var', :value => 'var_value'
6
+ end
7
+
8
+ test "setup" do
9
+ assert @var.valid?
10
+ end
11
+
12
+ test "duplicate flag name" do
13
+ @var.name = 'app_uri'
14
+ assert !@var.valid?
15
+ end
16
+
17
+ test "convenience get" do
18
+ @var.save!
19
+ assert_equal 'var_value', ConfigVar['var']
20
+ end
21
+
22
+ test "convenience set" do
23
+ ConfigVar['other_var'] = 'other_value'
24
+ assert_equal 'other_value',
25
+ ConfigVar.where(:name => 'other_var').first.value
26
+ end
27
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ require 'configvars_rails/generators/templates/config_vars_controller.rb'
4
+
5
+ # Run the tests in the generator, to make sure they pass.
6
+ require 'configvars_rails/generators/templates/config_vars_controller_test.rb'
7
+
8
+ # Tests the methods injected by config_vars_controller.
9
+ class ConfigVarsControllerApiTest < ActionController::TestCase
10
+ tests ConfigVarsController
11
+
12
+ setup do
13
+ @config_var = config_vars(:app_uri)
14
+ request.env['HTTP_AUTHORIZATION'] = encode_credentials('config', 'vars')
15
+ end
16
+
17
+ test "should get index" do
18
+ get :index
19
+ assert_response :success
20
+ assert_not_nil assigns(:config_vars)
21
+ end
22
+
23
+ test "should get new" do
24
+ get :new
25
+ assert_response :success
26
+ end
27
+
28
+ test "should create config_var" do
29
+ attributes = @config_var.attributes.merge 'name' => 'other_uri'
30
+ assert_difference('ConfigVar.count') do
31
+ post :create, :config_var => attributes
32
+ end
33
+
34
+ assert_redirected_to config_var_path(assigns(:config_var))
35
+ end
36
+
37
+ test "should show config_var" do
38
+ get :show, :id => @config_var.to_param
39
+ assert_response :success
40
+ end
41
+
42
+ test "should get edit" do
43
+ get :edit, :id => @config_var.to_param
44
+ assert_response :success
45
+ end
46
+
47
+ test "should update config_var" do
48
+ put :update, :id => @config_var.to_param,
49
+ :config_var => @config_var.attributes
50
+ assert_redirected_to config_var_path(assigns(:config_var))
51
+ end
52
+
53
+ test "should destroy config_var" do
54
+ assert_difference('ConfigVar.count', -1) do
55
+ delete :destroy, :id => @config_var.to_param
56
+ end
57
+
58
+ assert_redirected_to config_vars_path
59
+ end
60
+
61
+ # Verbatim, from ActiveController's own unit tests.
62
+ def encode_credentials(username, password)
63
+ "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # :nodoc: stubbed, because controllers inherit from it
2
+ class ApplicationController < ActionController::Base
3
+ prepend_view_path File.expand_path(
4
+ '../../../lib/configvars_rails/generators/templates', __FILE__)
5
+ end
@@ -0,0 +1,23 @@
1
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
2
+ :database => ':memory:'
3
+ ActiveRecord::Base.configurations = true
4
+
5
+ ActiveRecord::Migration.verbose = false
6
+ require 'configvars_rails/generators/templates/001_create_config_vars.rb'
7
+ CreateConfigVars.up
8
+
9
+ require 'configvars_rails/generators/templates/config_var.rb'
10
+
11
+ # :nodoc: open TestCase to setup fixtures
12
+ class ActiveSupport::TestCase
13
+ include ActiveRecord::TestFixtures
14
+
15
+ self.fixture_path =
16
+ File.expand_path '../../../lib/configvars_rails/generators/templates',
17
+ __FILE__
18
+
19
+ self.use_transactional_fixtures = false
20
+ self.use_instantiated_fixtures = false
21
+ self.pre_loaded_fixtures = false
22
+ fixtures :all
23
+ end
@@ -0,0 +1,13 @@
1
+ # :nodoc: the routes used in all tests
2
+ class ActionController::TestCase
3
+ def setup_routes
4
+ @routes = ActionController::Routing::RouteSet.new
5
+ @routes.draw do
6
+ # NOTE: this route should be kept in sync with the config_vars template.
7
+ resources :config_vars
8
+ end
9
+ ApplicationController.send :include, @routes.url_helpers
10
+ end
11
+
12
+ setup :setup_routes
13
+ end
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../../../app/helpers/config_vars_helper.rb', __FILE__)
2
+ ActionController::Base.helper ConfigVarsHelper
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ require 'action_pack'
5
+ require 'active_record'
6
+ require 'active_support'
7
+
8
+ require 'sqlite3'
9
+
10
+ require 'configvars_rails'
11
+
12
+ require 'helpers/view_helpers.rb'
13
+ # NOTE: application_controller has to follow view_helpers
14
+ require 'helpers/application_controller.rb'
15
+ require 'helpers/db_setup.rb'
16
+ require 'helpers/routes.rb'
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configvars_rails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Victor Costan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-08 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7712042
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ - rc
35
+ version: 3.0.0.rc
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3-ruby
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 27
47
+ segments:
48
+ - 1
49
+ - 3
50
+ - 0
51
+ version: 1.3.0
52
+ type: :development
53
+ version_requirements: *id002
54
+ description: |-
55
+ This gem provides a model and simple controller for
56
+ storing global application configuration in a database. This allows the
57
+ configuration to change without source code modifications.
58
+ email: victor@costan.us
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files:
64
+ - LICENSE
65
+ - README.rdoc
66
+ files:
67
+ - .gitignore
68
+ - .project
69
+ - LICENSE
70
+ - README.rdoc
71
+ - Rakefile
72
+ - VERSION
73
+ - app/helpers/config_vars_helper.rb
74
+ - configvars_rails.gemspec
75
+ - lib/configvars_rails.rb
76
+ - lib/configvars_rails/controller.rb
77
+ - lib/configvars_rails/engine.rb
78
+ - lib/configvars_rails/generators/all_generator.rb
79
+ - lib/configvars_rails/generators/templates/001_create_config_vars.rb
80
+ - lib/configvars_rails/generators/templates/config_var.rb
81
+ - lib/configvars_rails/generators/templates/config_vars.yml
82
+ - lib/configvars_rails/generators/templates/config_vars/_form.html.erb
83
+ - lib/configvars_rails/generators/templates/config_vars/edit.html.erb
84
+ - lib/configvars_rails/generators/templates/config_vars/index.html.erb
85
+ - lib/configvars_rails/generators/templates/config_vars/new.html.erb
86
+ - lib/configvars_rails/generators/templates/config_vars/show.html.erb
87
+ - lib/configvars_rails/generators/templates/config_vars_controller.rb
88
+ - lib/configvars_rails/generators/templates/config_vars_controller_test.rb
89
+ - lib/configvars_rails/model.rb
90
+ - test/config_var_test.rb
91
+ - test/config_vars_controller_api_test.rb
92
+ - test/helpers/application_controller.rb
93
+ - test/helpers/db_setup.rb
94
+ - test/helpers/routes.rb
95
+ - test/helpers/view_helpers.rb
96
+ - test/test_helper.rb
97
+ has_rdoc: true
98
+ homepage: http://github.com/pwnall/configvars_rails
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options:
103
+ - --charset=UTF-8
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project:
127
+ rubygems_version: 1.3.7
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Global configuration variables for Rails 3 applications.
131
+ test_files:
132
+ - test/test_helper.rb
133
+ - test/config_vars_controller_api_test.rb
134
+ - test/helpers/application_controller.rb
135
+ - test/helpers/routes.rb
136
+ - test/helpers/db_setup.rb
137
+ - test/helpers/view_helpers.rb
138
+ - test/config_var_test.rb