rails-settings-cached 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/lib/generators/settings/install_generator.rb +33 -0
- data/lib/rails-settings-cached.rb +8 -4
- data/lib/rails-settings/base.rb +112 -0
- data/lib/rails-settings/settings.rb +5 -108
- data/lib/rails-settings/version.rb +7 -0
- metadata +15 -69
- data/lib/generators/settings/settings_generator.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: caebc0d7ac88a78fec43016475b3ed2e9e22a730
|
4
|
+
data.tar.gz: 05861d98bbece04178fec89687f2310a42a31194
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c814d540d0f548744d4c10ced5de27142b56f4d50519ab33e7b9b8435d11970ee1cf6fce0f747df8ac33e9ecd64a115e105267e569055ae430d66e6608c1b72
|
7
|
+
data.tar.gz: f2f7b6c1ce43c34f6917a5ad9396d40735093fd06778d891c74c0cf6f4062ebcc3ee4aa5f833dc42926b2c11eccbd3e2ae9fe533daf42d470e305abea14e7b9e
|
data/README.md
CHANGED
@@ -9,13 +9,17 @@ of object. Strings, numbers, arrays, or any object.
|
|
9
9
|
|
10
10
|
## Status
|
11
11
|
|
12
|
-
[![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://api.travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached)
|
12
|
+
[![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://api.travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached) [![Code Climate](https://codeclimate.com/github/huacnlee/rails-settings-cached/badges/gpa.svg)](https://codeclimate.com/github/huacnlee/rails-settings-cached) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master)
|
13
13
|
|
14
14
|
## Setup
|
15
15
|
|
16
16
|
Edit your Gemfile:
|
17
17
|
|
18
18
|
```ruby
|
19
|
+
# Rails 5.0.x
|
20
|
+
gem 'rails-settings-cached', '~> 0.6.0'
|
21
|
+
# Rails 4.2.x
|
22
|
+
gem 'rails-settings-cached', "~> 0.5.4"
|
19
23
|
# Rails 4.1.x
|
20
24
|
gem "rails-settings-cached", "~> 0.4.0"
|
21
25
|
# Rails 4+
|
@@ -27,15 +31,13 @@ gem "rails-settings-cached", "0.2.4"
|
|
27
31
|
Generate your settings:
|
28
32
|
|
29
33
|
```bash
|
30
|
-
$ rails g settings
|
34
|
+
$ rails g settings:install
|
31
35
|
```
|
32
36
|
|
33
|
-
|
37
|
+
If you want custom model name:
|
34
38
|
|
35
|
-
```
|
36
|
-
|
37
|
-
...
|
38
|
-
end
|
39
|
+
```bash
|
40
|
+
$ rails g settings:install MySetting
|
39
41
|
```
|
40
42
|
|
41
43
|
Now just put that migration in the database with:
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Settings
|
5
|
+
class InstallGenerator < Rails::Generators::NamedBase
|
6
|
+
desc "Generate RailsSettings files."
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
argument :name, type: :string, default: 'setting'
|
10
|
+
|
11
|
+
source_root File.expand_path('../templates', __FILE__)
|
12
|
+
|
13
|
+
@@migrations = false
|
14
|
+
|
15
|
+
def self.next_migration_number(dirname) #:nodoc:
|
16
|
+
if ActiveRecord::Base.timestamped_migrations
|
17
|
+
if @@migrations
|
18
|
+
(current_migration_number(dirname) + 1)
|
19
|
+
else
|
20
|
+
@@migrations = true
|
21
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
22
|
+
end
|
23
|
+
else
|
24
|
+
format '%.3d', current_migration_number(dirname) + 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def install_setting
|
29
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb"), force: true
|
30
|
+
migration_template 'migration.rb', 'db/migrate/create_settings.rb'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,11 +1,15 @@
|
|
1
|
+
require_relative 'rails-settings/base'
|
1
2
|
require_relative 'rails-settings/settings'
|
2
3
|
require_relative 'rails-settings/cached_settings'
|
3
4
|
require_relative 'rails-settings/scoped_settings'
|
4
5
|
require_relative 'rails-settings/extend'
|
6
|
+
require_relative 'rails-settings/version'
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
module RailsSettings
|
9
|
+
class Railtie < Rails::Railtie
|
10
|
+
initializer "rails_settings.active_record.initialization" do
|
11
|
+
RailsSettings::CachedSettings.after_commit :rewrite_cache, on: %i(create update)
|
12
|
+
RailsSettings::CachedSettings.after_commit :expire_cache, on: %i(destroy)
|
13
|
+
end
|
10
14
|
end
|
11
15
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module RailsSettings
|
2
|
+
class Base < ActiveRecord::Base
|
3
|
+
self.table_name = table_name_prefix + 'settings'
|
4
|
+
|
5
|
+
class SettingNotFound < RuntimeError; end
|
6
|
+
|
7
|
+
cattr_accessor :defaults
|
8
|
+
@@defaults = {}.with_indifferent_access
|
9
|
+
|
10
|
+
belongs_to :thing, polymorphic: true
|
11
|
+
|
12
|
+
# Support old plugin
|
13
|
+
if defined?(SettingsDefaults::DEFAULTS)
|
14
|
+
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
|
15
|
+
end
|
16
|
+
|
17
|
+
# get the value field, YAML decoded
|
18
|
+
def value
|
19
|
+
YAML.load(self[:value]) if self[:value].present?
|
20
|
+
end
|
21
|
+
|
22
|
+
# set the value field, YAML encoded
|
23
|
+
def value=(new_value)
|
24
|
+
self[:value] = new_value.to_yaml
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
# get or set a variable with the variable as the called method
|
29
|
+
def method_missing(method, *args)
|
30
|
+
method_name = method.to_s
|
31
|
+
super(method, *args)
|
32
|
+
rescue NoMethodError
|
33
|
+
# set a value for a variable
|
34
|
+
if method_name[-1] == '='
|
35
|
+
var_name = method_name.sub('=', '')
|
36
|
+
value = args.first
|
37
|
+
self[var_name] = value
|
38
|
+
else
|
39
|
+
# retrieve a value
|
40
|
+
self[method_name]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# destroy the specified settings record
|
45
|
+
def destroy(var_name)
|
46
|
+
var_name = var_name.to_s
|
47
|
+
obj = object(var_name)
|
48
|
+
raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
|
49
|
+
|
50
|
+
obj.destroy
|
51
|
+
true
|
52
|
+
end
|
53
|
+
|
54
|
+
# retrieve all settings as a hash (optionally starting with a given namespace)
|
55
|
+
def get_all(starting_with = nil)
|
56
|
+
vars = thing_scoped.select('var, value')
|
57
|
+
vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
|
58
|
+
|
59
|
+
result = {}
|
60
|
+
vars.each do |record|
|
61
|
+
result[record.var] = record.value
|
62
|
+
end
|
63
|
+
default_keys = @@defaults.keys
|
64
|
+
default_keys = default_keys.select {|k| k.start_with? starting_with } if starting_with
|
65
|
+
result.merge! @@defaults.slice(*(default_keys - result.keys))
|
66
|
+
|
67
|
+
result.with_indifferent_access
|
68
|
+
end
|
69
|
+
|
70
|
+
def where(sql = nil)
|
71
|
+
vars = thing_scoped.where(sql) if sql
|
72
|
+
vars
|
73
|
+
end
|
74
|
+
|
75
|
+
# get a setting value by [] notation
|
76
|
+
def [](var_name)
|
77
|
+
object(var_name).try(:value) || @@defaults[var_name.to_s]
|
78
|
+
end
|
79
|
+
|
80
|
+
# set a setting value by [] notation
|
81
|
+
def []=(var_name, value)
|
82
|
+
var_name = var_name.to_s
|
83
|
+
|
84
|
+
record = object(var_name) || thing_scoped.new(var: var_name)
|
85
|
+
record.value = value
|
86
|
+
record.save!
|
87
|
+
|
88
|
+
value
|
89
|
+
end
|
90
|
+
|
91
|
+
def merge!(var_name, hash_value)
|
92
|
+
raise ArgumentError unless hash_value.is_a?(Hash)
|
93
|
+
|
94
|
+
old_value = self[var_name] || {}
|
95
|
+
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
|
96
|
+
|
97
|
+
new_value = old_value.merge(hash_value)
|
98
|
+
self[var_name] = new_value if new_value != old_value
|
99
|
+
|
100
|
+
new_value
|
101
|
+
end
|
102
|
+
|
103
|
+
def object(var_name)
|
104
|
+
thing_scoped.where(var: var_name.to_s).first
|
105
|
+
end
|
106
|
+
|
107
|
+
def thing_scoped
|
108
|
+
unscoped.where('thing_type is NULL and thing_id is NULL')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -1,112 +1,9 @@
|
|
1
1
|
module RailsSettings
|
2
|
-
class Settings <
|
3
|
-
self.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
cattr_accessor :defaults
|
8
|
-
@@defaults = {}.with_indifferent_access
|
9
|
-
|
10
|
-
belongs_to :thing, polymorphic: true
|
11
|
-
|
12
|
-
# Support old plugin
|
13
|
-
if defined?(SettingsDefaults::DEFAULTS)
|
14
|
-
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
|
15
|
-
end
|
16
|
-
|
17
|
-
# get the value field, YAML decoded
|
18
|
-
def value
|
19
|
-
YAML.load(self[:value]) if self[:value].present?
|
20
|
-
end
|
21
|
-
|
22
|
-
# set the value field, YAML encoded
|
23
|
-
def value=(new_value)
|
24
|
-
self[:value] = new_value.to_yaml
|
25
|
-
end
|
26
|
-
|
27
|
-
class << self
|
28
|
-
# get or set a variable with the variable as the called method
|
29
|
-
def method_missing(method, *args)
|
30
|
-
method_name = method.to_s
|
31
|
-
super(method, *args)
|
32
|
-
rescue NoMethodError
|
33
|
-
# set a value for a variable
|
34
|
-
if method_name[-1] == '='
|
35
|
-
var_name = method_name.sub('=', '')
|
36
|
-
value = args.first
|
37
|
-
self[var_name] = value
|
38
|
-
else
|
39
|
-
# retrieve a value
|
40
|
-
self[method_name]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# destroy the specified settings record
|
45
|
-
def destroy(var_name)
|
46
|
-
var_name = var_name.to_s
|
47
|
-
obj = object(var_name)
|
48
|
-
raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
|
49
|
-
|
50
|
-
obj.destroy
|
51
|
-
true
|
52
|
-
end
|
53
|
-
|
54
|
-
# retrieve all settings as a hash (optionally starting with a given namespace)
|
55
|
-
def get_all(starting_with = nil)
|
56
|
-
vars = thing_scoped.select('var, value')
|
57
|
-
vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
|
58
|
-
|
59
|
-
result = {}
|
60
|
-
vars.each do |record|
|
61
|
-
result[record.var] = record.value
|
62
|
-
end
|
63
|
-
default_keys = @@defaults.keys
|
64
|
-
default_keys = default_keys.select {|k| k.start_with? starting_with } if starting_with
|
65
|
-
result.merge! @@defaults.slice(*(default_keys - result.keys))
|
66
|
-
|
67
|
-
result.with_indifferent_access
|
68
|
-
end
|
69
|
-
|
70
|
-
def where(sql = nil)
|
71
|
-
vars = thing_scoped.where(sql) if sql
|
72
|
-
vars
|
73
|
-
end
|
74
|
-
|
75
|
-
# get a setting value by [] notation
|
76
|
-
def [](var_name)
|
77
|
-
object(var_name).try(:value) || @@defaults[var_name.to_s]
|
78
|
-
end
|
79
|
-
|
80
|
-
# set a setting value by [] notation
|
81
|
-
def []=(var_name, value)
|
82
|
-
var_name = var_name.to_s
|
83
|
-
|
84
|
-
record = object(var_name) || thing_scoped.new(var: var_name)
|
85
|
-
record.value = value
|
86
|
-
record.save!
|
87
|
-
|
88
|
-
value
|
89
|
-
end
|
90
|
-
|
91
|
-
def merge!(var_name, hash_value)
|
92
|
-
raise ArgumentError unless hash_value.is_a?(Hash)
|
93
|
-
|
94
|
-
old_value = self[var_name] || {}
|
95
|
-
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
|
96
|
-
|
97
|
-
new_value = old_value.merge(hash_value)
|
98
|
-
self[var_name] = new_value if new_value != old_value
|
99
|
-
|
100
|
-
new_value
|
101
|
-
end
|
102
|
-
|
103
|
-
def object(var_name)
|
104
|
-
thing_scoped.where(var: var_name.to_s).first
|
105
|
-
end
|
106
|
-
|
107
|
-
def thing_scoped
|
108
|
-
unscoped.where('thing_type is NULL and thing_id is NULL')
|
109
|
-
end
|
2
|
+
class Settings < Base
|
3
|
+
def self.inherited(subclass)
|
4
|
+
Kernel.warn 'DEPRECATION WARNING: RailsSettings::Settings is deprecated and it will removed in 0.6.0. ' <<
|
5
|
+
'Please use RailsSettings::Base instead.'
|
6
|
+
super(subclass)
|
110
7
|
end
|
111
8
|
end
|
112
9
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-settings-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
+
- Jason Lee
|
7
8
|
- Squeegy
|
8
9
|
- Georg Ledermann
|
9
10
|
- 100hz
|
10
|
-
- Jason Lee
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2016-
|
14
|
+
date: 2016-04-08 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -27,77 +27,28 @@ dependencies:
|
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 4.2.0
|
30
|
-
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
version: '0'
|
37
|
-
type: :development
|
38
|
-
prerelease: false
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
44
|
-
- !ruby/object:Gem::Dependency
|
45
|
-
name: rspec
|
46
|
-
requirement: !ruby/object:Gem::Requirement
|
47
|
-
requirements:
|
48
|
-
- - ">="
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version: 3.3.0
|
51
|
-
type: :development
|
52
|
-
prerelease: false
|
53
|
-
version_requirements: !ruby/object:Gem::Requirement
|
54
|
-
requirements:
|
55
|
-
- - ">="
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version: 3.3.0
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: rubocop
|
60
|
-
requirement: !ruby/object:Gem::Requirement
|
61
|
-
requirements:
|
62
|
-
- - ">="
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
version: '0'
|
65
|
-
type: :development
|
66
|
-
prerelease: false
|
67
|
-
version_requirements: !ruby/object:Gem::Requirement
|
68
|
-
requirements:
|
69
|
-
- - ">="
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
version: '0'
|
72
|
-
- !ruby/object:Gem::Dependency
|
73
|
-
name: sqlite3
|
74
|
-
requirement: !ruby/object:Gem::Requirement
|
75
|
-
requirements:
|
76
|
-
- - ">="
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version: 1.3.10
|
79
|
-
type: :development
|
80
|
-
prerelease: false
|
81
|
-
version_requirements: !ruby/object:Gem::Requirement
|
82
|
-
requirements:
|
83
|
-
- - ">="
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
version: 1.3.10
|
86
|
-
description:
|
30
|
+
description: "\n This is improved from rails-settings, added caching.\n Settings
|
31
|
+
plugin for Rails that makes managing a table of global key,\n value pairs easy.
|
32
|
+
Think of it like a global Hash stored in you database,\n that uses simple ActiveRecord
|
33
|
+
like methods for manipulation.\n\n Keep track of any global setting that you dont
|
34
|
+
want to hard code into your rails app.\n You can store any kind of object. Strings,
|
35
|
+
numbers, arrays, or any object.\n "
|
87
36
|
email: huacnlee@gmail.com
|
88
37
|
executables: []
|
89
38
|
extensions: []
|
90
39
|
extra_rdoc_files: []
|
91
40
|
files:
|
92
41
|
- README.md
|
93
|
-
- lib/generators/settings/
|
42
|
+
- lib/generators/settings/install_generator.rb
|
94
43
|
- lib/generators/settings/templates/migration.rb
|
95
44
|
- lib/generators/settings/templates/model.rb
|
96
45
|
- lib/rails-settings-cached.rb
|
46
|
+
- lib/rails-settings/base.rb
|
97
47
|
- lib/rails-settings/cached_settings.rb
|
98
48
|
- lib/rails-settings/extend.rb
|
99
49
|
- lib/rails-settings/scoped_settings.rb
|
100
50
|
- lib/rails-settings/settings.rb
|
51
|
+
- lib/rails-settings/version.rb
|
101
52
|
homepage: https://github.com/huacnlee/rails-settings-cached
|
102
53
|
licenses: []
|
103
54
|
metadata: {}
|
@@ -109,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
60
|
requirements:
|
110
61
|
- - ">="
|
111
62
|
- !ruby/object:Gem::Version
|
112
|
-
version: '2.
|
63
|
+
version: '2.1'
|
113
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
65
|
requirements:
|
115
66
|
- - ">="
|
@@ -117,13 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
68
|
version: '0'
|
118
69
|
requirements: []
|
119
70
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
71
|
+
rubygems_version: 2.6.2
|
121
72
|
signing_key:
|
122
73
|
specification_version: 4
|
123
|
-
summary:
|
124
|
-
that makes managing a table of global key, value pairs easy. Think of it like a
|
125
|
-
global Hash stored in you database, that uses simple ActiveRecord like methods for
|
126
|
-
manipulation. Keep track of any global setting that you dont want to hard code
|
127
|
-
into your rails app. You can store any kind of object. Strings, numbers, arrays,
|
128
|
-
or any object. Ported to Rails 3!
|
74
|
+
summary: Settings plugin for Rails that makes managing a table of global keys.
|
129
75
|
test_files: []
|
@@ -1,30 +0,0 @@
|
|
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
|
-
format '%.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
|