rails_admin_settings 0.5.5 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +2 -1
- data/CHANGELOG.md +11 -0
- data/app/models/rails_admin_settings/setting.rb +27 -5
- data/app/views/rails_admin/main/_setting_value.html.haml +1 -1
- data/config/locales/{en.rails_admin_settings.yml → en.yml} +0 -0
- data/config/locales/{ru.rails_admin_settings.yml → ru.yml} +20 -20
- data/lib/rails_admin_settings/dumper.rb +12 -0
- data/lib/rails_admin_settings/engine.rb +3 -0
- data/lib/rails_admin_settings/fallback.rb +19 -0
- data/lib/rails_admin_settings/hex_color_validator.rb +1 -1
- data/lib/rails_admin_settings/namespaced.rb +201 -0
- data/lib/rails_admin_settings/rails_admin_config.rb +3 -6
- data/lib/rails_admin_settings/railtie.rb +7 -0
- data/lib/rails_admin_settings/settings.rb +63 -165
- data/lib/rails_admin_settings/tasks.rb +25 -0
- data/lib/rails_admin_settings/types.rb +15 -1
- data/lib/rails_admin_settings/validation.rb +2 -4
- data/lib/rails_admin_settings/version.rb +1 -1
- data/lib/rails_admin_settings.rb +4 -0
- data/rails_admin_settings.gemspec +8 -7
- data/spec/carrierwave_spec.rb +7 -2
- data/spec/database_trickery_spec.rb +5 -7
- data/spec/defaults_spec.rb +53 -0
- data/spec/enabling_spec.rb +29 -0
- data/spec/label_spec.rb +2 -2
- data/spec/namespaced_spec.rb +41 -0
- data/spec/paperclip_spec.rb +5 -0
- data/spec/settings_spec.rb +13 -4
- data/spec/spec_helper.rb +4 -0
- data/spec/support/defaults.yml +15 -0
- data/spec/support/defaults_w_file.yml +19 -0
- data/spec/support/mongoid.yml +1 -1
- data/spec/types_spec.rb +12 -9
- metadata +49 -19
@@ -1,208 +1,106 @@
|
|
1
1
|
require 'thread'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
# we are inheriting from BasicObject so we don't get a bunch of methods from
|
4
|
+
# Kernel or Object
|
5
|
+
class Settings < BasicObject
|
6
|
+
cattr_accessor :file_uploads_supported, :file_uploads_engine
|
5
7
|
@@file_uploads_supported = false
|
6
8
|
@@file_uploads_engine = false
|
7
|
-
@@
|
9
|
+
@@namespaces = {}
|
10
|
+
@@mutex = ::Mutex.new
|
8
11
|
|
9
12
|
class << self
|
10
|
-
|
11
|
-
@@settings = {}
|
12
|
-
|
13
|
-
def load!
|
14
|
-
@@mutex.synchronize do
|
15
|
-
if @@loaded
|
16
|
-
false
|
17
|
-
else
|
18
|
-
@@settings = {}
|
19
|
-
RailsAdminSettings::Setting.all.each do |setting|
|
20
|
-
@@settings[setting.key] = setting
|
21
|
-
end
|
22
|
-
@@loaded = true
|
23
|
-
true
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def unload!
|
29
|
-
@@mutex.synchronize do
|
30
|
-
if @@loaded
|
31
|
-
@@settings = {}
|
32
|
-
@@loaded = false
|
33
|
-
true
|
34
|
-
else
|
35
|
-
false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# returns processed setting value
|
41
|
-
def method_missing(key, *args)
|
42
|
-
key = key.to_s
|
43
|
-
|
44
|
-
if key[-1] == '='
|
45
|
-
key = key[0..-2]
|
46
|
-
options = args[1] || {}
|
47
|
-
value = args.first
|
48
|
-
set(key, value, options).val
|
49
|
-
else
|
50
|
-
get(key, args.first || {}).val
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def set(key, value = nil, options = {})
|
55
|
-
load!
|
56
|
-
key = key.to_s
|
13
|
+
def ns(name, options = {})
|
57
14
|
options.symbolize_keys!
|
58
|
-
|
59
|
-
|
60
|
-
if !options[:type].nil? && options[:type] == 'yaml' && !value.nil? && !valid_yaml?(value)
|
61
|
-
value = value.to_yaml
|
62
|
-
end
|
63
|
-
|
64
|
-
is_file = !options[:type].nil? && (options[:type] == 'image' || options[:type] == 'file')
|
65
|
-
if is_file
|
66
|
-
options[:raw] = ''
|
15
|
+
if name.nil?
|
16
|
+
name = 'main'
|
67
17
|
else
|
68
|
-
|
18
|
+
name = name.to_s
|
69
19
|
end
|
70
|
-
|
71
|
-
|
72
|
-
write_to_database(key, options.merge(key: key))
|
73
|
-
else
|
74
|
-
@@mutex.synchronize do
|
75
|
-
@@settings[key].update_attributes!(options)
|
76
|
-
end
|
20
|
+
@@mutex.synchronize do
|
21
|
+
@@namespaces[name] ||= ::RailsAdminSettings::Namespaced.new(name.to_s)
|
77
22
|
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
@@settings[key].save!
|
83
|
-
end
|
23
|
+
if options[:fallback].nil?
|
24
|
+
@@namespaces[name]
|
25
|
+
else
|
26
|
+
::RailsAdminSettings::Fallback.new(@@namespaces[name], options[:fallback])
|
84
27
|
end
|
85
|
-
|
86
|
-
@@settings[key]
|
87
28
|
end
|
88
29
|
|
89
|
-
def
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
e.message << " [rails_admin_settings] Please add gem 'safe_yaml' to your Gemfile to use yaml settings"
|
94
|
-
raise e
|
95
|
-
rescue Psych::SyntaxError => e
|
96
|
-
return false
|
30
|
+
def unload!
|
31
|
+
@@mutex.synchronize do
|
32
|
+
@@namespaces.values.map(&:unload!)
|
33
|
+
@@namespaces = {}
|
97
34
|
end
|
98
35
|
end
|
99
36
|
|
100
|
-
def
|
101
|
-
|
37
|
+
def destroy_all!
|
38
|
+
RailsAdminSettings::Setting.destroy_all
|
39
|
+
unload!
|
102
40
|
end
|
103
41
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
key = key.to_s
|
108
|
-
if @@settings[key].nil?
|
109
|
-
create_setting(key, options)
|
42
|
+
def root_file_path
|
43
|
+
if Object.const_defined?('Rails')
|
44
|
+
Rails.root
|
110
45
|
else
|
111
|
-
|
46
|
+
Pathname.new(File.dirname(__FILE__)).join('../..')
|
112
47
|
end
|
113
48
|
end
|
114
49
|
|
115
|
-
def
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
@@settings[key].val
|
50
|
+
def apply_defaults!(file)
|
51
|
+
if File.file?(file)
|
52
|
+
yaml = YAML.load(File.read(file), safe: true)
|
53
|
+
yaml.each_pair do |namespace, vals|
|
54
|
+
vals.symbolize_keys!
|
55
|
+
n = ns(namespace)
|
56
|
+
vals.each_pair do |key, val|
|
57
|
+
val.symbolize_keys!
|
58
|
+
if !val[:type].nil? && (val[:type] == 'file' || val[:type] == 'image')
|
59
|
+
unless @@file_uploads_supported
|
60
|
+
::Kernel.raise ::RailsAdminSettings::PersistenceException, "Fatal: setting #{key} is #{val[:type]} but file upload engine is not detected"
|
61
|
+
end
|
62
|
+
value = File.open(root_file_path.join(val.delete(:value)))
|
63
|
+
else
|
64
|
+
value = val.delete(:value)
|
65
|
+
end
|
66
|
+
n.set(key, value, val.merge(overwrite: false))
|
67
|
+
end
|
68
|
+
n.unload!
|
135
69
|
end
|
136
70
|
end
|
137
71
|
end
|
138
72
|
|
139
|
-
def
|
140
|
-
load!
|
141
|
-
key = key.to_s
|
73
|
+
def get(key, options = {})
|
142
74
|
options.symbolize_keys!
|
143
|
-
options[:raw] = options.delete(:default)
|
144
75
|
|
145
|
-
if
|
146
|
-
|
76
|
+
if options[:ns].nil? || options[:ns].to_s == 'main'
|
77
|
+
ns('main').get(key, options)
|
147
78
|
else
|
148
|
-
|
79
|
+
ns(options[:ns]).get(key, options)
|
149
80
|
end
|
150
81
|
end
|
151
82
|
|
152
|
-
|
153
|
-
|
154
|
-
['Settings']
|
155
|
-
end
|
156
|
-
|
157
|
-
def destroy(key)
|
158
|
-
raise 'please call destroy! to delete setting'
|
159
|
-
end
|
160
|
-
|
161
|
-
def destroy_all
|
162
|
-
raise 'please call destroy_all! to delete all settings'
|
163
|
-
end
|
164
|
-
|
165
|
-
def destroy!(key)
|
166
|
-
load!
|
167
|
-
key = key.to_s
|
83
|
+
def set(key, value = nil, options = {})
|
84
|
+
options.symbolize_keys!
|
168
85
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
end
|
86
|
+
if options[:ns].nil? || options[:ns].to_s == 'main'
|
87
|
+
ns('main').set(key, value, options)
|
88
|
+
else
|
89
|
+
ns(options[:ns]).set(key, value, options)
|
174
90
|
end
|
175
91
|
end
|
176
92
|
|
177
|
-
def
|
178
|
-
|
179
|
-
unload!
|
93
|
+
def save_default(key, value, options = {})
|
94
|
+
set(key, value, options.merge(overwrite: false))
|
180
95
|
end
|
181
96
|
|
182
|
-
|
183
|
-
|
184
|
-
key = key.to_s
|
185
|
-
|
186
|
-
@@mutex.synchronize do
|
187
|
-
@@settings[key] = RailsAdminSettings::Setting.create(options)
|
188
|
-
unless @@settings[key].persisted?
|
189
|
-
if @@settings[key].errors[:key].any?
|
190
|
-
@@settings[key] = RailsAdminSettings::Setting.where(key: key).first
|
191
|
-
if options[:raw].blank? && !@@settings[key].blank?
|
192
|
-
# do not update setting if it's not blank in DB and we want to make it blank
|
193
|
-
else
|
194
|
-
unless @@settings[key].update_attributes(options)
|
195
|
-
raise RailsAdminSettings::PersistenceException
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
@@settings[key]
|
201
|
-
end
|
97
|
+
def create_setting(key, value, options = {})
|
98
|
+
set(key, nil, options.merge(overwrite: false))
|
202
99
|
end
|
203
100
|
|
204
|
-
def
|
205
|
-
|
101
|
+
def method_missing(*args)
|
102
|
+
ns('main').__send__(*args)
|
206
103
|
end
|
207
104
|
end
|
208
105
|
end
|
106
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# require this file to load the tasks
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
# Require sitemap_generator at runtime. If we don't do this the ActionView helpers are included
|
5
|
+
# before the Rails environment can be loaded by other Rake tasks, which causes problems
|
6
|
+
# for those tasks when rendering using ActionView.
|
7
|
+
namespace :settings do
|
8
|
+
# Require sitemap_generator only. When installed as a plugin the require will fail, so in
|
9
|
+
# that case, load the environment first.
|
10
|
+
task :require do
|
11
|
+
Rake::Task['environment'].invoke
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Dump settings to config/settings.yml"
|
15
|
+
task :dump => ['settings:require'] do
|
16
|
+
path = Settings.root_file_path.join('config/settings.yml')
|
17
|
+
RailsAdminSettings::Dumper.dump(path)
|
18
|
+
puts "dumped settings to #{path}"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "Delete all settings"
|
22
|
+
task :delete => ['settings:require'] do
|
23
|
+
Settings.destroy_all!
|
24
|
+
end
|
25
|
+
end
|
@@ -1,5 +1,19 @@
|
|
1
1
|
module RailsAdminSettings
|
2
2
|
def self.types
|
3
|
-
[
|
3
|
+
[
|
4
|
+
'string',
|
5
|
+
'integer',
|
6
|
+
'html',
|
7
|
+
'sanitized',
|
8
|
+
'yaml',
|
9
|
+
'phone',
|
10
|
+
'email',
|
11
|
+
'address',
|
12
|
+
'file',
|
13
|
+
'image',
|
14
|
+
'url',
|
15
|
+
'domain',
|
16
|
+
'color'
|
17
|
+
]
|
4
18
|
end
|
5
19
|
end
|
@@ -5,7 +5,7 @@ module RailsAdminSettings
|
|
5
5
|
self.raw = default_value if raw.blank?
|
6
6
|
end
|
7
7
|
base.before_validation :sanitize_value, if: :sanitized_type?
|
8
|
-
base.validates_uniqueness_of :key
|
8
|
+
base.validates_uniqueness_of :key, scope: :ns
|
9
9
|
base.validates_inclusion_of :type, in: RailsAdminSettings.types
|
10
10
|
base.validates_numericality_of :raw, if: :integer_type?
|
11
11
|
|
@@ -52,9 +52,7 @@ module RailsAdminSettings
|
|
52
52
|
base.after_validation(:geocode, if: :address_type?)
|
53
53
|
end
|
54
54
|
|
55
|
-
base.
|
56
|
-
base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw)
|
57
|
-
end
|
55
|
+
base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw, if: :color_type?)
|
58
56
|
|
59
57
|
base.validate if: :yaml_type? do
|
60
58
|
require_safe_yaml do
|
data/lib/rails_admin_settings.rb
CHANGED
@@ -4,18 +4,22 @@ module RailsAdminSettings
|
|
4
4
|
class PersistenceException < Exception
|
5
5
|
end
|
6
6
|
|
7
|
+
autoload :Fallback, "rails_admin_settings/fallback"
|
8
|
+
autoload :Namespaced, "rails_admin_settings/namespaced"
|
7
9
|
autoload :Processing, "rails_admin_settings/processing"
|
8
10
|
autoload :Validation, "rails_admin_settings/validation"
|
9
11
|
autoload :RequireHelpers, "rails_admin_settings/require_helpers"
|
10
12
|
autoload :RailsAdminConfig, "rails_admin_settings/rails_admin_config"
|
11
13
|
autoload :Uploads, "rails_admin_settings/uploads"
|
12
14
|
autoload :HexColorValidator, "rails_admin_settings/hex_color_validator"
|
15
|
+
autoload :Dumper, "rails_admin_settings/dumper"
|
13
16
|
end
|
14
17
|
|
15
18
|
require "rails_admin_settings/types"
|
16
19
|
require "rails_admin_settings/settings"
|
17
20
|
|
18
21
|
if Object.const_defined?('Rails')
|
22
|
+
require "rails_admin_settings/railtie"
|
19
23
|
require "rails_admin_settings/engine"
|
20
24
|
else
|
21
25
|
require File.dirname(__FILE__) + '/../app/models/rails_admin_settings/setting.rb'
|
@@ -21,14 +21,14 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_runtime_dependency "mongoid", ">= 3.1"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler"
|
24
|
-
spec.add_development_dependency "rake", "~> 10.
|
25
|
-
spec.add_development_dependency "rspec", "~> 2.
|
26
|
-
spec.add_development_dependency "mongoid-rspec", "~> 1.
|
27
|
-
spec.add_development_dependency "simplecov", "~> 0.
|
28
|
-
spec.add_development_dependency "database_cleaner", "~>
|
29
|
-
spec.add_development_dependency "factory_girl", "~> 4.
|
24
|
+
spec.add_development_dependency "rake", "~> 10.1.1"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
26
|
+
spec.add_development_dependency "mongoid-rspec", "~> 1.10.0"
|
27
|
+
spec.add_development_dependency "simplecov", "~> 0.8.2"
|
28
|
+
spec.add_development_dependency "database_cleaner", "~> 1.2.0"
|
29
|
+
spec.add_development_dependency "factory_girl", "~> 4.3.0"
|
30
30
|
|
31
|
-
spec.add_development_dependency "safe_yaml", "~> 0.
|
31
|
+
spec.add_development_dependency "safe_yaml", "~> 1.0.0"
|
32
32
|
spec.add_development_dependency "russian_phone", "~> 0.3.2"
|
33
33
|
spec.add_development_dependency "sanitize", "~> 2.0.3"
|
34
34
|
spec.add_development_dependency "validates_email_format_of", "~> 1.5.3"
|
@@ -37,4 +37,5 @@ Gem::Specification.new do |spec|
|
|
37
37
|
|
38
38
|
spec.add_development_dependency "glebtv-carrierwave-mongoid"
|
39
39
|
spec.add_development_dependency "glebtv-mongoid-paperclip"
|
40
|
+
spec.add_development_dependency "pry"
|
40
41
|
end
|
data/spec/carrierwave_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe "Uploads" do
|
|
18
18
|
|
19
19
|
Settings.get(:file).file.file.file.should eq "#{File.dirname(__FILE__).gsub('/spec', '/')}uploads/1024x768.gif"
|
20
20
|
|
21
|
-
File.exists?(
|
21
|
+
File.exists?(Settings.root_file_path.join("uploads/1024x768.gif")).should be_true
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'supports image type' do
|
@@ -29,7 +29,12 @@ describe "Uploads" do
|
|
29
29
|
|
30
30
|
Settings.get(:file).file.file.file.should eq "#{File.dirname(__FILE__).gsub('/spec', '/')}uploads/1024x768.gif"
|
31
31
|
|
32
|
-
File.exists?(
|
32
|
+
File.exists?(Settings.root_file_path.join("uploads/1024x768.gif")).should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'supports defaults' do
|
36
|
+
Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults_w_file.yml'))
|
37
|
+
File.exists?(Settings.root_file_path.join("uploads/1024x768.gif")).should be_true
|
33
38
|
end
|
34
39
|
end
|
35
40
|
end
|
@@ -4,7 +4,7 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
# this tests check how well rails_admin_settings handles settings disappearing from DB during execution
|
6
6
|
# real usage: app specs with database_cleaner enabled
|
7
|
-
describe
|
7
|
+
describe 'Database trickery' do
|
8
8
|
|
9
9
|
it "should handle settings disappearing from DB" do
|
10
10
|
email = "my@mail.ru"
|
@@ -19,30 +19,28 @@ describe Settings do
|
|
19
19
|
Settings.email.should == email2
|
20
20
|
end
|
21
21
|
|
22
|
-
it "should handle settings appearing
|
22
|
+
it "should handle settings appearing in DB when settings are loaded" do
|
23
23
|
Settings.tst2.should == ''
|
24
|
-
RailsAdminSettings::Setting.create(key: 'tst', raw: 'tst')
|
24
|
+
RailsAdminSettings::Setting.create!(key: 'tst', raw: 'tst')
|
25
25
|
# settings are still cached, but when we try to create a setting it sees updated value in DB
|
26
26
|
Settings.tst.should == 'tst'
|
27
27
|
end
|
28
28
|
|
29
|
-
it "should handle settings appearing in DB" do
|
29
|
+
it "should handle settings appearing in DB when settings are not loaded" do
|
30
30
|
RailsAdminSettings::Setting.create(key: 'tst', raw: 'tst')
|
31
|
-
|
32
31
|
Settings.tst = 'str'
|
33
32
|
Settings.tst.should == 'str'
|
34
33
|
end
|
35
34
|
|
36
35
|
it "#destroy_all!" do
|
37
36
|
Settings.tst = 'str'
|
38
|
-
expect { Settings.destroy_all }.to raise_exception
|
39
37
|
Settings.destroy_all!
|
40
38
|
Settings.tst.should == ''
|
41
39
|
end
|
40
|
+
|
42
41
|
it "#destroy!" do
|
43
42
|
Settings.tst = 'str'
|
44
43
|
Settings.tst2 = 'str2'
|
45
|
-
expect { Settings.destroy(:tst) }.to raise_exception
|
46
44
|
Settings.destroy!(:tst)
|
47
45
|
Settings.tst.should == ''
|
48
46
|
Settings.tst2.should == 'str2'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Settings loading defaults' do
|
6
|
+
before :each do
|
7
|
+
Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults.yml'))
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'loads twice ok' do
|
11
|
+
Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults.yml'))
|
12
|
+
Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults.yml'))
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'sets value' do
|
16
|
+
Settings.footer.should eq 'test <b></b>'
|
17
|
+
Settings.get(:footer).type.should eq 'html'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'sets type' do
|
21
|
+
Settings.get(:phone).phone_type?.should be_true
|
22
|
+
Settings.get(:phone).val.city.should eq '906'
|
23
|
+
Settings.get(:phone).val.formatted_subscriber.should eq '111-11-11'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'sets enabled' do
|
27
|
+
Settings.phone_enabled?.should eq true
|
28
|
+
Settings.disabled_enabled?.should eq false
|
29
|
+
Settings.enabled?(:disabled).should eq false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'works with namespace' do
|
33
|
+
Settings.ns(:main).phone.should eq '906 1111111'
|
34
|
+
Settings.ns(:other).footer.should eq 'zzz'
|
35
|
+
Settings.footer.should eq 'test <b></b>'
|
36
|
+
Settings.ns(:main).footer.should eq 'test <b></b>'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'works with fallback' do
|
40
|
+
Settings.ns(:etc, fallback: :main).phone.should eq '906 1111111'
|
41
|
+
Settings.ns(:etc, fallback: :main).footer.should eq 'test <b></b>'
|
42
|
+
Settings.ns(:other, fallback: :main).footer.should eq 'zzz'
|
43
|
+
Settings.ns(:etc, fallback: :other).footer.should eq 'zzz'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "doesn't overwrite" do
|
47
|
+
Settings.ns(:main).phone = '906 2222222'
|
48
|
+
Settings.ns(:other).footer = 'xxx'
|
49
|
+
Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults.yml'))
|
50
|
+
Settings.ns(:main).phone.should eq '906 2222222'
|
51
|
+
Settings.ns(:other).footer.should eq 'xxx'
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Settings enabling and disabling' do
|
6
|
+
it 'works for name_enabled? and name_enabled =' do
|
7
|
+
Settings.zzz = '123'
|
8
|
+
Settings.zzz.should eq '123'
|
9
|
+
Settings.get(:zzz).enabled.should eq true
|
10
|
+
Settings.enabled?(:zzz).should eq true
|
11
|
+
Settings.zzz_enabled?.should eq true
|
12
|
+
|
13
|
+
Settings.zzz.should eq '123'
|
14
|
+
Settings.zzz_enabled = false
|
15
|
+
Settings.zzz_enabled?.should eq false
|
16
|
+
Settings.get(:zzz).enabled.should eq false
|
17
|
+
Settings.enabled?(:zzz).should eq false
|
18
|
+
Settings.zzz.should eq ''
|
19
|
+
Settings.unload!
|
20
|
+
Settings.zzz.should eq ''
|
21
|
+
Settings.get(:zzz).raw.should eq '123'
|
22
|
+
|
23
|
+
Settings.zzz_enabled = true
|
24
|
+
Settings.zzz.should eq '123'
|
25
|
+
Settings.zzz_enabled?.should eq true
|
26
|
+
Settings.get(:zzz).enabled.should eq true
|
27
|
+
Settings.enabled?(:zzz).should eq true
|
28
|
+
end
|
29
|
+
end
|
data/spec/label_spec.rb
CHANGED
@@ -7,12 +7,12 @@ describe 'Settings label' do
|
|
7
7
|
it "should have label" do
|
8
8
|
label = "E-Mail"
|
9
9
|
Settings.email(label: label, default: "my@mail.ru")
|
10
|
-
Settings.
|
10
|
+
Settings.get(:email).name.should == label
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should properly set label as key if blank" do
|
14
14
|
Settings.email(default: "my@mail.ru")
|
15
|
-
Settings.
|
15
|
+
Settings.get(:email).name.should == 'email'
|
16
16
|
end
|
17
17
|
|
18
18
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Namespaced settings' do
|
6
|
+
it 'sets namespaced' do
|
7
|
+
Settings.ns(:other).test = 'test'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'reads namespaced from cache' do
|
11
|
+
ns = Settings.ns(:other)
|
12
|
+
ns.test = 'test'
|
13
|
+
ns.test.should eq 'test'
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'reads namespaced from db' do
|
17
|
+
Settings.ns(:other).test = 'test'
|
18
|
+
Settings.ns(:other).test.should eq 'test'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'destroys' do
|
22
|
+
Settings.ns(:other).test = 'test'
|
23
|
+
Settings.ns(:other).destroy_all!
|
24
|
+
Settings.ns(:other).test.should eq ''
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'sets type' do
|
28
|
+
expect {
|
29
|
+
Settings.ns(:other).set(:phone, 'test', type: 'phone')
|
30
|
+
}.to raise_error
|
31
|
+
Settings.ns(:other).set(:phone, '906 111 11 11', type: 'phone')
|
32
|
+
Settings.get(:phone, ns: 'other').phone_type?.should be_true
|
33
|
+
Settings.get(:phone, ns: 'other').val.city.should eq '906'
|
34
|
+
Settings.get(:phone, ns: 'other').val.formatted_subscriber.should eq '111-11-11'
|
35
|
+
|
36
|
+
ns = Settings.ns(:other)
|
37
|
+
ns.get(:phone).phone_type?.should be_true
|
38
|
+
ns.get(:phone).val.city.should eq '906'
|
39
|
+
ns.get(:phone).val.formatted_subscriber.should eq '111-11-11'
|
40
|
+
end
|
41
|
+
end
|
data/spec/paperclip_spec.rb
CHANGED
@@ -29,6 +29,11 @@ describe "Uploads" do
|
|
29
29
|
|
30
30
|
File.exists?("#{File.dirname(__FILE__)}/../uploads/1024x768.gif").should be_true
|
31
31
|
end
|
32
|
+
|
33
|
+
it 'supports defaults' do
|
34
|
+
Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults_w_file.yml'))
|
35
|
+
File.exists?(Settings.root_file_path.join("uploads/1024x768.gif")).should be_true
|
36
|
+
end
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
data/spec/settings_spec.rb
CHANGED
@@ -2,8 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Settings do
|
6
|
-
|
5
|
+
describe 'Settings' do
|
7
6
|
it "should works as RailsSettings" do
|
8
7
|
Settings.destroy_all!
|
9
8
|
|
@@ -12,6 +11,18 @@ describe Settings do
|
|
12
11
|
Settings.email.should == email
|
13
12
|
end
|
14
13
|
|
14
|
+
it '#get should return new setting when setting does not exist' do
|
15
|
+
t = Settings.get(:test)
|
16
|
+
t.class.name.should eq 'RailsAdminSettings::Setting'
|
17
|
+
t.persisted?.should eq true
|
18
|
+
t.value.should eq ''
|
19
|
+
end
|
20
|
+
|
21
|
+
it '#name should return empty string when setting does not exist' do
|
22
|
+
Settings.test.should eq ''
|
23
|
+
Settings['test'].value.should eq ''
|
24
|
+
end
|
25
|
+
|
15
26
|
it "should save default" do
|
16
27
|
Settings.destroy_all!
|
17
28
|
|
@@ -26,7 +37,6 @@ describe Settings do
|
|
26
37
|
end
|
27
38
|
|
28
39
|
it 'should properly unload' do
|
29
|
-
Settings.class_eval { cattr_accessor :loaded }
|
30
40
|
Settings.load!
|
31
41
|
Settings.loaded.should eq true
|
32
42
|
Settings.unload!
|
@@ -34,7 +44,6 @@ describe Settings do
|
|
34
44
|
end
|
35
45
|
|
36
46
|
it 'should properly store settings to DB' do
|
37
|
-
Settings.class_eval { cattr_accessor :loaded }
|
38
47
|
Settings.unload!
|
39
48
|
Settings.loaded.should eq false
|
40
49
|
Settings.temp = '123'
|