rails-3-settings 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.project ADDED
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>rails-settings</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/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2006 Alex Wayne
2
+ Some additional features added 2009 by Georg Ledermann
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
18
+ NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,91 @@
1
+ = Settings Gem
2
+
3
+ Settings is a plugin that makes managing a table of global key, value pairs easy.
4
+ Think of it like a global Hash stored in you database, that uses simple ActiveRecord
5
+ like methods for manipulation. Keep track of any global setting that you dont want
6
+ to hard code into your rails app. You can store any kind of object. Strings, numbers,
7
+ arrays, or any object. Ported to Rails 3!
8
+
9
+
10
+ == Setup
11
+
12
+ Edit your Gemfile:
13
+ gem "rails-settings", :git => "git://github.com/100hz/rails-settings.git"
14
+
15
+ Generate your settings:
16
+ rails g settings <settings_name>
17
+
18
+ Now just put that migration in the database with:
19
+ rake db:migrate
20
+
21
+
22
+ == Usage
23
+
24
+ The syntax is easy. First, lets create some settings to keep track of:
25
+
26
+ MySettings.admin_password = 'supersecret'
27
+ MySettings.date_format = '%m %d, %Y'
28
+ MySettings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
29
+ MySettings.foo = 123
30
+ MySettings.credentials = { :username => 'tom', :password => 'secret' }
31
+
32
+ Now lets read them back:
33
+
34
+ MySettings.foo # returns 123
35
+
36
+ Changing an existing setting is the same as creating a new setting:
37
+
38
+ MySettings.foo = 'super duper bar'
39
+
40
+ For changing an existing setting which is a Hash, you can merge new values with existing ones:
41
+ MySettings.merge! :credentials, :password => 'topsecret'
42
+ MySettings.credentials # returns { :username => 'tom', :password => 'topsecret' }
43
+
44
+ Decide you dont want to track a particular setting anymore?
45
+
46
+ MySettings.destroy :foo
47
+ MySettings.foo # returns nil
48
+
49
+ Want a list of all the settings?
50
+
51
+ MySettings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
52
+
53
+ You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use Settings.all like this:
54
+
55
+ MySettings['preferences.color'] = :blue
56
+ MySettings['preferences.size'] = :large
57
+ MySettings['license.key'] = 'ABC-DEF'
58
+ MySettings.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
59
+
60
+ Set defaults for certain settings of your app. This will cause the defined settings to return with the
61
+ Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
62
+ with the following:
63
+
64
+ MySettings.defaults[:some_setting] = 'footastic'
65
+
66
+ Now even if the database is completely empty, you app will have some intelligent defaults:
67
+
68
+ MySettings.some_setting # returns 'footastic'
69
+
70
+ Settings may be bound to any existing ActiveRecord object. Define this association like this:
71
+
72
+ class User < ActiveRecord::Base
73
+ has_settings
74
+ end
75
+
76
+ Then you can set/get a setting for a given user instance just by doing this:
77
+
78
+ user = User.find(123)
79
+ user.settings.color = :red
80
+ user.settings.color # returns :red
81
+ user.settings.all # { "color" => :red }
82
+
83
+ I you want to find users having or not having some settings, there are named scopes for this:
84
+
85
+ User.with_settings # => returns a scope of users having any setting
86
+ User.with_settings_for('color') # => returns a scope of users having a 'color' setting
87
+
88
+ User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {})
89
+ User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
90
+
91
+ That's all there is to it! Enjoy!
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new() do |gem|
7
+ gem.name = "rails-3-settings"
8
+ gem.summary = "Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object. Ported to Rails 3!"
9
+ gem.email = "rails-settings@theblackestbox.net"
10
+ gem.homepage = "http://github.com/100hz/rails-settings"
11
+ gem.authors = ["Squeegy","Georg Ledermann","100hz"]
12
+ gem.add_dependency "rails", ">= 3.0.0"
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+
20
+ task :default => :release
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,30 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class SettingsGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::Migration
5
+
6
+ argument :name, :type => :string, :default => "my_settings"
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ @@migrations = false
11
+
12
+ def self.next_migration_number(dirname) #:nodoc:
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ if @@migrations
15
+ (current_migration_number(dirname) + 1)
16
+ else
17
+ @@migrations = true
18
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
19
+ end
20
+ else
21
+ "%.3d" % (current_migration_number(dirname) + 1)
22
+ end
23
+ end
24
+
25
+ def settings
26
+ #generate(:model, name, "--skip-migration")
27
+ template "model.rb", File.join("app/models",class_path,"#{file_name}.rb"), :force => true
28
+ migration_template "migration.rb", "db/migrate/create_settings.rb"
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ class CreateSettings < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :settings do |t|
4
+ t.string :var, :null => false
5
+ t.text :value, :null => true
6
+ t.integer :thing_id, :null => true
7
+ t.string :thing_type, :limit => 30, :null => true
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :settings, [ :thing_type, :thing_id, :var ], :unique => true
12
+ end
13
+
14
+ def self.down
15
+ drop_table :settings
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %> < RailsSettings::Settings
2
+
3
+ end
@@ -0,0 +1,5 @@
1
+ require "rails-settings/settings"
2
+ require "rails-settings/scoped_settings"
3
+
4
+
5
+ require "rails-settings/railtie" if defined?(Rails) && Rails.version >= "3"
@@ -0,0 +1,43 @@
1
+ module RailsSettings
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'rails_settings.initialize', :after => :after_initialize do
5
+ Railtie.extend_active_record
6
+ end
7
+
8
+ end
9
+
10
+ class Railtie
11
+ def self.extend_active_record
12
+ ActiveRecord::Base.class_eval do
13
+ def self.has_settings
14
+ class_eval do
15
+ def settings
16
+ RailsSettings::ScopedSettings.for_thing(self)
17
+ end
18
+
19
+ scope :with_settings, :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
20
+ settings.thing_type = '#{self.base_class.name}')",
21
+ :select => "DISTINCT #{self.table_name}.*"
22
+
23
+ scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
24
+ settings.thing_type = '#{self.base_class.name}') AND
25
+ settings.var = '#{var}'" } }
26
+
27
+ scope :without_settings, :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
28
+ settings.thing_type = '#{self.base_class.name}')",
29
+ :conditions => 'settings.id IS NULL'
30
+
31
+ scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
32
+ settings.thing_type = '#{self.base_class.name}') AND
33
+ settings.var = '#{var}'",
34
+ :conditions => 'settings.id IS NULL' } }
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+
@@ -0,0 +1,13 @@
1
+ module RailsSettings
2
+ class ScopedSettings < Settings
3
+ def self.for_thing(object)
4
+ @object = object
5
+ self
6
+ end
7
+
8
+ def self.thing_scoped
9
+ Settings.scoped_by_thing_type_and_thing_id(@object.class.base_class.to_s, @object.id)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,112 @@
1
+ module RailsSettings
2
+ class Settings < ActiveRecord::Base
3
+
4
+ set_table_name 'settings'
5
+
6
+ class SettingNotFound < RuntimeError; end
7
+
8
+ cattr_accessor :defaults
9
+ @@defaults = {}.with_indifferent_access
10
+
11
+ # Support old plugin
12
+ if defined?(SettingsDefaults::DEFAULTS)
13
+ @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
14
+ end
15
+
16
+ #get or set a variable with the variable as the called method
17
+ def self.method_missing(method, *args)
18
+ method_name = method.to_s
19
+ super(method, *args)
20
+
21
+ rescue NoMethodError
22
+ #set a value for a variable
23
+ if method_name =~ /=$/
24
+ var_name = method_name.gsub('=', '')
25
+ value = args.first
26
+ self[var_name] = value
27
+
28
+ #retrieve a value
29
+ else
30
+ self[method_name]
31
+
32
+ end
33
+ end
34
+
35
+ #destroy the specified settings record
36
+ def self.destroy(var_name)
37
+ var_name = var_name.to_s
38
+ if self[var_name]
39
+ object(var_name).destroy
40
+ true
41
+ else
42
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
43
+ end
44
+ end
45
+
46
+ #retrieve all settings as a hash (optionally starting with a given namespace)
47
+ def self.all(starting_with=nil)
48
+ options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
49
+ vars = thing_scoped.find(:all, {:select => 'var, value'}.merge(options))
50
+
51
+ result = {}
52
+ vars.each do |record|
53
+ result[record.var] = record.value
54
+ end
55
+ result.with_indifferent_access
56
+ end
57
+
58
+ #get a setting value by [] notation
59
+ def self.[](var_name)
60
+ if var = object(var_name)
61
+ var.value
62
+ elsif @@defaults[var_name.to_s]
63
+ @@defaults[var_name.to_s]
64
+ else
65
+ nil
66
+ end
67
+ end
68
+
69
+ #set a setting value by [] notation
70
+ def self.[]=(var_name, value)
71
+ var_name = var_name.to_s
72
+
73
+ record = object(var_name) || thing_scoped.new(:var => var_name)
74
+ record.value = value
75
+ record.save!
76
+
77
+ value
78
+ end
79
+
80
+ def self.merge!(var_name, hash_value)
81
+ raise ArgumentError unless hash_value.is_a?(Hash)
82
+
83
+ old_value = self[var_name] || {}
84
+ raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
85
+
86
+ new_value = old_value.merge(hash_value)
87
+ self[var_name] = new_value if new_value != old_value
88
+
89
+ new_value
90
+ end
91
+
92
+ def self.object(var_name)
93
+ thing_scoped.find_by_var(var_name.to_s)
94
+ end
95
+
96
+ #get the value field, YAML decoded
97
+ def value
98
+ YAML::load(self[:value])
99
+ end
100
+
101
+ #set the value field, YAML encoded
102
+ def value=(new_value)
103
+ self[:value] = new_value.to_yaml
104
+ end
105
+
106
+ def self.thing_scoped
107
+ self.scoped_by_thing_type_and_thing_id(nil, nil)
108
+ end
109
+
110
+
111
+ end
112
+ end
@@ -0,0 +1,52 @@
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{rails-settings}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Squeegy", "Georg Ledermann", "100hz"]
12
+ s.date = %q{2010-10-07}
13
+ s.email = %q{rails-settings@theblackestbox.net}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".project",
19
+ "MIT-LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/generators/settings/settings_generator.rb",
24
+ "lib/generators/settings/templates/migration.rb",
25
+ "lib/generators/settings/templates/model.rb",
26
+ "lib/rails-settings.rb",
27
+ "lib/rails-settings/railtie.rb",
28
+ "lib/rails-settings/scoped_settings.rb",
29
+ "lib/rails-settings/settings.rb",
30
+ "rails-settings.gemspec",
31
+ "rails/init.rb"
32
+ ]
33
+ s.homepage = %q{http://theblackestbox.net}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object. Ported to Rails 3!}
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<rails>, [">= 3.0.0"])
45
+ else
46
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
50
+ end
51
+ end
52
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails-settings'
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-3-settings
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Squeegy
9
+ - Georg Ledermann
10
+ - 100hz
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2011-04-14 00:00:00 +02:00
16
+ default_executable:
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: rails
20
+ prerelease: false
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ type: :runtime
28
+ version_requirements: *id001
29
+ description:
30
+ email: rails-settings@theblackestbox.net
31
+ executables: []
32
+
33
+ extensions: []
34
+
35
+ extra_rdoc_files:
36
+ - README.rdoc
37
+ files:
38
+ - .project
39
+ - MIT-LICENSE
40
+ - README.rdoc
41
+ - Rakefile
42
+ - VERSION
43
+ - lib/generators/settings/settings_generator.rb
44
+ - lib/generators/settings/templates/migration.rb
45
+ - lib/generators/settings/templates/model.rb
46
+ - lib/rails-settings.rb
47
+ - lib/rails-settings/railtie.rb
48
+ - lib/rails-settings/scoped_settings.rb
49
+ - lib/rails-settings/settings.rb
50
+ - rails-settings.gemspec
51
+ - rails/init.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/100hz/rails-settings
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.5.1
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object. Ported to Rails 3!
80
+ test_files: []
81
+