rails-settings-cached 0.4.1 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6bd842dbc504791a77630162468aa6e478a995c2
4
- data.tar.gz: 5dcfb862ea79ee47eec0dd6316dfad8b3d1ff811
3
+ metadata.gz: 80bfa0c52c2d8fe8b4535ff364b0d727476f9e5c
4
+ data.tar.gz: ca264291977f18ee792e52191e6415974263ca13
5
5
  SHA512:
6
- metadata.gz: cd476936667b371251f5fb6b66a3a1dabdf5e99b6808171ecfdd0baa38bca03df6e6ed9475ed7a488626795952076a87d42923b174a13da57e4e1d24bb7c5e95
7
- data.tar.gz: 8f093f58bf66a5939d57c6d412a7b9ce742b95f0de4d2f34ea1c0db53fc720df7aa23761964162b0a6d9ff1344630afa405ad9c348bc266a18c0a8ef4008d560
6
+ metadata.gz: 88de4555fb37729d39d6f1f2f6868fac66399662c2d0139bb59783e07760cd6ca225c45ad2e1d42b10095be5bf8dbe6e356f2961347805ef7394045ce145ac3f
7
+ data.tar.gz: 4bdbfc25e039ac4b95baad02f27002a81710cd5ad0378b95f18ccab8583edeef080daba0697f149240c646783c9292ebdc98f5064093078e1587669d5875984a
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # Settings Gem
2
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!
3
+ This is improved from [rails-settings](https://github.com/ledermann/rails-settings),
4
+ added caching for all settings. Settings is a plugin that makes managing a table of
5
+ global key, value pairs easy. Think of it like a global Hash stored in your database,
6
+ that uses simple ActiveRecord like methods for manipulation. Keep track of any global
7
+ setting that you dont want to hard code into your rails app. You can store any kind
8
+ of object. Strings, numbers, arrays, or any object.
9
9
 
10
10
  ## Status
11
11
 
@@ -18,7 +18,7 @@ Edit your Gemfile:
18
18
 
19
19
  ```ruby
20
20
  # Rails 4.1.x
21
- gem "rails-settings-cached", "0.4.1"
21
+ gem "rails-settings-cached", "~> 0.4.0"
22
22
  # Rails 4+
23
23
  gem "rails-settings-cached", "0.3.1"
24
24
  # Rails 3.x
@@ -84,19 +84,24 @@ Setting.foo # returns nil
84
84
  ```
85
85
 
86
86
  Want a list of all the settings?
87
-
88
87
  ```ruby
88
+ # Rails 4.1.x
89
89
  Setting.get_all
90
+ # Rails 3.x and 4.0.x
91
+ Setting.all
90
92
  # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
91
93
  ```
92
94
 
93
- 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.get_all like this:
95
+ 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.get_all` (`Settings.all` for # Rails 3.x and 4.0.x) like this:
94
96
 
95
97
  ```ruby
96
98
  Setting['preferences.color'] = :blue
97
99
  Setting['preferences.size'] = :large
98
100
  Setting['license.key'] = 'ABC-DEF'
101
+ # Rails 4.1.x
99
102
  Setting.get_all('preferences.')
103
+ # Rails 3.x and 4.0.x
104
+ Setting.all('preferences.')
100
105
  # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
101
106
  ```
102
107
 
@@ -137,7 +142,11 @@ Then you can set/get a setting for a given user instance just by doing this:
137
142
  user = User.find(123)
138
143
  user.settings.color = :red
139
144
  user.settings.color # returns :red
140
- user.settings.get_all # { "color" => :red }
145
+ # Rails 4.1.x
146
+ user.settings.get_all
147
+ # Rails 3.x and 4.0.x
148
+ user.settings.all
149
+ # { "color" => :red }
141
150
  ```
142
151
 
143
152
  I you want to find users having or not having some settings, there are named scopes for this:
@@ -168,7 +177,7 @@ class SettingsController < ApplicationController
168
177
  # to get all items for render list
169
178
  @settings = Setting.unscoped
170
179
  end
171
-
180
+
172
181
  def edit
173
182
  @setting = Setting.unscoped.find(params[:id])
174
183
  end
@@ -176,4 +185,6 @@ end
176
185
  ```
177
186
 
178
187
 
179
- That's all there is to it! Enjoy!
188
+ Also you may use [rails-settings-ui](https://github.com/accessd/rails-settings-ui) gem
189
+ for building ready to using interface with validations.
190
+
@@ -1,30 +1,30 @@
1
1
  require 'rails/generators/migration'
2
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
3
+ class SettingsGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::Migration
24
5
 
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"
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')
29
19
  end
30
- 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
@@ -1,14 +1,14 @@
1
1
  class CreateSettings < ActiveRecord::Migration
2
2
  def self.up
3
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
4
+ t.string :var, null: false
5
+ t.text :value, null: true
6
+ t.integer :thing_id, null: true
7
+ t.string :thing_type, null: true, limit: 30
8
8
  t.timestamps
9
9
  end
10
-
11
- add_index :settings, [ :thing_type, :thing_id, :var ], :unique => true
10
+
11
+ add_index :settings, %i(thing_type thing_id var), unique: true
12
12
  end
13
13
 
14
14
  def self.down
@@ -1,2 +1,3 @@
1
+ # RailsSettings Model
1
2
  class <%= class_name %> < RailsSettings::CachedSettings
2
3
  end
@@ -1,25 +1,27 @@
1
1
  module RailsSettings
2
2
  class CachedSettings < Settings
3
- after_update :rewrite_cache
4
- after_create :rewrite_cache
5
3
  def rewrite_cache
6
- Rails.cache.write("settings:#{self.var}", self.value)
4
+ Rails.cache.write("rails_settings_cached:#{var}", value)
7
5
  end
8
-
9
- after_destroy { |record| Rails.cache.delete("settings:#{record.var}") }
10
-
11
- def self.[](var_name)
12
- cache_key = "settings:#{var_name}"
13
- obj = Rails.cache.fetch(cache_key) {
14
- super(var_name)
15
- }
16
- obj == nil ? @@defaults[var_name.to_s] : obj
17
- end
18
-
19
- def self.save_default(key,value)
20
- if self.send(key) == nil
21
- self.send("#{key}=",value)
6
+
7
+ def expire_cache
8
+ Rails.cache.delete("rails_settings_cached:#{var}")
9
+ end
10
+
11
+ class << self
12
+ def [](var_name)
13
+ cache_key = "rails_settings_cached:#{var_name}"
14
+ obj = Rails.cache.read(cache_key)
15
+ obj = super(var_name) if obj.nil?
16
+
17
+ return @@defaults[var_name.to_s] if obj.nil?
18
+ obj
19
+ end
20
+
21
+ def save_default(key, value)
22
+ return false unless self[key].nil?
23
+ self[key] = value
22
24
  end
23
25
  end
24
26
  end
25
- end
27
+ end
@@ -1,37 +1,34 @@
1
1
  module RailsSettings
2
2
  module Extend
3
3
  extend ActiveSupport::Concern
4
-
5
- included do
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
-
12
4
 
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}'")
5
+ included do
6
+ scope :with_settings, lambda {
7
+ joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
8
+ settings.thing_type = '#{base_class.name}')")
9
+ .select("DISTINCT #{table_name}.*")
16
10
  }
17
-
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
- }
22
-
23
- scope :without_settings_for, ->(var) {
11
+
12
+ scope :with_settings_for, lambda { |var|
13
+ joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
14
+ settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
15
+ }
16
+
17
+ scope :without_settings, lambda {
18
+ joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
19
+ settings.thing_type = '#{base_class.name}')")
20
+ .where('settings.id IS NULL')
21
+ }
22
+
23
+ scope :without_settings_for, lambda { |var|
24
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}'")
25
+ .joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
26
+ settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
27
27
  }
28
-
29
28
  end
30
-
29
+
31
30
  def settings
32
31
  ScopedSettings.for_thing(self)
33
32
  end
34
33
  end
35
34
  end
36
-
37
-
@@ -4,10 +4,9 @@ module RailsSettings
4
4
  @object = object
5
5
  self
6
6
  end
7
-
7
+
8
8
  def self.thing_scoped
9
- unscoped.where(:thing_type => @object.class.base_class.to_s, :thing_id => @object.id)
9
+ unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
10
10
  end
11
-
12
11
  end
13
- end
12
+ end
@@ -1,6 +1,5 @@
1
1
  module RailsSettings
2
2
  class Settings < ActiveRecord::Base
3
-
4
3
  self.table_name = table_name_prefix + 'settings'
5
4
 
6
5
  class SettingNotFound < RuntimeError; end
@@ -13,106 +12,95 @@ module RailsSettings
13
12
  @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
14
13
  end
15
14
 
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
- rescue NoMethodError
21
- #set a value for a variable
22
- if method_name =~ /=$/
23
- var_name = method_name.gsub('=', '')
24
- value = args.first
25
- self[var_name] = value
26
- #retrieve a value
27
- else
28
- self[method_name]
29
- end
15
+ # get the value field, YAML decoded
16
+ def value
17
+ YAML.load(self[:value])
30
18
  end
31
19
 
32
- #destroy the specified settings record
33
- def self.destroy(var_name)
34
- var_name = var_name.to_s
35
- obj = object(var_name)
36
- unless obj.nil?
37
- obj.destroy
38
- true
39
- else
40
- raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
41
- end
20
+ # set the value field, YAML encoded
21
+ def value=(new_value)
22
+ self[:value] = new_value.to_yaml
42
23
  end
43
24
 
44
- #retrieve all settings as a hash (optionally starting with a given namespace)
45
- def self.get_all(starting_with = nil)
46
- vars = thing_scoped.select("var,value")
47
- if starting_with
48
- vars = vars.where("var LIKE '#{starting_with}%'")
25
+ class << self
26
+ # get or set a variable with the variable as the called method
27
+ def method_missing(method, *args)
28
+ method_name = method.to_s
29
+ super(method, *args)
30
+ rescue NoMethodError
31
+ # set a value for a variable
32
+ if method_name[-1] == '='
33
+ var_name = method_name.sub('=', '')
34
+ value = args.first
35
+ self[var_name] = value
36
+ else
37
+ # retrieve a value
38
+ self[method_name]
39
+ end
49
40
  end
50
41
 
51
- result = {}
52
- vars.each do |record|
53
- result[record.var] = record.value
54
- end
55
- result.with_indifferent_access
56
- end
57
-
58
- def self.where(sql = nil)
59
- if sql
60
- vars = thing_scoped.where(sql)
42
+ # destroy the specified settings record
43
+ def destroy(var_name)
44
+ var_name = var_name.to_s
45
+ obj = object(var_name)
46
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
47
+
48
+ obj.destroy
49
+ true
61
50
  end
62
- vars
63
- end
64
-
65
- #get a setting value by [] notation
66
- def self.[](var_name)
67
- if var = object(var_name)
68
- var.value
69
- elsif @@defaults[var_name.to_s]
70
- @@defaults[var_name.to_s]
71
- else
72
- nil
51
+
52
+ # retrieve all settings as a hash (optionally starting with a given namespace)
53
+ def get_all(starting_with = nil)
54
+ vars = thing_scoped.select('var, value')
55
+ vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
56
+
57
+ result = {}
58
+ vars.each do |record|
59
+ result[record.var] = record.value
60
+ end
61
+ result.with_indifferent_access
73
62
  end
74
- end
75
63
 
76
- #set a setting value by [] notation
77
- def self.[]=(var_name, value)
78
- var_name = var_name.to_s
64
+ def where(sql = nil)
65
+ vars = thing_scoped.where(sql) if sql
66
+ vars
67
+ end
79
68
 
80
- record = object(var_name) || thing_scoped.new(:var => var_name)
81
- record.value = value
82
- record.save!
69
+ # get a setting value by [] notation
70
+ def [](var_name)
71
+ object(var_name).try(:value) || @@defaults[var_name.to_s]
72
+ end
83
73
 
84
- value
85
- end
74
+ # set a setting value by [] notation
75
+ def []=(var_name, value)
76
+ var_name = var_name.to_s
86
77
 
87
- def self.merge!(var_name, hash_value)
88
- raise ArgumentError unless hash_value.is_a?(Hash)
78
+ record = object(var_name) || thing_scoped.new(var: var_name)
79
+ record.value = value
80
+ record.save!
89
81
 
90
- old_value = self[var_name] || {}
91
- raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
82
+ value
83
+ end
92
84
 
93
- new_value = old_value.merge(hash_value)
94
- self[var_name] = new_value if new_value != old_value
85
+ def merge!(var_name, hash_value)
86
+ raise ArgumentError unless hash_value.is_a?(Hash)
95
87
 
96
- new_value
97
- end
88
+ old_value = self[var_name] || {}
89
+ raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
98
90
 
99
- def self.object(var_name)
100
- thing_scoped.where(:var => var_name.to_s).first
101
- end
91
+ new_value = old_value.merge(hash_value)
92
+ self[var_name] = new_value if new_value != old_value
102
93
 
103
- #get the value field, YAML decoded
104
- def value
105
- YAML::load(self[:value])
106
- end
94
+ new_value
95
+ end
107
96
 
108
- #set the value field, YAML encoded
109
- def value=(new_value)
110
- self[:value] = new_value.to_yaml
111
- end
97
+ def object(var_name)
98
+ thing_scoped.where(var: var_name.to_s).first
99
+ end
112
100
 
113
- def self.thing_scoped
114
- unscoped.where("thing_type is NULL and thing_id is NULL")
101
+ def thing_scoped
102
+ unscoped.where('thing_type is NULL and thing_id is NULL')
103
+ end
115
104
  end
116
-
117
105
  end
118
106
  end
@@ -1,4 +1,11 @@
1
- require "rails-settings/settings"
2
- require "rails-settings/scoped_settings"
3
- require "rails-settings/cached_settings"
4
- require "rails-settings/extend"
1
+ require_relative 'rails-settings/settings'
2
+ require_relative 'rails-settings/scoped_settings'
3
+ require_relative 'rails-settings/cached_settings'
4
+ require_relative 'rails-settings/extend'
5
+
6
+ class RailsSettings::Railtie < Rails::Railtie
7
+ initializer "rails_settings.active_record.initialization" do
8
+ RailsSettings::CachedSettings.after_commit :rewrite_cache, on: %i(create update)
9
+ RailsSettings::CachedSettings.after_commit :expire_cache, on: %i(destroy)
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-settings-cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Squeegy
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-05-12 00:00:00.000000000 Z
14
+ date: 2015-09-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -19,14 +19,56 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 4.0.0
22
+ version: 4.2.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 4.0.0
29
+ version: 4.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.3.0
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.3.0
44
+ - !ruby/object:Gem::Dependency
45
+ name: rubocop
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.10
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 1.3.10
30
72
  description:
31
73
  email: huacnlee@gmail.com
32
74
  executables: []
@@ -61,13 +103,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
103
  version: '0'
62
104
  requirements: []
63
105
  rubyforge_project:
64
- rubygems_version: 2.2.2
106
+ rubygems_version: 2.4.7
65
107
  signing_key:
66
108
  specification_version: 4
67
- summary: This is imporved from rails-settings, added caching. Settings is a plugin
109
+ summary: This is improved from rails-settings, added caching. Settings is a plugin
68
110
  that makes managing a table of global key, value pairs easy. Think of it like a
69
111
  global Hash stored in you database, that uses simple ActiveRecord like methods for
70
112
  manipulation. Keep track of any global setting that you dont want to hard code
71
113
  into your rails app. You can store any kind of object. Strings, numbers, arrays,
72
114
  or any object. Ported to Rails 3!
73
115
  test_files: []
116
+ has_rdoc: