rails-settings-cached 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Settings Gem
2
+
3
+ This is improved 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
+ ## Status
11
+
12
+ [![CI Status](https://secure.travis-ci.org/huacnlee/rails-settings-cached.png)](http://travis-ci.org/huacnlee/rails-settings-cached)
13
+
14
+ ## Setup
15
+
16
+ Edit your Gemfile:
17
+
18
+ gem "rails-settings-cached"
19
+
20
+ Generate your settings:
21
+
22
+ rails g settings <settings_name>
23
+
24
+ Now just put that migration in the database with:
25
+
26
+ rake db:migrate
27
+
28
+ ## Usage
29
+
30
+ You need create a class in `app/models/setting.rb` :
31
+
32
+ # app/models/setting.rb
33
+ # extension RailsSettings::CachedSettings
34
+ class Setting < RailsSettings::CachedSettings
35
+ end
36
+
37
+ The syntax is easy. First, lets create some settings to keep track of:
38
+
39
+ Setting.admin_password = 'supersecret'
40
+ Setting.date_format = '%m %d, %Y'
41
+ Setting.cocktails = ['Martini', 'Screwdriver', 'White Russian']
42
+ Setting.foo = 123
43
+ Setting.credentials = { :username => 'tom', :password => 'secret' }
44
+
45
+ Now lets read them back:
46
+
47
+ Setting.foo # returns 123
48
+
49
+ Changing an existing setting is the same as creating a new setting:
50
+
51
+ Setting.foo = 'super duper bar'
52
+
53
+ For changing an existing setting which is a Hash, you can merge new values with existing ones:
54
+
55
+ Setting.merge!(:credentials, :password => 'topsecret')
56
+ Setting.credentials # returns { :username => 'tom', :password => 'topsecret' }
57
+
58
+ Decide you dont want to track a particular setting anymore?
59
+
60
+ Setting.destroy :foo
61
+ Setting.foo # returns nil
62
+
63
+ Want a list of all the settings?
64
+
65
+ Setting.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
66
+
67
+ You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use Setting.all like this:
68
+
69
+ Setting['preferences.color'] = :blue
70
+ Setting['preferences.size'] = :large
71
+ Setting['license.key'] = 'ABC-DEF'
72
+ Setting.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
73
+
74
+ Set defaults for certain settings of your app. This will cause the defined settings to return with the
75
+ Specified value even if they are not in the database. Make a new file in `config/initializers/default_settings.rb`
76
+ with the following:
77
+
78
+ Setting.defaults[:some_setting] = 'footastic'
79
+
80
+ Now even if the database is completely empty, you app will have some intelligent defaults:
81
+
82
+ Setting.some_setting # returns 'footastic'
83
+
84
+ Settings may be bound to any existing ActiveRecord object. Define this association like this:
85
+ Notice! is not do caching in this version.
86
+
87
+ class User < ActiveRecord::Base
88
+ include RailsSettings::Extend
89
+ end
90
+
91
+ Then you can set/get a setting for a given user instance just by doing this:
92
+
93
+ user = User.find(123)
94
+ user.settings.color = :red
95
+ user.settings.color # returns :red
96
+ user.settings.all # { "color" => :red }
97
+
98
+ I you want to find users having or not having some settings, there are named scopes for this:
99
+
100
+ User.with_settings # => returns a scope of users having any setting
101
+ User.with_settings_for('color') # => returns a scope of users having a 'color' setting
102
+
103
+ User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {})
104
+ User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
105
+
106
+ That's all there is to it! Enjoy!
@@ -1,5 +1,4 @@
1
1
  require "rails-settings/settings"
2
2
  require "rails-settings/scoped_settings"
3
3
  require "rails-settings/cached_settings"
4
-
5
- require "rails-settings/railtie" if defined?(Rails) && Rails.version >= "3"
4
+ require "rails-settings/extend"
@@ -0,0 +1,29 @@
1
+ module RailsSettings
2
+ module Extend
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ scope :with_settings, :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
7
+ settings.thing_type = '#{self.base_class.name}')",
8
+ :select => "DISTINCT #{self.table_name}.*"
9
+
10
+ scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
11
+ settings.thing_type = '#{self.base_class.name}') AND settings.var = '#{var}'" } }
12
+
13
+ scope :without_settings, :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
14
+ settings.thing_type = '#{self.base_class.name}')",
15
+ :conditions => 'settings.id IS NULL'
16
+
17
+ scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
18
+ settings.thing_type = '#{self.base_class.name}') AND
19
+ settings.var = '#{var}'",
20
+ :conditions => 'settings.id IS NULL' } }
21
+ end
22
+
23
+ def settings
24
+ ScopedSettings.for_thing(self)
25
+ end
26
+ end
27
+ end
28
+
29
+
@@ -1,7 +1,7 @@
1
1
  module RailsSettings
2
2
  class Settings < ActiveRecord::Base
3
3
 
4
- set_table_name 'settings'
4
+ self.table_name = 'settings'
5
5
 
6
6
  class SettingNotFound < RuntimeError; end
7
7
 
@@ -109,4 +109,4 @@ module RailsSettings
109
109
 
110
110
 
111
111
  end
112
- end
112
+ end
metadata CHANGED
@@ -1,14 +1,10 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
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
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Squeegy
13
9
  - Georg Ledermann
14
10
  - 100hz
@@ -16,80 +12,61 @@ authors:
16
12
  autorequire:
17
13
  bindir: bin
18
14
  cert_chain: []
19
-
20
- date: 2011-01-14 00:00:00 +08:00
21
- default_executable:
22
- dependencies:
23
- - !ruby/object:Gem::Dependency
15
+ date: 2011-01-14 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
24
18
  name: rails
25
- prerelease: false
26
- requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirement: &70106977170520 !ruby/object:Gem::Requirement
27
20
  none: false
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- segments:
32
- - 3
33
- - 0
34
- - 0
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
35
24
  version: 3.0.0
36
25
  type: :runtime
37
- version_requirements: *id001
26
+ prerelease: false
27
+ version_requirements: *70106977170520
38
28
  description:
39
- email: rails-settings@theblackestbox.net
29
+ email: huacnlee@gmail.com
40
30
  executables: []
41
-
42
31
  extensions: []
43
-
44
- extra_rdoc_files:
45
- - README.rdoc
46
- files:
47
- - .project
48
- - MIT-LICENSE
49
- - README.rdoc
50
- - Rakefile
51
- - VERSION
32
+ extra_rdoc_files: []
33
+ files:
52
34
  - lib/generators/settings/settings_generator.rb
53
35
  - lib/generators/settings/templates/migration.rb
54
36
  - lib/generators/settings/templates/model.rb
55
- - lib/rails-settings.rb
56
- - lib/rails-settings/railtie.rb
37
+ - lib/rails-settings/cached_settings.rb
38
+ - lib/rails-settings/extend.rb
57
39
  - lib/rails-settings/scoped_settings.rb
58
40
  - 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
41
+ - lib/rails-settings-cached.rb
42
+ - README.md
43
+ homepage: https://github.com/huacnlee/rails-settings-cached
64
44
  licenses: []
65
-
66
45
  post_install_message:
67
- rdoc_options:
68
- - --charset=UTF-8
69
- require_paths:
46
+ rdoc_options: []
47
+ require_paths:
70
48
  - lib
71
- required_ruby_version: !ruby/object:Gem::Requirement
49
+ required_ruby_version: !ruby/object:Gem::Requirement
72
50
  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
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
56
  none: false
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- segments:
85
- - 0
86
- version: "0"
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
87
61
  requirements: []
88
-
89
62
  rubyforge_project:
90
- rubygems_version: 1.3.7
63
+ rubygems_version: 1.8.10
91
64
  signing_key:
92
65
  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!
66
+ summary: This is imporved from rails-settings, added caching. Settings is a plugin
67
+ that makes managing a table of global key, value pairs easy. Think of it like a
68
+ global Hash stored in you database, that uses simple ActiveRecord like methods for
69
+ manipulation. Keep track of any global setting that you dont want to hard code
70
+ into your rails app. You can store any kind of object. Strings, numbers, arrays,
71
+ or any object. Ported to Rails 3!
94
72
  test_files: []
95
-
data/.project DELETED
@@ -1,12 +0,0 @@
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 DELETED
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,100 +0,0 @@
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 DELETED
@@ -1,20 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- 0.1.1
@@ -1,43 +0,0 @@
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
-
@@ -1,53 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- require 'rails-settings'