rails-settings-cached 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +9 -1
- data/lib/generators/settings/templates/model.rb +0 -1
- data/lib/rails-settings/cached_settings.rb +11 -12
- data/lib/rails-settings/extend.rb +20 -12
- data/lib/rails-settings/scoped_settings.rb +1 -1
- data/lib/rails-settings/settings.rb +15 -10
- metadata +15 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c8b23098050aa4759654409c3c89868a28f75053
|
4
|
+
data.tar.gz: 3aea3f6687a9248d88ff6512130a3646e53c7107
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: caf5df986fc15f69e417409c6617a3c40026e6d9383d82007970699860a3fcff6390cf6393fe200200256be373c79979c26560d7db1c8e6ba71cdae1be288d5b
|
7
|
+
data.tar.gz: a74798f521363fdf8a44a6bbae620b9fca61b56ce29553805416c23bdb5702d6bc15502f2bf5a1339e68fe9a1331e135721de46394c6d700f2782f003f1bbaa2
|
data/README.md
CHANGED
@@ -25,6 +25,14 @@ Generate your settings:
|
|
25
25
|
$ rails g settings <settings_name>
|
26
26
|
```
|
27
27
|
|
28
|
+
Note: If you migrating from gem `rails-settings` then make sure you have it in your model
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class Settings < RailsSettings::CachedSettings
|
32
|
+
...
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
28
36
|
Now just put that migration in the database with:
|
29
37
|
|
30
38
|
```bash
|
@@ -101,7 +109,7 @@ Setting.some_setting
|
|
101
109
|
Init defualt value in database, this has indifferent with `Setting.defaults[:some_setting]`, this will **save the value into database**:
|
102
110
|
|
103
111
|
```ruby
|
104
|
-
Setting.save_default(:some_key
|
112
|
+
Setting.save_default(:some_key, "123")
|
105
113
|
Setting.where(:var => "some_key").count
|
106
114
|
=> 1
|
107
115
|
Setting.some_key
|
@@ -1,18 +1,17 @@
|
|
1
1
|
module RailsSettings
|
2
|
-
|
2
|
+
class CachedSettings < Settings
|
3
3
|
after_update :rewrite_cache
|
4
4
|
after_create :rewrite_cache
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def rewrite_cache
|
6
|
+
Rails.cache.write("settings:#{self.var}", self.value)
|
7
|
+
end
|
8
8
|
|
9
|
-
|
9
|
+
after_destroy { |record| Rails.cache.delete("settings:#{record.var}") }
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
}
|
11
|
+
def self.[](var_name)
|
12
|
+
obj = Rails.cache.fetch("settings:#{var_name}") {
|
13
|
+
super(var_name)
|
14
|
+
}
|
16
15
|
obj || @@defaults[var_name.to_s]
|
17
16
|
end
|
18
17
|
|
@@ -21,5 +20,5 @@ module RailsSettings
|
|
21
20
|
self.send("#{key}=",value)
|
22
21
|
end
|
23
22
|
end
|
24
|
-
|
25
|
-
end
|
23
|
+
end
|
24
|
+
end
|
@@ -3,21 +3,29 @@ module RailsSettings
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
scope :with_settings,
|
7
|
-
|
8
|
-
|
6
|
+
scope :with_settings, -> {
|
7
|
+
joins("JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
|
8
|
+
settings.thing_type = '#{self.base_class.name}')")
|
9
|
+
.select("DISTINCT #{self.table_name}.*")
|
10
|
+
}
|
11
|
+
|
9
12
|
|
10
|
-
scope :with_settings_for,
|
11
|
-
|
13
|
+
scope :with_settings_for, -> (var) {
|
14
|
+
joins("JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
|
15
|
+
settings.thing_type = '#{self.base_class.name}') AND settings.var = '#{var}'")
|
16
|
+
}
|
12
17
|
|
13
|
-
scope :without_settings,
|
14
|
-
|
15
|
-
|
18
|
+
scope :without_settings, -> {
|
19
|
+
joins("LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND settings.thing_type = '#{self.base_class.name}')")
|
20
|
+
.where("settings.id IS NULL")
|
21
|
+
}
|
16
22
|
|
17
|
-
scope :without_settings_for,
|
18
|
-
|
19
|
-
|
20
|
-
|
23
|
+
scope :without_settings_for, -> (var) {
|
24
|
+
where('settings.id IS NULL')
|
25
|
+
.joins("LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
|
26
|
+
settings.thing_type = '#{self.base_class.name}') AND settings.var = '#{var}'")
|
27
|
+
}
|
28
|
+
|
21
29
|
end
|
22
30
|
|
23
31
|
def settings
|
@@ -2,7 +2,7 @@ module RailsSettings
|
|
2
2
|
class Settings < ActiveRecord::Base
|
3
3
|
|
4
4
|
self.table_name = 'settings'
|
5
|
-
attr_accessible :var
|
5
|
+
# attr_accessible :var
|
6
6
|
|
7
7
|
class SettingNotFound < RuntimeError; end
|
8
8
|
|
@@ -18,18 +18,15 @@ module RailsSettings
|
|
18
18
|
def self.method_missing(method, *args)
|
19
19
|
method_name = method.to_s
|
20
20
|
super(method, *args)
|
21
|
-
|
22
21
|
rescue NoMethodError
|
23
22
|
#set a value for a variable
|
24
23
|
if method_name =~ /=$/
|
25
24
|
var_name = method_name.gsub('=', '')
|
26
25
|
value = args.first
|
27
26
|
self[var_name] = value
|
28
|
-
|
29
27
|
#retrieve a value
|
30
28
|
else
|
31
29
|
self[method_name]
|
32
|
-
|
33
30
|
end
|
34
31
|
end
|
35
32
|
|
@@ -45,9 +42,11 @@ module RailsSettings
|
|
45
42
|
end
|
46
43
|
|
47
44
|
#retrieve all settings as a hash (optionally starting with a given namespace)
|
48
|
-
def self.all(starting_with=nil)
|
49
|
-
|
50
|
-
|
45
|
+
def self.all(starting_with = nil)
|
46
|
+
vars = thing_scoped.select("var,value")
|
47
|
+
if starting_with
|
48
|
+
vars = vars.where("var LIKE '#{starting_with}%'")
|
49
|
+
end
|
51
50
|
|
52
51
|
result = {}
|
53
52
|
vars.each do |record|
|
@@ -55,7 +54,14 @@ module RailsSettings
|
|
55
54
|
end
|
56
55
|
result.with_indifferent_access
|
57
56
|
end
|
58
|
-
|
57
|
+
|
58
|
+
def self.where(sql = nil)
|
59
|
+
if sql
|
60
|
+
vars = thing_scoped.where(sql)
|
61
|
+
end
|
62
|
+
vars
|
63
|
+
end
|
64
|
+
|
59
65
|
#get a setting value by [] notation
|
60
66
|
def self.[](var_name)
|
61
67
|
if var = object(var_name)
|
@@ -105,9 +111,8 @@ module RailsSettings
|
|
105
111
|
end
|
106
112
|
|
107
113
|
def self.thing_scoped
|
108
|
-
|
114
|
+
unscoped.where("thing_type is NULL and thing_id is NULL")
|
109
115
|
end
|
110
116
|
|
111
|
-
|
112
117
|
end
|
113
118
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-settings-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Squeegy
|
@@ -12,19 +11,22 @@ authors:
|
|
12
11
|
autorequire:
|
13
12
|
bindir: bin
|
14
13
|
cert_chain: []
|
15
|
-
date:
|
14
|
+
date: 2013-02-26 00:00:00.000000000 Z
|
16
15
|
dependencies:
|
17
16
|
- !ruby/object:Gem::Dependency
|
18
17
|
name: rails
|
19
|
-
requirement:
|
20
|
-
none: false
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
21
19
|
requirements:
|
22
|
-
- -
|
20
|
+
- - '>='
|
23
21
|
- !ruby/object:Gem::Version
|
24
|
-
version:
|
22
|
+
version: 4.0.0.beta1
|
25
23
|
type: :runtime
|
26
24
|
prerelease: false
|
27
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.0.0.beta1
|
28
30
|
description:
|
29
31
|
email: huacnlee@gmail.com
|
30
32
|
executables: []
|
@@ -42,27 +44,26 @@ files:
|
|
42
44
|
- README.md
|
43
45
|
homepage: https://github.com/huacnlee/rails-settings-cached
|
44
46
|
licenses: []
|
47
|
+
metadata: {}
|
45
48
|
post_install_message:
|
46
49
|
rdoc_options: []
|
47
50
|
require_paths:
|
48
51
|
- lib
|
49
52
|
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
53
|
requirements:
|
52
|
-
- -
|
54
|
+
- - '>='
|
53
55
|
- !ruby/object:Gem::Version
|
54
56
|
version: '0'
|
55
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
58
|
requirements:
|
58
|
-
- -
|
59
|
+
- - '>='
|
59
60
|
- !ruby/object:Gem::Version
|
60
61
|
version: '0'
|
61
62
|
requirements: []
|
62
63
|
rubyforge_project:
|
63
|
-
rubygems_version:
|
64
|
+
rubygems_version: 2.0.0
|
64
65
|
signing_key:
|
65
|
-
specification_version:
|
66
|
+
specification_version: 4
|
66
67
|
summary: This is imporved from rails-settings, added caching. Settings is a plugin
|
67
68
|
that makes managing a table of global key, value pairs easy. Think of it like a
|
68
69
|
global Hash stored in you database, that uses simple ActiveRecord like methods for
|