ledermann-rails-settings 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7aa1af77c8b2aba3819e77ba00790f313f143e69
4
+ data.tar.gz: c4d85b477105c7f86dae04d5fb5b9245506fd1a2
5
+ SHA512:
6
+ metadata.gz: 883960f19b067da03bc8f54d3de59d82d8640eeeb44d31b5ada5949a552a43027f59bec7b3854e9f9bc341c10126deb3c1714c0a6b6d4a5433639a9a1f53ebdb
7
+ data.tar.gz: 9786e48162d45e3a07020d268dc8dd9cbab7a2d5dd2bcc290a5ac47121f27bd4d0601fe75054d009773a95bf644390546aeaaf8b8425f5735a7eb06c02cdc2cb
data/.travis.yml CHANGED
@@ -2,8 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.8.7
4
4
  - 1.9.3
5
+ - 2.0.0
5
6
  gemfile:
6
- - ci/Gemfile.rails-2.3.x
7
- # - ci/Gemfile.rails-3.0.x
8
7
  - ci/Gemfile.rails-3.1.x
9
- - ci/Gemfile.rails-3.2.x
8
+ - ci/Gemfile.rails-3.2.x
data/Changelog.md CHANGED
@@ -1,17 +1,38 @@
1
+ Version 2.0.0 (2013-03-07)
2
+
3
+ - Complete rewrite
4
+ - New DSL (see README.md)
5
+ - No global defaults anymore. Defaults are defined per ActiveRecord class
6
+ - No more storing defaults in the database
7
+ - Rails >= 3.1 needed (Rails 2.3 not supported anymore)
8
+ - Threadsafe
9
+ - Switched to RSpec for testing
10
+
11
+
12
+ Version 1.2.1 (2013-02-09)
13
+
14
+ - Quick and dirty fix for design flaw in target scope implementation (thanks to Yves-Eric Martin)
15
+ - Use Thread.current instead of cattr_accessor to be threadsafe
16
+ - Code cleanup
17
+
18
+
1
19
  Version 1.2.0 (2012-07-21)
2
20
 
3
21
  - Added model-level settings (thanks to Jim Ryan)
4
22
 
23
+
5
24
  Version 1.1.0 (2012-06-01)
6
25
 
7
26
  - Added caching (thanks to Matthew McEachen)
8
27
 
28
+
9
29
  Version 1.0.1 (2011-11-05)
10
30
 
11
31
  - Gemspec: Fixed missing dependency
12
32
 
33
+
13
34
  Version 1.0.0 (2011-11-05)
14
35
 
15
36
  - Conversion from Plugin to Gem
16
37
  - Destroying false values (thanks to @Pavling)
17
- - Testing with Travis
38
+ - Testing with Travis
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) 2013 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,133 @@
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 3
2
+
3
+ [![Build Status](https://secure.travis-ci.org/ledermann/rails-settings.png)](http://travis-ci.org/ledermann/rails-settings)
4
+ [![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.png)](https://codeclimate.com/github/ledermann/rails-settings)
5
+
6
+ Ruby gem to handle settings for ActiveRecord objects by storing them as serialized Hash in a separate database table. Optional: Defaults and Namespaces.
7
+
8
+ **BEWARE: This is version 2 which is a complete rewrite and NOT compatible with version 1.x**
9
+
10
+
11
+ ## Example
12
+
13
+ ### Defining settings
14
+
15
+ ```ruby
16
+ class User < ActiveRecord::Base
17
+ has_settings do |s|
18
+ s.key :dashboard, :defaults => { :theme => 'blue', :view => 'monthly', :filter => false }
19
+ s.key :calendar, :defaults => { :scope => 'company'}
20
+ end
21
+ end
22
+ ```
23
+
24
+ If no defaults are needed, a simplified syntax can be used:
25
+
26
+ ```ruby
27
+ class User < ActiveRecord::Base
28
+ has_settings :dashboard, :calendar
29
+ end
30
+ ```
31
+
32
+ Every setting is handled by the class `RailsSettings::SettingObject`. You can use your own class, e.g. for validations:
33
+
34
+ ```ruby
35
+ class Project < ActiveRecord::Base
36
+ has_settings :info, :class_name => 'ProjectSettingObject'
37
+ end
38
+
39
+ class ProjectSettingObject < RailsSettings::SettingObject
40
+ validate do
41
+ unless self.owner_name.present? && self.owner_name.is_a?(String)
42
+ errors.add(:base, "Owner name is missing")
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ ### Set settings
49
+
50
+ ```ruby
51
+ user = User.find(1)
52
+ user.settings(:dashboard).theme = 'black'
53
+ user.settings(:calendar).scope = 'all'
54
+ user.settings(:calendar).display = 'daily'
55
+ user.save! # saves new or changed settings, too
56
+ ```
57
+
58
+ or
59
+
60
+ ```ruby
61
+ user = User.find(1)
62
+ user.settings(:dashboard).update_attributes! :theme => 'black'
63
+ user.settings(:calendar).update_attributes! :scope => 'all', :display => 'dialy'
64
+ ```
65
+
66
+
67
+ ### Get settings
68
+
69
+ ```ruby
70
+ user = User.find(1)
71
+ user.settings(:dashboard).theme
72
+ # => 'black
73
+
74
+ user.settings(:dashboard).view
75
+ # => 'monthly' (it's the default)
76
+
77
+ user.settings(:calendar).scope
78
+ # => 'all'
79
+ ```
80
+
81
+
82
+ ### Using scopes
83
+
84
+ ```ruby
85
+ User.with_settings
86
+ # => all users having any setting
87
+
88
+ User.without_settings
89
+ # => all users without having any setting
90
+
91
+ User.with_settings_for(:calendar)
92
+ # => all users having a setting for 'calender'
93
+
94
+ User.without_settings_for(:calendar)
95
+ # => all users without having settings for 'calendar'
96
+ ```
97
+
98
+
99
+ ## Requirements
100
+
101
+ Rails 3.1.x or 3.2.x
102
+ Ruby 1.8.7, 1.9.3 or 2.0.0
103
+
104
+
105
+ ## Installation
106
+
107
+ Include the gem in your Gemfile:
108
+
109
+ ```ruby
110
+ gem 'ledermann-rails-settings', :require => 'rails-settings'
111
+ ```
112
+
113
+ Generate and run the migration:
114
+
115
+ ```shell
116
+ rails g rails_settings:migration
117
+ rake db:migrate
118
+ ```
119
+
120
+ ## Compatibility
121
+
122
+ Version 2 is a complete rewrite and has a new DSL, so it's **not** compatible with Version 1. But the database schema is unchanged, so you can use the same data after upgrading.
123
+ In addition, Rails 2.3 is not supported anymore.
124
+
125
+ 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.
126
+
127
+
128
+ ## License
129
+
130
+ MIT License
131
+ Copyright (c) 2013 Georg Ledermann
132
+
133
+ 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
@@ -1,5 +1,6 @@
1
1
  source :rubygems
2
2
 
3
- gem 'activerecord', '~> 3.1.4'
4
- gem 'sqlite3'
5
- gem 'rake'
3
+ gem 'activerecord', '~> 3.1.0'
4
+ gem 'sqlite3', '~> 1.3'
5
+ gem 'rake', '~> 10.0'
6
+ gem 'rspec', '~> 2.13'
@@ -1,5 +1,6 @@
1
1
  source :rubygems
2
2
 
3
- gem 'activerecord', '~> 3.2.2'
4
- gem 'sqlite3'
5
- gem 'rake'
3
+ gem 'activerecord', '~> 3.2.0'
4
+ gem 'sqlite3', '~> 1.3'
5
+ gem 'rake', '~> 10.0'
6
+ gem 'rspec', '~> 2.13'
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module RailsSettings
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ desc "Generates migration for rails-settings"
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def create_migration_file
12
+ migration_template 'migration.rb', 'db/migrate/rails_settings_migration'
13
+ end
14
+
15
+ def self.next_migration_number(dirname)
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
18
+ else
19
+ "%.3d" % (current_migration_number(dirname) + 1)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ class RailsSettingsMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :settings do |t|
4
+ t.string :var, :null => false
5
+ t.text :value, :null => false
6
+ t.references :target, :null => false, :polymorphic => true
7
+ t.timestamps
8
+ end
9
+ add_index :settings, [ :target_type, :target_id, :var ], :unique => true
10
+ end
11
+
12
+ def self.down
13
+ drop_table :settings
14
+ end
15
+ end