rails-settings-rails32 0.1.2

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/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2
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-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://theblackestbox.net"
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.2
@@ -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,47 @@
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
+ def settings=(hash)
20
+ hash.each { |k,v| settings[k] = v }
21
+ end
22
+
23
+ scope :with_settings, :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
24
+ settings.thing_type = '#{self.base_class.name}')",
25
+ :select => "DISTINCT #{self.table_name}.*"
26
+
27
+ scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
28
+ settings.thing_type = '#{self.base_class.name}') AND
29
+ settings.var = '#{var}'" } }
30
+
31
+ scope :without_settings, :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
32
+ settings.thing_type = '#{self.base_class.name}')",
33
+ :conditions => 'settings.id IS NULL'
34
+
35
+ scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
36
+ settings.thing_type = '#{self.base_class.name}') AND
37
+ settings.var = '#{var}'",
38
+ :conditions => 'settings.id IS NULL' } }
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+
47
+
@@ -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.where(:thing_type => @object.class.base_class.to_s, :thing_id => @object.id)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,115 @@
1
+ module RailsSettings
2
+ class Settings < ActiveRecord::Base
3
+
4
+ self.table_name = 'settings'
5
+ attr_accessible :var
6
+
7
+ class SettingNotFound < RuntimeError; end
8
+
9
+ cattr_accessor :defaults
10
+ @@defaults = {}.with_indifferent_access
11
+
12
+ # Support old plugin
13
+ if defined?(SettingsDefaults::DEFAULTS)
14
+ @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
15
+ end
16
+
17
+ #get or set a variable with the variable as the called method
18
+ def self.method_missing(method, *args)
19
+ method_name = method.to_s
20
+ super(method, *args)
21
+
22
+ rescue NoMethodError
23
+ #set a value for a variable
24
+ if method_name =~ /=$/
25
+ var_name = method_name.gsub('=', '')
26
+ value = args.first
27
+ self[var_name] = value
28
+
29
+ #retrieve a value
30
+ else
31
+ self[method_name]
32
+
33
+ end
34
+ end
35
+
36
+ def self.defaults=(defaults)
37
+ @@defaults = defaults.with_indifferent_access
38
+ end
39
+
40
+ #destroy the specified settings record
41
+ def self.destroy(var_name)
42
+ var_name = var_name.to_s
43
+ if self[var_name]
44
+ object(var_name).destroy
45
+ true
46
+ else
47
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
48
+ end
49
+ end
50
+
51
+ #retrieve all settings as a hash (optionally starting with a given namespace)
52
+ def self.all(starting_with=nil)
53
+ options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
54
+ vars = thing_scoped.find(:all, {:select => 'var, value'}.merge(options))
55
+
56
+ result = {}
57
+ vars.each do |record|
58
+ result[record.var] = record.value
59
+ end
60
+ @@defaults.select { |k,_| k =~ /^#{starting_with}/ }.merge(result).with_indifferent_access
61
+ end
62
+
63
+ #get a setting value by [] notation
64
+ def self.[](var_name)
65
+ if var = object(var_name)
66
+ var.value
67
+ elsif @@defaults[var_name.to_s]
68
+ @@defaults[var_name.to_s]
69
+ else
70
+ nil
71
+ end
72
+ end
73
+
74
+ #set a setting value by [] notation
75
+ def self.[]=(var_name, value)
76
+ var_name = var_name.to_s
77
+
78
+ record = object(var_name) || thing_scoped.new(:var => var_name)
79
+ record.value = value
80
+ record.save!
81
+
82
+ value
83
+ end
84
+
85
+ def self.merge!(var_name, hash_value)
86
+ raise ArgumentError unless hash_value.is_a?(Hash)
87
+
88
+ old_value = self[var_name] || {}
89
+ raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
90
+
91
+ new_value = old_value.merge(hash_value)
92
+ self[var_name] = new_value if new_value != old_value
93
+
94
+ new_value
95
+ end
96
+
97
+ def self.object(var_name)
98
+ thing_scoped.find_by_var(var_name.to_s)
99
+ end
100
+
101
+ #get the value field, YAML decoded
102
+ def value
103
+ YAML::load(self[:value])
104
+ end
105
+
106
+ #set the value field, YAML encoded
107
+ def value=(new_value)
108
+ self[:value] = new_value.to_yaml
109
+ end
110
+
111
+ def self.thing_scoped
112
+ self.where(:thing_type => nil, :thing_id => nil)
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,51 @@
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{rails-settings-rails32}
8
+ s.version = "0.1.2"
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{2011-07-08}
13
+ s.email = %q{rails-settings@theblackestbox.net}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".project",
19
+ ".rvmrc",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/generators/settings/settings_generator.rb",
25
+ "lib/generators/settings/templates/migration.rb",
26
+ "lib/generators/settings/templates/model.rb",
27
+ "lib/rails-settings.rb",
28
+ "lib/rails-settings/railtie.rb",
29
+ "lib/rails-settings/scoped_settings.rb",
30
+ "lib/rails-settings/settings.rb",
31
+ "rails-settings.gemspec",
32
+ "rails/init.rb"
33
+ ]
34
+ s.homepage = %q{http://theblackestbox.net}
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.7.2}
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
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
44
+ else
45
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
49
+ end
50
+ end
51
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails-settings'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-settings-rails32
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Squeegy
9
+ - Georg Ledermann
10
+ - 100hz
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2011-07-08 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ requirement: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: 3.0.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: 3.0.0
32
+ description:
33
+ email: rails-settings@theblackestbox.net
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files:
37
+ - README.rdoc
38
+ files:
39
+ - .project
40
+ - .rvmrc
41
+ - MIT-LICENSE
42
+ - README.rdoc
43
+ - Rakefile
44
+ - VERSION
45
+ - lib/generators/settings/settings_generator.rb
46
+ - lib/generators/settings/templates/migration.rb
47
+ - lib/generators/settings/templates/model.rb
48
+ - lib/rails-settings.rb
49
+ - lib/rails-settings/railtie.rb
50
+ - lib/rails-settings/scoped_settings.rb
51
+ - lib/rails-settings/settings.rb
52
+ - rails-settings.gemspec
53
+ - rails/init.rb
54
+ homepage: http://theblackestbox.net
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.23
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Settings is a plugin that makes managing a table of global key, value pairs
78
+ easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord
79
+ like methods for manipulation. Keep track of any global setting that you dont want
80
+ to hard code into your rails app. You can store any kind of object. Strings, numbers,
81
+ arrays, or any object. Ported to Rails 3!
82
+ test_files: []