ledermann-rails-settings 1.2.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.travis.yml +14 -6
  4. data/Gemfile +1 -1
  5. data/MIT-LICENSE +6 -5
  6. data/README.md +171 -158
  7. data/Rakefile +4 -9
  8. data/ci/Gemfile-rails-4-2 +7 -0
  9. data/ci/Gemfile-rails-5-0 +6 -0
  10. data/ci/Gemfile-rails-5-1 +6 -0
  11. data/ci/Gemfile-rails-5-2 +6 -0
  12. data/ci/Gemfile-rails-6-0 +6 -0
  13. data/lib/generators/rails_settings/migration/migration_generator.rb +23 -0
  14. data/lib/generators/rails_settings/migration/templates/migration.rb +21 -0
  15. data/lib/ledermann-rails-settings.rb +1 -0
  16. data/lib/rails-settings.rb +21 -5
  17. data/lib/rails-settings/base.rb +48 -0
  18. data/lib/rails-settings/configuration.rb +39 -0
  19. data/lib/rails-settings/scopes.rb +34 -0
  20. data/lib/rails-settings/setting_object.rb +84 -0
  21. data/lib/rails-settings/version.rb +2 -2
  22. data/rails-settings.gemspec +22 -21
  23. data/spec/configuration_spec.rb +120 -0
  24. data/spec/database.yml +3 -0
  25. data/spec/queries_spec.rb +101 -0
  26. data/spec/scopes_spec.rb +31 -0
  27. data/spec/serialize_spec.rb +40 -0
  28. data/spec/setting_object_spec.rb +153 -0
  29. data/spec/settings_spec.rb +248 -0
  30. data/spec/spec_helper.rb +111 -0
  31. data/spec/support/matchers/perform_queries.rb +22 -0
  32. data/spec/support/query_counter.rb +17 -0
  33. metadata +130 -118
  34. data/Changelog.md +0 -17
  35. data/ci/Gemfile.rails-2.3.x +0 -5
  36. data/ci/Gemfile.rails-3.0.x +0 -5
  37. data/ci/Gemfile.rails-3.1.x +0 -5
  38. data/ci/Gemfile.rails-3.2.x +0 -5
  39. data/init.rb +0 -1
  40. data/lib/rails-settings/active_record.rb +0 -38
  41. data/lib/rails-settings/null_store.rb +0 -48
  42. data/lib/rails-settings/scoped_settings.rb +0 -14
  43. data/lib/rails-settings/settings.rb +0 -142
  44. data/test/settings_test.rb +0 -252
  45. data/test/test_helper.rb +0 -34
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d35191c6b15823ec6934c9b66a8cf4291aa552c79bfe1ac6e24b2298a3bf6942
4
+ data.tar.gz: 1dabb6089d5894acc9f16aa57637daded9e9751c590b924fe3a8d3e074e01ac8
5
+ SHA512:
6
+ metadata.gz: 2556adc7367efd4acd8a208d342d4f485ea356a79b1bac8971785bb2d3ab9ea3d31ec13b2008fe4e58171eb181090e7d7b18f3a242ee220fee9703777c7ca387
7
+ data.tar.gz: b391a6f0c86c8abab56b8b68b20161fbe4981c7d08acfa5766416d097d0c2ada7c77db29182b46b62853f0e822b77cc21ae300b3f42657c50cdebc7d83bdf4a8
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ coverage/*
data/.travis.yml CHANGED
@@ -1,9 +1,17 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.3
3
+ - 2.4.6
4
+ - 2.5.5
5
+ - 2.6.3
5
6
  gemfile:
6
- - ci/Gemfile.rails-2.3.x
7
- # - ci/Gemfile.rails-3.0.x
8
- - ci/Gemfile.rails-3.1.x
9
- - ci/Gemfile.rails-3.2.x
7
+ - ci/Gemfile-rails-4-2
8
+ - ci/Gemfile-rails-5-0
9
+ - ci/Gemfile-rails-5-1
10
+ - ci/Gemfile-rails-5-2
11
+ - ci/Gemfile-rails-6-0
12
+ matrix:
13
+ exclude:
14
+ - rvm: 2.4.6
15
+ gemfile: ci/Gemfile-rails-6-0
16
+ before_install: gem update bundler
17
+ sudo: false
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rails-settings.gemspec
4
4
  gemspec
data/MIT-LICENSE CHANGED
@@ -1,5 +1,6 @@
1
- Copyright (c) 2006 Alex Wayne
2
- Some additional features added 2009-2012 by Georg Ledermann
1
+ Copyright (c) 2012-2019 Georg Ledermann
2
+
3
+ MIT License
3
4
 
4
5
  Permission is hereby granted, free of charge, to any person obtaining
5
6
  a copy of this software and associated documentation files (the
@@ -14,8 +15,8 @@ included in all copies or substantial portions of the Software.
14
15
 
15
16
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
17
  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
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
21
  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.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,158 +1,171 @@
1
- # Settings Gem/Plugin for Rails
2
-
3
- [![Build Status](https://secure.travis-ci.org/ledermann/rails-settings.png)](http://travis-ci.org/ledermann/rails-settings)
4
-
5
- Settings is a gem/plugin that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any setting that you don't want to hard code into your rails app. You can store any kind of object: Strings, numbers, arrays, or any object which can be noted as YAML.
6
-
7
-
8
- ## Requirements
9
-
10
- Rails 2.3.x, 3.1.x or 3.2.x (due to an [issue with Rails caching](https://github.com/rails/rails/pull/2010) it does not work properly with Rails 3.0.x)
11
-
12
- Tested with Ruby 1.8.7 and 1.9.3
13
-
14
-
15
- ## Installation
16
-
17
- Include the gem in your Gemfile
18
-
19
- gem 'ledermann-rails-settings', :require => 'rails-settings'
20
-
21
- or install as a plugin:
22
-
23
- ./script/plugin install git://github.com/ledermann/rails-settings.git
24
-
25
-
26
- You have to create the table used by the Settings model by using this migration:
27
-
28
- class CreateSettingsTable < ActiveRecord::Migration
29
- def self.up
30
- create_table :settings, :force => true do |t|
31
- t.string :var, :null => false
32
- t.text :value
33
- t.integer :target_id
34
- t.string :target_type, :limit => 30
35
- t.timestamps
36
- end
37
-
38
- add_index :settings, [ :target_type, :target_id, :var ], :unique => true
39
- end
40
-
41
- def self.down
42
- drop_table :settings
43
- end
44
- end
45
-
46
- Now update your database with:
47
-
48
- rake db:migrate
49
-
50
- ## Usage
51
-
52
- The syntax is easy. First, lets create some settings to keep track of:
53
-
54
- Settings.admin_password = 'supersecret'
55
- Settings.date_format = '%m %d, %Y'
56
- Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
57
- Settings.foo = 123
58
- Settings.credentials = { :username => 'tom', :password => 'secret' }
59
-
60
- Now lets read them back:
61
-
62
- Settings.foo
63
- # => 123
64
-
65
- Changing an existing setting is the same as creating a new setting:
66
-
67
- Settings.foo = 'super duper bar'
68
-
69
- For changing an existing setting which is a Hash, you can merge new values with existing ones:
70
-
71
- Settings.merge! :credentials, :password => 'topsecret'
72
- Settings.credentials
73
- # => { :username => 'tom', :password => 'topsecret' }
74
-
75
- Decide you dont want to track a particular setting anymore?
76
-
77
- Settings.destroy :foo
78
- Settings.foo
79
- # => nil
80
-
81
- Want a list of all the settings (including defaults)?
82
-
83
- Settings.all
84
- # => { 'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y' }
85
-
86
- 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:
87
-
88
- Settings['preferences.color'] = :blue
89
- Settings['preferences.size'] = :large
90
- Settings['license.key'] = 'ABC-DEF'
91
- Settings.all('preferences.')
92
- # => { 'preferences.color' => :blue, 'preferences.size' => :large }
93
-
94
- Settings may be bound to any existing ActiveRecord object. Define this association like this:
95
-
96
- class User < ActiveRecord::Base
97
- has_settings
98
- end
99
-
100
- Then you can set/get a setting for a given user instance just by doing this:
101
-
102
- user = User.find(123)
103
- user.settings.color = :red
104
- user.settings.color
105
- # => :red
106
-
107
- user.settings.all
108
- # => { "color" => :red }
109
-
110
-
111
- Set defaults for certain settings of your app. This will cause the defined settings to return with the
112
- specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
113
- with the following:
114
-
115
- Settings.defaults[:foo] = 'footastic'
116
-
117
- Now even if the database is completely empty, you app will have some intelligent defaults:
118
-
119
- Settings.foo
120
- # => 'footastic'
121
-
122
- Defaults can be defined on the model level, too:
123
-
124
- User.settings.foo = 'bar'
125
- User.find(123).settings.foo
126
- # => 'bar'
127
-
128
- If the setting doesn't exist on the object or the model, you'll get the default, as expected:
129
-
130
- Settings.defaults[:some_default] = 'foo'
131
-
132
- User.settings.some_default
133
- # => 'foo'
134
-
135
- User.find(123).settings.some_default
136
- # => 'foo'
137
-
138
-
139
- I you want to find users having or not having some settings, there are named scopes for this:
140
-
141
- User.with_settings
142
- # returns a scope of users having any setting
143
-
144
- User.with_settings_for('color')
145
- # returns a scope of users having a 'color' setting
146
-
147
- User.without_settings
148
- # returns a scope of users having no setting at all (means user.settings.all == {})
149
-
150
- User.without_settings('color')
151
- # returns a scope of users having no 'color' setting (means user.settings.color == nil)
152
-
153
- For better performance, you can enable caching, e.g.:
154
-
155
- Settings.cache = ActiveSupport::Cache::MemoryStore.new
156
- Settings.cache_options = { :expires_in => 5.minutes }
157
-
158
- That's all there is to it! Enjoy!
1
+ # Settings for Rails
2
+
3
+ [![Build Status](https://travis-ci.org/ledermann/rails-settings.svg?branch=master)](https://travis-ci.org/ledermann/rails-settings)
4
+ [![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.svg)](https://codeclimate.com/github/ledermann/rails-settings)
5
+ [![Coverage Status](https://coveralls.io/repos/ledermann/rails-settings/badge.svg?branch=master)](https://coveralls.io/r/ledermann/rails-settings?branch=master)
6
+
7
+ Ruby gem to handle settings for ActiveRecord instances by storing them as serialized Hash in a separate database table. Namespaces and defaults included.
8
+
9
+ ## Requirements
10
+
11
+ * Ruby 2.4 or newer
12
+ * Rails 4.2 or newer (including Rails 6)
13
+
14
+
15
+ ## Installation
16
+
17
+ Include the gem in your Gemfile and run `bundle` to install it:
18
+
19
+ ```ruby
20
+ gem 'ledermann-rails-settings'
21
+ ```
22
+
23
+ Generate and run the migration:
24
+
25
+ ```shell
26
+ rails g rails_settings:migration
27
+ rake db:migrate
28
+ ```
29
+
30
+
31
+ ## Usage
32
+
33
+ ### Define settings
34
+
35
+ ```ruby
36
+ class User < ActiveRecord::Base
37
+ has_settings do |s|
38
+ s.key :dashboard, :defaults => { :theme => 'blue', :view => 'monthly', :filter => false }
39
+ s.key :calendar, :defaults => { :scope => 'company'}
40
+ end
41
+ end
42
+ ```
43
+
44
+ If no defaults are needed, a simplified syntax can be used:
45
+
46
+ ```ruby
47
+ class User < ActiveRecord::Base
48
+ has_settings :dashboard, :calendar
49
+ end
50
+ ```
51
+
52
+ Every setting is handled by the class `RailsSettings::SettingObject`. You can use your own class, e.g. for validations:
53
+
54
+ ```ruby
55
+ class Project < ActiveRecord::Base
56
+ has_settings :info, :class_name => 'ProjectSettingObject'
57
+ end
58
+
59
+ class ProjectSettingObject < RailsSettings::SettingObject
60
+ validate do
61
+ unless self.owner_name.present? && self.owner_name.is_a?(String)
62
+ errors.add(:base, "Owner name is missing")
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ In case you need to define settings separatedly for the same models, you can use the persistent option
69
+
70
+ ```ruby
71
+ module UserDashboardConcern
72
+ extend ActiveSupport::Concern
73
+
74
+ included do
75
+ has_settings persistent: true do |s|
76
+ s.key :dashboard
77
+ end
78
+ end
79
+ end
80
+
81
+ class User < ActiveRecord::Base
82
+ has_settings persistent: true do |s|
83
+ s.key :calendar
84
+ end
85
+ end
86
+ ```
87
+
88
+ ### Set settings
89
+
90
+ ```ruby
91
+ user = User.find(1)
92
+ user.settings(:dashboard).theme = 'black'
93
+ user.settings(:calendar).scope = 'all'
94
+ user.settings(:calendar).display = 'daily'
95
+ user.save! # saves new or changed settings, too
96
+ ```
97
+
98
+ or
99
+
100
+ ```ruby
101
+ user = User.find(1)
102
+ user.settings(:dashboard).update! :theme => 'black'
103
+ user.settings(:calendar).update! :scope => 'all', :display => 'daily'
104
+ ```
105
+
106
+
107
+ ### Get settings
108
+
109
+ ```ruby
110
+ user = User.find(1)
111
+ user.settings(:dashboard).theme
112
+ # => 'black
113
+
114
+ user.settings(:dashboard).view
115
+ # => 'monthly' (it's the default)
116
+
117
+ user.settings(:calendar).scope
118
+ # => 'all'
119
+ ```
120
+
121
+ ### Delete settings
122
+
123
+ ```ruby
124
+ user = User.find(1)
125
+ user.settings(:dashboard).update! :theme => nil
126
+
127
+ user.settings(:dashboard).view = nil
128
+ user.settings(:dashboard).save!
129
+ ```
130
+
131
+ ### Using scopes
132
+
133
+ ```ruby
134
+ User.with_settings
135
+ # => all users having any setting
136
+
137
+ User.without_settings
138
+ # => all users without having any setting
139
+
140
+ User.with_settings_for(:calendar)
141
+ # => all users having a setting for 'calendar'
142
+
143
+ User.without_settings_for(:calendar)
144
+ # => all users without having settings for 'calendar'
145
+ ```
146
+
147
+ ### Eager Loading
148
+ ```ruby
149
+ User.includes(:setting_objects)
150
+ # => Eager load setting_objects when querying many users
151
+ ```
152
+
153
+ ## Compatibility
154
+
155
+ Version 2 is a complete rewrite and has a new DSL, so it's **not** compatible with Version 1. In addition, Rails 2.3 is not supported anymore. But the database schema is unchanged, so you can continue to use the data created by 1.x, no conversion is needed.
156
+
157
+ If you don't want to upgrade, you find the old version in the [1.x](https://github.com/ledermann/rails-settings/commits/1.x) branch. But don't expect any updates there.
158
+
159
+
160
+ ## Changelog
161
+
162
+ See https://github.com/ledermann/rails-settings/releases
163
+
164
+
165
+ ## License
166
+
167
+ MIT License
168
+
169
+ Copyright (c) 2012-2019 [Georg Ledermann](http://www.georg-ledermann.de)
170
+
171
+ This gem is a complete rewrite of [rails-settings](https://github.com/Squeegy/rails-settings) by [Alex Wayne](https://github.com/Squeegy)
data/Rakefile CHANGED
@@ -1,11 +1,6 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'lib' << 'test'
5
- t.test_files = Dir.glob("test/**/*_test.rb")
6
- t.verbose = true
7
- end
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
8
3
 
9
- task :default => :test
4
+ RSpec::Core::RakeTask.new(:spec)
10
5
 
11
- require 'bundler/gem_tasks'
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.2.11'
4
+ gem 'protected_attributes' if ENV['PROTECTED_ATTRIBUTES'] == 'true'
5
+ gem 'sqlite3', '~> 1.3.6'
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 5.0.7'
4
+ gem 'sqlite3', '~> 1.3.6'
5
+
6
+ gemspec :path => "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 5.1.7'
4
+ gem "sqlite3", "~> 1.3", ">= 1.3.6"
5
+
6
+ gemspec :path => "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 5.2.3'
4
+ gem "sqlite3", "~> 1.3", ">= 1.3.6"
5
+
6
+ gemspec :path => "../"