real_settings 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +8 -0
- data/LICENSE +22 -0
- data/README.md +164 -0
- data/Rakefile +2 -0
- data/lib/generators/real_settings/.DS_Store +0 -0
- data/lib/generators/real_settings/USAGE +9 -0
- data/lib/generators/real_settings/install/.DS_Store +0 -0
- data/lib/generators/real_settings/install/install_generator.rb +26 -0
- data/lib/generators/real_settings/install/templates/migration.rb +17 -0
- data/lib/generators/real_settings/install/templates/real_settings.rb +4 -0
- data/lib/real_settings/owner_settings.rb +85 -0
- data/lib/real_settings/relations.rb +14 -0
- data/lib/real_settings/settings.rb +103 -0
- data/lib/real_settings/version.rb +3 -0
- data/lib/real_settings.rb +4 -0
- data/real_settings.gemspec +17 -0
- data/spec/real_settings_spec.rb +150 -0
- data/spec/spec_helper.rb +36 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Macrow
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# RealSettings
|
2
|
+
|
3
|
+
RealSettings is a real settings tool for Rails3.
|
4
|
+
|
5
|
+
RealSetting load default settings from config file, and store settings in memory.
|
6
|
+
|
7
|
+
RealSetting can store settings in database.
|
8
|
+
|
9
|
+
RealSetting can store different settings for your model(activerecord model only).
|
10
|
+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'real_settings'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install real_settings
|
25
|
+
|
26
|
+
Run install command and migrate database:
|
27
|
+
|
28
|
+
$ rails g real_settings:install
|
29
|
+
$ rake db:migrate
|
30
|
+
|
31
|
+
## Configuration in config/initializers/real_settings.rb
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Settings.config do
|
35
|
+
settings.app_name = "My Application Name"
|
36
|
+
settings.app_url = "http://www.my-application.com"
|
37
|
+
default_meta_keywords = "default meta keywords"
|
38
|
+
default_meta_description = "default meta description"
|
39
|
+
# add some settings here
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Features & Usage
|
44
|
+
|
45
|
+
### RealSettings load default settings from config file
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
Settings.config do
|
49
|
+
settings.app_name = "My Application Name"
|
50
|
+
settings.app_url = "http://www.my-application.com"
|
51
|
+
default_meta_keywords = "default meta keywords"
|
52
|
+
default_meta_description = "default meta description"
|
53
|
+
......
|
54
|
+
end
|
55
|
+
|
56
|
+
Settings.app_name # => "real_settings"
|
57
|
+
Settings.app_url # => "http://www.real-settings.com"
|
58
|
+
Settings.default_meta_keywords # => "default meta keywords"
|
59
|
+
Settings.default_meta_description # => "default meta description"
|
60
|
+
```
|
61
|
+
|
62
|
+
### RealSettings can write settings into database
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Settings.admin_name # => nil
|
66
|
+
Settings.admin_name = "Macrow"
|
67
|
+
Settings.save!
|
68
|
+
```
|
69
|
+
|
70
|
+
### Support has_settings method
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class User < ActiveReocrd::Base
|
74
|
+
has_settings
|
75
|
+
end
|
76
|
+
|
77
|
+
user = User.first
|
78
|
+
user.settings.to_hash # => {}
|
79
|
+
user.settings.email # => nil
|
80
|
+
user.settings.email = "Macrow_wh@163.com"
|
81
|
+
user.settings.email # => "Macrow_wh@163.com"
|
82
|
+
user.settings.to_hash # => { :email => "Macrow_wh@163.com" }
|
83
|
+
user.save! # save email settings in database
|
84
|
+
|
85
|
+
another_user = User.last
|
86
|
+
another_user.settings.to_hash # => {}
|
87
|
+
another_user.settings.email # => nil
|
88
|
+
```
|
89
|
+
|
90
|
+
### has_settings method with default values
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class User < ActiveRecord::Base
|
94
|
+
has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
|
95
|
+
end
|
96
|
+
|
97
|
+
user = User.first
|
98
|
+
user.settings.to_hash # => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
|
99
|
+
user.settings.email # => nil
|
100
|
+
user.settings.email = "Macrow_wh@163.com"
|
101
|
+
user.settings.email # => "Macrow_wh@163.com"
|
102
|
+
user.settings # => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4', :email => "Macrow_wh@163.com" }
|
103
|
+
user.save! # save email settings in database
|
104
|
+
|
105
|
+
another_user = User.last
|
106
|
+
another_user.settings.to_hash # => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
|
107
|
+
another_user.settings.email # => nil
|
108
|
+
```
|
109
|
+
|
110
|
+
### RealSettings load all settings in memory for performance in first query action
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
Settings.admin_name # => nil
|
114
|
+
Settings.admin_name = "Macrow"
|
115
|
+
Settings.admin_name # => "Macrow"
|
116
|
+
Settings.reload!
|
117
|
+
Settings.admin_name # => nil
|
118
|
+
|
119
|
+
Settings.admin_name = "Macrow"
|
120
|
+
Settings.save!
|
121
|
+
Settings.admin_name # => "Macrow"
|
122
|
+
Settings.reload!
|
123
|
+
Settings.admin_name # => "Macrow"
|
124
|
+
```
|
125
|
+
|
126
|
+
### Editing Global Settings
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
# Edit Form:
|
130
|
+
form_for(Settings) do |f|
|
131
|
+
f.text_field :app_name
|
132
|
+
f.text_field :app_url
|
133
|
+
......
|
134
|
+
end
|
135
|
+
|
136
|
+
# Update Action:
|
137
|
+
Settings.update_attributes(params[:settings])
|
138
|
+
```
|
139
|
+
|
140
|
+
### Editing User's settings
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
# config/routes.rb
|
144
|
+
resources :users do
|
145
|
+
get 'settings/edit' => 'users#edit_settings'
|
146
|
+
put 'settings/update' => 'users#update_settings'
|
147
|
+
end
|
148
|
+
|
149
|
+
# User's settings Edit Form:
|
150
|
+
form_for @user.settings, :as => :settings, :url => user_settings_update_path(@user), :method => :put do |f|
|
151
|
+
f.text_field :notebook
|
152
|
+
f.text_field :mobile
|
153
|
+
......
|
154
|
+
end
|
155
|
+
|
156
|
+
# Update Action:
|
157
|
+
@user = User.find(params[:user_id])
|
158
|
+
@user.settings.update_attributes(params[:settings])
|
159
|
+
```
|
160
|
+
|
161
|
+
|
162
|
+
## License
|
163
|
+
|
164
|
+
MIT License.
|
data/Rakefile
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RealSettings
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
desc "Install RealSettings for your application."
|
6
|
+
|
7
|
+
def copy_initializers_files
|
8
|
+
template "real_settings.rb", "config/initializers/real_settings.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
def copy_migration_files
|
12
|
+
migration_template "migration.rb", "db/migrate/create_real_settings.rb"
|
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
|
+
|
23
|
+
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateRealSettings < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :settings, :force => true do |t|
|
4
|
+
t.string :key, :null => false
|
5
|
+
t.text :value
|
6
|
+
t.integer :target_id
|
7
|
+
t.string :target_type
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :settings, [ :target_type, :target_id, :key ], :unique => true
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :settings
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
class OwnerSettings < Settings
|
2
|
+
@@__owner_settings, @@__owner_db_settings, @@__owner_temp_settings, @@__owner_loaded, @@__owner_default_settings = {}, {}, {}, {}, {}
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def get_settings(owner, defaults)
|
6
|
+
@owner = owner
|
7
|
+
@@__owner_settings[owner_symbol] ||= {}
|
8
|
+
@@__owner_db_settings[owner_symbol] ||= {}
|
9
|
+
@@__owner_temp_settings[owner_symbol] ||= {}
|
10
|
+
@@__owner_default_settings[owner_type_symbol] ||= ( defaults || {} )
|
11
|
+
if @@__owner_loaded[owner_symbol].nil?
|
12
|
+
reload!
|
13
|
+
@@__owner_loaded[owner_symbol] = true
|
14
|
+
end
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_hash
|
19
|
+
@@__owner_default_settings[owner_type_symbol].merge(__settings)
|
20
|
+
end
|
21
|
+
|
22
|
+
def target_id
|
23
|
+
@owner.id
|
24
|
+
end
|
25
|
+
|
26
|
+
def target_type
|
27
|
+
@owner.class.base_class.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
def owner_type_symbol
|
31
|
+
target_type.downcase.to_sym
|
32
|
+
end
|
33
|
+
|
34
|
+
def owner_symbol
|
35
|
+
"#{target_type}_#{target_id}".downcase.to_sym
|
36
|
+
end
|
37
|
+
|
38
|
+
def reload!
|
39
|
+
super
|
40
|
+
__settings
|
41
|
+
end
|
42
|
+
|
43
|
+
def __settings
|
44
|
+
@@__owner_settings[owner_symbol]
|
45
|
+
end
|
46
|
+
|
47
|
+
def __settings=(value)
|
48
|
+
@@__owner_settings[owner_symbol] = value
|
49
|
+
end
|
50
|
+
|
51
|
+
def __file_settings
|
52
|
+
{}
|
53
|
+
end
|
54
|
+
|
55
|
+
def __file_settings=(value)
|
56
|
+
{}
|
57
|
+
end
|
58
|
+
|
59
|
+
def __db_settings
|
60
|
+
@@__owner_db_settings[owner_symbol]
|
61
|
+
end
|
62
|
+
|
63
|
+
def __db_settings=(value)
|
64
|
+
@@__owner_db_settings[owner_symbol] = value
|
65
|
+
end
|
66
|
+
|
67
|
+
def __temp_settings
|
68
|
+
@@__owner_temp_settings[owner_symbol]
|
69
|
+
end
|
70
|
+
|
71
|
+
def __temp_settings=(value)
|
72
|
+
@@__owner_temp_settings[owner_symbol] = value
|
73
|
+
end
|
74
|
+
|
75
|
+
protected
|
76
|
+
|
77
|
+
def __hook_for_load_owner_default_settings(key)
|
78
|
+
@@__owner_default_settings[owner_type_symbol][key]
|
79
|
+
end
|
80
|
+
|
81
|
+
def __hook_for_delete_owner_default_settings(key, value)
|
82
|
+
value == @@__owner_default_settings[owner_type_symbol][key]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
ActiveRecord::Base.class_eval do
|
2
|
+
def self.has_settings(*args)
|
3
|
+
cattr_accessor :__default_settings_options
|
4
|
+
self.__default_settings_options = args.extract_options![:defaults]
|
5
|
+
|
6
|
+
class_eval do
|
7
|
+
def settings
|
8
|
+
OwnerSettings.get_settings(self, __default_settings_options)
|
9
|
+
end
|
10
|
+
|
11
|
+
after_destroy { |owner| owner.settings.target_settings.each { |s| s.destroy } }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
class Settings < ActiveRecord::Base
|
2
|
+
attr_accessible :key, :value, :target_id, :target_type
|
3
|
+
@@__settings, @@__file_settings, @@__db_settings, @@__temp_settings = {}, {}, {}, {}
|
4
|
+
cattr_accessor :__settings, :__file_settings, :__db_settings, :__temp_settings
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def config
|
8
|
+
yield self
|
9
|
+
self.__file_settings = __temp_settings
|
10
|
+
reload!
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_hash
|
14
|
+
__settings
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_attributes(attributes)
|
18
|
+
if attributes.nil?
|
19
|
+
false
|
20
|
+
else
|
21
|
+
attributes.each { |key, value| send("#{key}=", value) }
|
22
|
+
save!
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def reload!
|
27
|
+
self.__temp_settings = {}
|
28
|
+
load!
|
29
|
+
end
|
30
|
+
|
31
|
+
def save!
|
32
|
+
__file_settings.each { |key, value| __settings.delete(key) }
|
33
|
+
__settings.delete_if { |key, value| value == __db_settings[key] || __hook_for_delete_owner_default_settings(key, value) }
|
34
|
+
__settings.each do |key, value|
|
35
|
+
if __db_settings.has_key?(key)
|
36
|
+
where(:key => key).first.update_attribute(:value, value)
|
37
|
+
else
|
38
|
+
create!(:key => key, :value => value, :target_id => target_id, :target_type => target_type)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
__db_settings.merge!(__settings)
|
42
|
+
__temp_settings = {}
|
43
|
+
calculate_settings
|
44
|
+
end
|
45
|
+
|
46
|
+
def target_id
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
|
50
|
+
def target_type
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def target_settings
|
55
|
+
where(:target_type => target_type).where(:target_id => target_id)
|
56
|
+
end
|
57
|
+
|
58
|
+
def method_missing(name, *args)
|
59
|
+
if self.respond_to?(name)
|
60
|
+
super(name, args)
|
61
|
+
else
|
62
|
+
if name =~ /\w+=$/
|
63
|
+
__temp_settings[name.to_s.downcase.gsub('=', '').to_sym] = args.first
|
64
|
+
__settings[name.to_s.downcase.gsub('=', '').to_sym] = args.first
|
65
|
+
elsif name =~ /\w+/
|
66
|
+
__settings[name.to_s.downcase.to_sym] || __hook_for_load_owner_default_settings(name.to_s.downcase.to_sym)
|
67
|
+
else
|
68
|
+
raise NoMethodError
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
protected
|
74
|
+
|
75
|
+
def __hook_for_load_owner_default_settings(key)
|
76
|
+
nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def __hook_for_delete_owner_default_settings(key, value)
|
80
|
+
false
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def load!
|
86
|
+
load_from_database!
|
87
|
+
calculate_settings
|
88
|
+
end
|
89
|
+
|
90
|
+
def calculate_settings # load settings priority is file > database > temp
|
91
|
+
self.__settings = self.__temp_settings.merge(self.__db_settings).merge(self.__file_settings)
|
92
|
+
end
|
93
|
+
|
94
|
+
def load_from_database!
|
95
|
+
begin
|
96
|
+
__db_settings = {}
|
97
|
+
where(:target_type => target_type, :target_id => target_id).order(:key).all.each { |s| self.__db_settings[s.key.to_sym] = s.value }
|
98
|
+
rescue
|
99
|
+
__db_settings = {}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/real_settings/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Macrow"]
|
6
|
+
gem.email = ["Macrow_wh@163.com"]
|
7
|
+
gem.description = %q{A real settings tool for Rails3}
|
8
|
+
gem.summary = %q{A real settings tool for Rails3}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "real_settings"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RealSettings::VERSION
|
17
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "RealSettings" do
|
4
|
+
setup_db
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@app_name = "real_settings"
|
8
|
+
@app_url = "http://github.real_settings.com"
|
9
|
+
Settings.config do |settings|
|
10
|
+
settings.app_name = @app_name
|
11
|
+
settings.app_url = @app_url
|
12
|
+
end
|
13
|
+
@user1 = User.create!(:name => 'admin1')
|
14
|
+
@user2 = User.create!(:name => 'admin2')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "load default settings from config file" do
|
18
|
+
Settings.app_name.should == @app_name
|
19
|
+
Settings.app_url.should == @app_url
|
20
|
+
end
|
21
|
+
|
22
|
+
it "load settings priority is file > database > temp" do
|
23
|
+
Settings.create(:key => 'app_name', :value => 'name from database')
|
24
|
+
Settings.create(:key => 'app_url', :value => 'url from database')
|
25
|
+
Settings.reload!
|
26
|
+
Settings.app_name.should == @app_name
|
27
|
+
Settings.app_url.should == @app_url
|
28
|
+
|
29
|
+
Settings.app_name = 'new name'
|
30
|
+
Settings.app_name.should == 'new name'
|
31
|
+
Settings.reload!
|
32
|
+
Settings.app_name.should == @app_name
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
it "load default settings from database" do
|
37
|
+
Settings.create(:key => 'another_name', :value => 'name from database')
|
38
|
+
Settings.create(:key => 'another_url', :value => 'url from database')
|
39
|
+
Settings.reload!
|
40
|
+
Settings.another_name.should == 'name from database'
|
41
|
+
Settings.another_url.should == 'url from database'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "reload feature" do
|
45
|
+
Settings.new_name.should == nil
|
46
|
+
Settings.new_name = 'new name'
|
47
|
+
Settings.new_name.should == 'new name'
|
48
|
+
Settings.reload!
|
49
|
+
Settings.new_name.should == nil
|
50
|
+
end
|
51
|
+
|
52
|
+
it "save settings into database" do
|
53
|
+
Settings.new_name.should == nil
|
54
|
+
Settings.new_name = 'new name'
|
55
|
+
Settings.save!
|
56
|
+
Settings.new_name.should == 'new name'
|
57
|
+
Settings.reload!
|
58
|
+
Settings.new_name.should == 'new name'
|
59
|
+
|
60
|
+
Settings.create(:key => 'another_name', :value => 'name from database')
|
61
|
+
Settings.another_name = 'new name'
|
62
|
+
Settings.save!
|
63
|
+
Settings.another_name.should == 'new name'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "has_settings without default values" do
|
67
|
+
@account1 = Account.create!(:name => 'account1')
|
68
|
+
@account1.settings.to_hash.should == {}
|
69
|
+
end
|
70
|
+
|
71
|
+
it "user has settings different with global settings" do
|
72
|
+
@user1.settings.editor = 'textmate'
|
73
|
+
@user1.settings.editor.should == 'textmate'
|
74
|
+
@user1.settings.reload!
|
75
|
+
@user1.settings.editor.should == nil
|
76
|
+
@user1.settings.editor = 'textmate'
|
77
|
+
@user1.settings.save!
|
78
|
+
@user1.settings.reload!
|
79
|
+
@user1.settings.editor.should == 'textmate'
|
80
|
+
|
81
|
+
Settings.editor.should == nil
|
82
|
+
end
|
83
|
+
|
84
|
+
it "user has same settings with global settings" do
|
85
|
+
@user1.settings.app_name.should == nil
|
86
|
+
@user1.settings.app_name = 'app name for user'
|
87
|
+
@user1.settings.save!
|
88
|
+
@user1.settings.app_name.should == 'app name for user'
|
89
|
+
Settings.app_name.should == @app_name
|
90
|
+
end
|
91
|
+
|
92
|
+
it "diferent users have same settings" do
|
93
|
+
@user1.settings.editor = 'textmate'
|
94
|
+
@user2.settings.editor = 'vim'
|
95
|
+
@user1.settings.editor.should == 'textmate'
|
96
|
+
@user2.settings.editor.should == 'vim'
|
97
|
+
@user1.settings.reload!
|
98
|
+
@user2.settings.reload!
|
99
|
+
@user1.settings.editor.should == nil
|
100
|
+
@user2.settings.editor.should == nil
|
101
|
+
|
102
|
+
@user1.settings.editor = 'textmate'
|
103
|
+
@user2.settings.editor = 'vim'
|
104
|
+
@user1.settings.save!
|
105
|
+
@user2.settings.save!
|
106
|
+
@user1.settings.editor.should == 'textmate'
|
107
|
+
@user2.settings.editor.should == 'vim'
|
108
|
+
end
|
109
|
+
|
110
|
+
it "diferent users have different settings" do
|
111
|
+
@user1.settings.editor = 'textmate'
|
112
|
+
@user2.settings.editor.should == nil
|
113
|
+
@user1.settings.save!
|
114
|
+
@user2.settings.editor.should == nil
|
115
|
+
end
|
116
|
+
|
117
|
+
it "user has default settings" do
|
118
|
+
# has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
|
119
|
+
@user1.settings.to_hash[:notebook].should == 'Macbook Pro'
|
120
|
+
@user1.settings.to_hash[:mobile].should == 'iPhone 4'
|
121
|
+
@user1.settings.notebook.should == 'Macbook Pro'
|
122
|
+
@user1.settings.mobile.should == 'iPhone 4'
|
123
|
+
@user1.settings.notebook = 'Macbook Air'
|
124
|
+
@user1.settings.mobile = 'iPhone 4S'
|
125
|
+
@user1.settings.notebook.should == 'Macbook Air'
|
126
|
+
@user1.settings.mobile.should == 'iPhone 4S'
|
127
|
+
@user1.settings.reload!
|
128
|
+
@user1.settings.notebook.should == 'Macbook Pro'
|
129
|
+
@user1.settings.mobile.should == 'iPhone 4'
|
130
|
+
|
131
|
+
@user1.settings.notebook = 'Macbook Air'
|
132
|
+
@user1.settings.mobile = 'iPhone 4S'
|
133
|
+
@user1.settings.save!
|
134
|
+
@user1.settings.notebook.should == 'Macbook Air'
|
135
|
+
@user1.settings.mobile.should == 'iPhone 4S'
|
136
|
+
@user1.settings.reload!
|
137
|
+
@user1.settings.notebook.should == 'Macbook Air'
|
138
|
+
@user1.settings.mobile.should == 'iPhone 4S'
|
139
|
+
|
140
|
+
@user2.settings.notebook.should == 'Macbook Pro'
|
141
|
+
@user2.settings.mobile.should == 'iPhone 4'
|
142
|
+
end
|
143
|
+
|
144
|
+
it "be sure destroy settings after destroy user" do
|
145
|
+
@user1.settings.new_feature = "new setting for user"
|
146
|
+
@user1.settings.save!
|
147
|
+
@user1.destroy
|
148
|
+
Settings.where(:key => 'new_feature').all.should be_empty
|
149
|
+
end
|
150
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_record'
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'real_settings')
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
7
|
+
ActiveRecord::Migration.verbose = false
|
8
|
+
|
9
|
+
class User < ActiveRecord::Base
|
10
|
+
has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
|
11
|
+
end
|
12
|
+
|
13
|
+
class Account < ActiveRecord::Base
|
14
|
+
has_settings
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup_db
|
18
|
+
ActiveRecord::Schema.define(:version => 1) do
|
19
|
+
create_table :settings do |t|
|
20
|
+
t.string :key, :null => false
|
21
|
+
t.text :value, :null => true
|
22
|
+
t.integer :target_id, :null => true
|
23
|
+
t.string :target_type, :null => true
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
add_index :settings, [ :target_type, :target_id, :key ], :unique => true
|
27
|
+
|
28
|
+
create_table :users do |t|
|
29
|
+
t.string :name
|
30
|
+
end
|
31
|
+
|
32
|
+
create_table :accounts do |t|
|
33
|
+
t.string :name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: real_settings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Macrow
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A real settings tool for Rails3
|
15
|
+
email:
|
16
|
+
- Macrow_wh@163.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/generators/real_settings/.DS_Store
|
27
|
+
- lib/generators/real_settings/USAGE
|
28
|
+
- lib/generators/real_settings/install/.DS_Store
|
29
|
+
- lib/generators/real_settings/install/install_generator.rb
|
30
|
+
- lib/generators/real_settings/install/templates/migration.rb
|
31
|
+
- lib/generators/real_settings/install/templates/real_settings.rb
|
32
|
+
- lib/real_settings.rb
|
33
|
+
- lib/real_settings/owner_settings.rb
|
34
|
+
- lib/real_settings/relations.rb
|
35
|
+
- lib/real_settings/settings.rb
|
36
|
+
- lib/real_settings/version.rb
|
37
|
+
- real_settings.gemspec
|
38
|
+
- spec/real_settings_spec.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
homepage: ''
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.24
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A real settings tool for Rails3
|
64
|
+
test_files:
|
65
|
+
- spec/real_settings_spec.rb
|
66
|
+
- spec/spec_helper.rb
|