rails-settings-cached 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/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,100 @@
1
+ = Settings Gem
2
+
3
+ This is imporved from rails-settings, added caching for all settings.
4
+ Settings is a plugin that makes managing a table of global key, value pairs easy.
5
+ Think of it like a global Hash stored in you database, that uses simple ActiveRecord
6
+ like methods for manipulation. Keep track of any global setting that you dont want
7
+ to hard code into your rails app. You can store any kind of object. Strings, numbers,
8
+ arrays, or any object. Ported to Rails 3!
9
+
10
+
11
+ == Setup
12
+
13
+ Edit your Gemfile:
14
+ gem "rails-settings-cached"
15
+
16
+ Generate your settings:
17
+ rails g settings <settings_name>
18
+
19
+ Now just put that migration in the database with:
20
+ rake db:migrate
21
+
22
+
23
+ == Usage
24
+
25
+ You need create a class in app/models/setting.rb :
26
+
27
+ # app/models/setting.rb
28
+ # extension RailsSettings::CachedSettings
29
+ class Setting < RailsSettings::CachedSettings
30
+ end
31
+
32
+ The syntax is easy. First, lets create some settings to keep track of:
33
+
34
+ Setting.admin_password = 'supersecret'
35
+ Setting.date_format = '%m %d, %Y'
36
+ Setting.cocktails = ['Martini', 'Screwdriver', 'White Russian']
37
+ Setting.foo = 123
38
+ Setting.credentials = { :username => 'tom', :password => 'secret' }
39
+
40
+ Now lets read them back:
41
+
42
+ Setting.foo # returns 123
43
+
44
+ Changing an existing setting is the same as creating a new setting:
45
+
46
+ Setting.foo = 'super duper bar'
47
+
48
+ For changing an existing setting which is a Hash, you can merge new values with existing ones:
49
+ Setting.merge! :credentials, :password => 'topsecret'
50
+ Setting.credentials # returns { :username => 'tom', :password => 'topsecret' }
51
+
52
+ Decide you dont want to track a particular setting anymore?
53
+
54
+ Setting.destroy :foo
55
+ Setting.foo # returns nil
56
+
57
+ Want a list of all the settings?
58
+
59
+ Setting.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
60
+
61
+ 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:
62
+
63
+ Setting['preferences.color'] = :blue
64
+ Setting['preferences.size'] = :large
65
+ Setting['license.key'] = 'ABC-DEF'
66
+ Setting.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
67
+
68
+ Set defaults for certain settings of your app. This will cause the defined settings to return with the
69
+ Specified value even if they are not in the database. Make a new file in config/initializers/default_settings.rb
70
+ with the following:
71
+
72
+ Setting.defaults[:some_setting] = 'footastic'
73
+
74
+ Now even if the database is completely empty, you app will have some intelligent defaults:
75
+
76
+ Setting.some_setting # returns 'footastic'
77
+
78
+ Settings may be bound to any existing ActiveRecord object. Define this association like this:
79
+ Notice! has_settings is not do caching in this version.
80
+
81
+ class User < ActiveRecord::Base
82
+ has_settings
83
+ end
84
+
85
+ Then you can set/get a setting for a given user instance just by doing this:
86
+
87
+ user = User.find(123)
88
+ user.settings.color = :red
89
+ user.settings.color # returns :red
90
+ user.settings.all # { "color" => :red }
91
+
92
+ I you want to find users having or not having some settings, there are named scopes for this:
93
+
94
+ User.with_settings # => returns a scope of users having any setting
95
+ User.with_settings_for('color') # => returns a scope of users having a 'color' setting
96
+
97
+ User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {})
98
+ User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
99
+
100
+ 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.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
+ require "rails-settings/cached_settings"
4
+
5
+ require "rails-settings/railtie" if defined?(Rails) && Rails.version >= "3"
@@ -0,0 +1,18 @@
1
+ module RailsSettings
2
+ class CachedSettings < Settings
3
+ after_update :rewrite_cache
4
+ after_create :rewrite_cache
5
+ def rewrite_cache
6
+ Rails.cache.write("settings:#{self.var}", self.value)
7
+ end
8
+
9
+ before_destroy { |record| Rails.cache.delete("settings:#{record.var}") }
10
+
11
+
12
+ def self.[](var_name)
13
+ Rails.cache.fetch("settings:#{var_name}") {
14
+ super(var_name)
15
+ }
16
+ end
17
+ end
18
+ end
@@ -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,53 @@
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-cached}
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", "Jason Lee"]
12
+ s.date = %q{2011-01-14}
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
+ "lib/rails-settings/cached_settings.rb",
31
+ "rails-settings-cached.gemspec",
32
+ "rails/init.rb"
33
+ ]
34
+ s.homepage = %q{http://theblackestbox.net}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{This is imporved from rails-settings, added caching. 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!}
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
46
+ else
47
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
51
+ end
52
+ end
53
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails-settings'
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-settings-cached
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Squeegy
13
+ - Georg Ledermann
14
+ - 100hz
15
+ - Jason Lee
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-01-14 00:00:00 +08:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: rails
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 0
35
+ version: 3.0.0
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description:
39
+ email: rails-settings@theblackestbox.net
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.rdoc
46
+ files:
47
+ - .project
48
+ - MIT-LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - VERSION
52
+ - lib/generators/settings/settings_generator.rb
53
+ - lib/generators/settings/templates/migration.rb
54
+ - lib/generators/settings/templates/model.rb
55
+ - lib/rails-settings.rb
56
+ - lib/rails-settings/railtie.rb
57
+ - lib/rails-settings/scoped_settings.rb
58
+ - lib/rails-settings/settings.rb
59
+ - lib/rails-settings/cached_settings.rb
60
+ - rails-settings-cached.gemspec
61
+ - rails/init.rb
62
+ has_rdoc: true
63
+ homepage: http://theblackestbox.net
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: This is imporved from rails-settings, added caching. 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!
94
+ test_files: []
95
+