rails-settings-cached 0.4.1 → 0.4.2

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: 4c9f513e434fb39d1cfefa3ff34bac2b079d6aa9
4
+ data.tar.gz: 9d1c7c9775ab9108d5604bb1617f2bffde890e80
5
5
  SHA512:
6
- metadata.gz: cd476936667b371251f5fb6b66a3a1dabdf5e99b6808171ecfdd0baa38bca03df6e6ed9475ed7a488626795952076a87d42923b174a13da57e4e1d24bb7c5e95
7
- data.tar.gz: 8f093f58bf66a5939d57c6d412a7b9ce742b95f0de4d2f34ea1c0db53fc720df7aa23761964162b0a6d9ff1344630afa405ad9c348bc266a18c0a8ef4008d560
6
+ metadata.gz: 992cc15c9cadd8298dab78168b42d4ddf60d784a3749ecf66b7e0b1535b18487aa7b763ea89771b0e6e61ff805cb831d1a6d3700a545c51d5ba88b0dc6279f4b
7
+ data.tar.gz: 310666efc9ba502e62da3e7fd06f3282909c984fcee2518838c6ddd4c13b3045a9bd4dd485b8acd5c5beaeeca09789c04e46ef2b8f6d192a4c573e2d7a186a50
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
 
@@ -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,4 +1,4 @@
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'
@@ -1,25 +1,29 @@
1
1
  module RailsSettings
2
2
  class CachedSettings < Settings
3
- after_update :rewrite_cache
4
- after_create :rewrite_cache
3
+ after_commit :rewrite_cache, on: %i(create update)
5
4
  def rewrite_cache
6
- Rails.cache.write("settings:#{self.var}", self.value)
5
+ Rails.cache.write("rails_settings_cached:#{var}", value)
7
6
  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)
7
+
8
+ after_commit :expire_cache, on: %i(destroy)
9
+ def expire_cache
10
+ Rails.cache.delete("rails_settings_cached:#{var}")
11
+ end
12
+
13
+ class << self
14
+ def [](var_name)
15
+ cache_key = "rails_settings_cached:#{var_name}"
16
+ obj = Rails.cache.read(cache_key)
17
+ obj = super(var_name) if obj.nil?
18
+
19
+ return @@defaults[var_name.to_s] if obj.nil?
20
+ obj
21
+ end
22
+
23
+ def save_default(key, value)
24
+ return false unless send(key).nil?
25
+ send("#{key}=", value)
22
26
  end
23
27
  end
24
28
  end
25
- end
29
+ 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
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.2
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-08-27 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: