configurable_engine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'jeweler'
5
+ gem 'rspec'
6
+ gem 'cucumber'
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ builder (3.0.0)
5
+ cucumber (0.10.0)
6
+ builder (>= 2.1.2)
7
+ diff-lcs (~> 1.1.2)
8
+ gherkin (~> 2.3.2)
9
+ json (~> 1.4.6)
10
+ term-ansicolor (~> 1.0.5)
11
+ diff-lcs (1.1.2)
12
+ gherkin (2.3.3)
13
+ json (~> 1.4.6)
14
+ git (1.2.5)
15
+ jeweler (1.5.2)
16
+ bundler (~> 1.0.0)
17
+ git (>= 1.2.5)
18
+ rake
19
+ json (1.4.6)
20
+ rake (0.8.7)
21
+ rspec (2.4.0)
22
+ rspec-core (~> 2.4.0)
23
+ rspec-expectations (~> 2.4.0)
24
+ rspec-mocks (~> 2.4.0)
25
+ rspec-core (2.4.0)
26
+ rspec-expectations (2.4.0)
27
+ diff-lcs (~> 1.1.2)
28
+ rspec-mocks (2.4.0)
29
+ term-ansicolor (1.0.5)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ cucumber
36
+ jeweler
37
+ rake
38
+ rspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Paul Campbell
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.
@@ -0,0 +1,90 @@
1
+ # Configurable #
2
+
3
+ A Rails 3 configuration plugin. An update to [Behavior](http://github.com/paulca/behavior).
4
+
5
+ ## How it works ##
6
+
7
+ Configurable lets you define app-wide configuration variables and values in `config/configurable.yml`. These can then be accessed throughout your app.
8
+
9
+ If you or your app users need to change these variables, Configurable stores new values in the database.
10
+
11
+ ## Installation ##
12
+
13
+ Configurable is available as a Ruby gem. Simply add it to your Rails 3 app's `Gemfile`:
14
+
15
+ gem 'configurable'
16
+
17
+ Then run the `configurable_engine:install` generator:
18
+
19
+ rails generate configurable:install
20
+
21
+ ## Usage ##
22
+
23
+ There are two parts to how behavior works. First of all there is a config file, config/behavior.yml. This file controls the variables that are allowed to be set in the app.
24
+
25
+ For example, if you wanted to have access to a config variable "site_title", put this in behavior.yml:
26
+
27
+ site_title:
28
+ name: Site Title
29
+ default: My Site
30
+
31
+ Now, within your app, you can access `Configurable[:site\_title]` (or `Configurable.site_title` if you prefer).
32
+
33
+ If you want to update the config, create a Configurable record in the database:
34
+
35
+ Configurable.create!(:name => 'site_title', :value => 'My New Site')
36
+
37
+ ## Web Interface ##
38
+
39
+ Using Rails 3's Engines feature, Configurable comes with a web interface that is available to your app straight away at `http://localhost:3000/admin/configurable`.
40
+
41
+ If you want to add a layout, or protect the configurable controller, create `app/controllers/admin/application_controller.rb` which would look something like this:
42
+
43
+ class Admin::ApplicationController < ApplicationController
44
+ before_filter :protect_my_code
45
+ layout 'admin'
46
+ end
47
+
48
+ If you want to control how the fields in the admin interface appear, you can add additional params in your behavior.yml file:
49
+
50
+ site_title:
51
+ name: Name of Your Site # sets the edit label
52
+ default: My Site # sets the default value
53
+ type: string # uses input type="text"
54
+
55
+ site_description:
56
+ name: Describe Your Site # sets the edit label
57
+ default: My Site # sets the default value
58
+ type: text # uses textarea
59
+
60
+ secret:
61
+ name: A Secret Passphrase # sets the edit label
62
+ default: passpass # sets the default value
63
+ type: password # uses input type="password"
64
+
65
+ Value:
66
+ name: A number # sets the edit label
67
+ default: 10 # sets the default value
68
+ type: integer # coerces the value to an integer
69
+
70
+ Price:
71
+ name: A price # sets the edit label
72
+ default: "10.00" # sets the default value
73
+ type: decimal # coerces the value to a decimal
74
+
75
+ ## Running the Tests ##
76
+
77
+ The tests for this rails engine are in the `dummy` directory, which is a dummy Rails app with Configurable loaded in the Gemfile.
78
+
79
+ Within the `dummy` folder, run:
80
+
81
+ rake db:schema:load
82
+ rake db:test:prepare
83
+ bundle exec rspec spec
84
+ bundle exec cucumber features
85
+
86
+ == Copyright
87
+
88
+ Copyright (c) 2011 Paul Campbell. See LICENSE.txt for
89
+ further details.
90
+
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "configurable_engine"
16
+ gem.homepage = "http://github.com/paulca/configurable_engine"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Database-backed configuration for Rails 3, with defaults from config file.}
19
+ gem.description = %Q{Configurable allows you to set up config variables in a config file, specifying default values for all environments. These variables can then be set on a per-app basis using a user facing configuration screen. }
20
+ gem.email = "paul@rslw.com"
21
+ gem.authors = ["Paul Campbell"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'rails', '~>3.0.0'
25
+ gem.files = FileList["[A-Za-z]*", "app/**/*", "config/*"]
26
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ require 'cucumber/rake/task'
42
+ Cucumber::Rake::Task.new(:features)
43
+
44
+ task :default => :spec
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "configurable #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,2 @@
1
+ class Admin::ApplicationController < ::ApplicationController
2
+ end
@@ -0,0 +1,15 @@
1
+ class Admin::ConfigurablesController < Admin::ApplicationController
2
+
3
+ def show
4
+
5
+ end
6
+
7
+ def update
8
+ Configurable.keys.each do |key|
9
+ Configurable.find_or_create_by_name(key).
10
+ update_attribute(:value,params[key])
11
+ end
12
+ redirect_to admin_configurable_path
13
+ end
14
+
15
+ end
@@ -0,0 +1,38 @@
1
+ class Configurable < ActiveRecord::Base
2
+
3
+ def self.defaults
4
+ HashWithIndifferentAccess.new(
5
+ YAML.load_file(
6
+ Rails.root.join('config', 'configurable.yml')
7
+ )
8
+ )
9
+ end
10
+
11
+ def self.keys
12
+ self.defaults.collect { |k,v| k.to_s }
13
+ end
14
+
15
+ def self.[](key)
16
+ value = find_by_name(key).try(:value) || self.defaults[key][:default]
17
+ case self.defaults[key][:type]
18
+ when 'boolean'
19
+ value == true or value == 1 or value == "1" or value == "t"
20
+ when 'decimal'
21
+ BigDecimal.new(value.to_s)
22
+ when 'integer'
23
+ value.to_i
24
+ else
25
+ value
26
+ end
27
+ end
28
+
29
+ def self.method_missing(name, *args)
30
+ name_stripped = name.to_s.gsub('?', '')
31
+ if self.keys.include?(name_stripped)
32
+ self[name_stripped]
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,16 @@
1
+ <h2>Config</h2>
2
+
3
+ <%= form_tag(admin_configurable_path, :method => :put) do -%>
4
+ <%- Configurable.defaults.each do |key, options| -%>
5
+ <%= label_tag key, options[:name] %>
6
+ <%- if options[:type] == 'password' -%>
7
+ <%= text_area_tag key, Configurable.send(key) %>
8
+ <%- elsif options[:type] == 'text' -%>
9
+ <%= password_field_tag key, Configurable.send(key) %>
10
+ <%- else -%>
11
+ <%= text_field_tag key, Configurable.send(key) %>
12
+ <%- end -%>
13
+ <%- end -%>
14
+
15
+ <%= submit_tag 'Save' %>
16
+ <%- end -%>
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ namespace :admin do
3
+ resource :configurable
4
+ end
5
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{configurable_engine}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Campbell"]
12
+ s.date = %q{2011-01-10}
13
+ s.description = %q{Configurable allows you to set up config variables in a config file, specifying default values for all environments. These variables can then be set on a per-app basis using a user facing configuration screen. }
14
+ s.email = %q{paul@rslw.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "app/controllers/admin/application_controller.rb",
27
+ "app/controllers/admin/configurables_controller.rb",
28
+ "app/models/configurable.rb",
29
+ "app/views/admin/configurables/show.html.erb",
30
+ "config/routes.rb",
31
+ "configurable_engine.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/paulca/configurable_engine}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Database-backed configuration for Rails 3, with defaults from config file.}
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<rake>, [">= 0"])
45
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
46
+ s.add_runtime_dependency(%q<rspec>, [">= 0"])
47
+ s.add_runtime_dependency(%q<cucumber>, [">= 0"])
48
+ s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
49
+ else
50
+ s.add_dependency(%q<rake>, [">= 0"])
51
+ s.add_dependency(%q<jeweler>, [">= 0"])
52
+ s.add_dependency(%q<rspec>, [">= 0"])
53
+ s.add_dependency(%q<cucumber>, [">= 0"])
54
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rake>, [">= 0"])
58
+ s.add_dependency(%q<jeweler>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, [">= 0"])
60
+ s.add_dependency(%q<cucumber>, [">= 0"])
61
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
62
+ end
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configurable_engine
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Paul Campbell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-10 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rake
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: jeweler
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: cucumber
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: rails
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 3
81
+ - 0
82
+ - 0
83
+ version: 3.0.0
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: *id005
87
+ description: "Configurable allows you to set up config variables in a config file, specifying default values for all environments. These variables can then be set on a per-app basis using a user facing configuration screen. "
88
+ email: paul@rslw.com
89
+ executables: []
90
+
91
+ extensions: []
92
+
93
+ extra_rdoc_files:
94
+ - LICENSE.txt
95
+ - README.md
96
+ files:
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - VERSION
103
+ - app/controllers/admin/application_controller.rb
104
+ - app/controllers/admin/configurables_controller.rb
105
+ - app/models/configurable.rb
106
+ - app/views/admin/configurables/show.html.erb
107
+ - config/routes.rb
108
+ - configurable_engine.gemspec
109
+ has_rdoc: true
110
+ homepage: http://github.com/paulca/configurable_engine
111
+ licenses:
112
+ - MIT
113
+ post_install_message:
114
+ rdoc_options: []
115
+
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: -271690143338276698
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ requirements: []
136
+
137
+ rubyforge_project:
138
+ rubygems_version: 1.3.7
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Database-backed configuration for Rails 3, with defaults from config file.
142
+ test_files: []
143
+