rails_admin_settings 0.8.0 → 0.9.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -2
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +173 -22
- data/README.md +20 -5
- data/app/models/rails_admin_settings/setting.rb +19 -16
- data/lib/generators/rails_admin_settings/migration_generator.rb +15 -0
- data/lib/generators/rails_admin_settings/templates/migration.rb +26 -0
- data/lib/rails_admin_settings/engine.rb +4 -2
- data/lib/rails_admin_settings/{types.rb → kinds.rb} +5 -1
- data/lib/rails_admin_settings/mongoid.rb +19 -0
- data/lib/rails_admin_settings/namespaced.rb +8 -3
- data/lib/rails_admin_settings/processing.rb +33 -27
- data/lib/rails_admin_settings/rails_admin_config.rb +6 -6
- data/lib/rails_admin_settings/require_helpers.rb +3 -3
- data/lib/rails_admin_settings/settings.rb +7 -0
- data/lib/rails_admin_settings/uploads.rb +13 -3
- data/lib/rails_admin_settings/validation.rb +19 -17
- data/lib/rails_admin_settings/version.rb +2 -1
- data/lib/rails_admin_settings.rb +54 -4
- data/rails_admin_settings.gemspec +4 -3
- data/spec/carrierwave_spec.rb +4 -4
- data/spec/defaults_spec.rb +3 -3
- data/spec/migration_spec.rb +15 -0
- data/spec/model_spec.rb +3 -3
- data/spec/namespaced_spec.rb +5 -5
- data/spec/paperclip_spec.rb +4 -4
- data/spec/settings_spec.rb +7 -7
- data/spec/spec_helper.rb +5 -1
- data/spec/support/defaults.yml +2 -2
- data/spec/support/defaults_w_file.yml +3 -3
- data/spec/types_spec.rb +20 -20
- metadata +27 -9
- data/lib/rails_admin_settings/railtie.rb +0 -7
@@ -37,7 +37,7 @@ module RailsAdminSettings
|
|
37
37
|
require 'validates_email_format_of'
|
38
38
|
yield
|
39
39
|
rescue LoadError => e
|
40
|
-
e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email
|
40
|
+
e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email kind settings"
|
41
41
|
raise e
|
42
42
|
end
|
43
43
|
end
|
@@ -47,7 +47,7 @@ module RailsAdminSettings
|
|
47
47
|
require 'geocoder'
|
48
48
|
yield
|
49
49
|
rescue LoadError => e
|
50
|
-
e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email
|
50
|
+
e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email kind settings"
|
51
51
|
raise e
|
52
52
|
end
|
53
53
|
end
|
@@ -57,7 +57,7 @@ module RailsAdminSettings
|
|
57
57
|
require 'addressable/uri'
|
58
58
|
yield
|
59
59
|
rescue LoadError => e
|
60
|
-
e.message << " [rails_admin_settings] Please add gem 'addressable' to your Gemfile to use url/domain
|
60
|
+
e.message << " [rails_admin_settings] Please add gem 'addressable' to your Gemfile to use url/domain kind settings"
|
61
61
|
raise e
|
62
62
|
end
|
63
63
|
end
|
@@ -23,6 +23,9 @@ class Settings < BasicObject
|
|
23
23
|
else
|
24
24
|
name = name.to_s
|
25
25
|
end
|
26
|
+
if options.key?(:type)
|
27
|
+
options[:kind] = options.delete(:type)
|
28
|
+
end
|
26
29
|
@@mutex.synchronize do
|
27
30
|
@@namespaces[name] ||= ::RailsAdminSettings::Namespaced.new(name.to_s)
|
28
31
|
end
|
@@ -33,6 +36,10 @@ class Settings < BasicObject
|
|
33
36
|
ns(nil, fallback: @@ns_fallback)
|
34
37
|
end
|
35
38
|
|
39
|
+
def table_exists?
|
40
|
+
RailsAdminSettings::Setting.table_exists?
|
41
|
+
end
|
42
|
+
|
36
43
|
def unload!
|
37
44
|
@@mutex.synchronize do
|
38
45
|
@@namespaces.values.map(&:unload!)
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module RailsAdminSettings
|
2
2
|
module Uploads
|
3
3
|
autoload :CarrierWave, "rails_admin_settings/storage/carrierwave"
|
4
|
-
|
5
4
|
def self.included(base)
|
6
5
|
# carrierwave
|
7
6
|
if base.respond_to?(:mount_uploader)
|
@@ -10,8 +9,8 @@ module RailsAdminSettings
|
|
10
9
|
base.mount_uploader(:file, RailsAdminSettings::Uploads::CarrierWave)
|
11
10
|
Settings.file_uploads_supported = true
|
12
11
|
Settings.file_uploads_engine = :carrierwave
|
13
|
-
# paperclip
|
14
|
-
elsif Mongoid.const_defined?('Paperclip')
|
12
|
+
# mongoid-paperclip
|
13
|
+
elsif RailsAdminSettings.mongoid? && Mongoid.const_defined?('Paperclip')
|
15
14
|
base.send(:include, Mongoid::Paperclip)
|
16
15
|
# puts "[rails_admin_settings] PaperClip detected"
|
17
16
|
base.field(:file, type: String)
|
@@ -24,6 +23,17 @@ module RailsAdminSettings
|
|
24
23
|
base.do_not_validate_attachment_file_type :file
|
25
24
|
end
|
26
25
|
|
26
|
+
Settings.file_uploads_supported = true
|
27
|
+
Settings.file_uploads_engine = :paperclip
|
28
|
+
elsif RailsAdminSettings.active_record? && defined?('Paperclip')
|
29
|
+
if defined?(Rails)
|
30
|
+
base.has_attached_file(:file)
|
31
|
+
else
|
32
|
+
base.has_attached_file(:file, path: "#{File.dirname(__FILE__)}/../../uploads/:filename", url: '/uploads/:filename')
|
33
|
+
end
|
34
|
+
if base.respond_to?(:do_not_validate_attachment_file_type)
|
35
|
+
base.do_not_validate_attachment_file_type :file
|
36
|
+
end
|
27
37
|
Settings.file_uploads_supported = true
|
28
38
|
Settings.file_uploads_engine = :paperclip
|
29
39
|
else
|
@@ -4,18 +4,18 @@ module RailsAdminSettings
|
|
4
4
|
base.before_validation do
|
5
5
|
self.raw = default_serializable_value if raw.blank?
|
6
6
|
end
|
7
|
-
base.before_validation :sanitize_value, if: :
|
7
|
+
base.before_validation :sanitize_value, if: :sanitized_kind?
|
8
8
|
base.validates_uniqueness_of :key, scope: :ns
|
9
|
-
base.validates_inclusion_of :
|
10
|
-
base.validates_numericality_of :raw, if: :
|
9
|
+
base.validates_inclusion_of :kind, in: RailsAdminSettings.kinds
|
10
|
+
base.validates_numericality_of :raw, if: :integer_kind?
|
11
11
|
|
12
|
-
base.validate if: :
|
12
|
+
base.validate if: :phone_kind? do
|
13
13
|
require_russian_phone do
|
14
14
|
errors.add(:raw, I18n.t('admin.settings.phone_invalid')) unless raw.blank? || RussianPhone::Number.new(raw).valid?
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
base.validate if: :
|
18
|
+
base.validate if: :phones_kind? do
|
19
19
|
require_russian_phone do
|
20
20
|
unless raw.blank?
|
21
21
|
invalid_phones = raw.gsub("\r", '').split("\n").inject([]) do |memo, value|
|
@@ -27,46 +27,48 @@ module RailsAdminSettings
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
base.validate if: :
|
30
|
+
base.validate if: :email_kind? do
|
31
31
|
require_validates_email_format_of do
|
32
32
|
errors.add(:raw, I18n.t('admin.settings.email_invalid')) unless raw.blank? || ValidatesEmailFormatOf.validate_email_format(raw).nil?
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
base.validate if: :
|
36
|
+
base.validate if: :address_kind? do
|
37
37
|
require_geocoder do
|
38
|
-
# just raise error if we are trying to use address
|
38
|
+
# just raise error if we are trying to use address kind without geocoder
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
base.validate if: :
|
42
|
+
base.validate if: :file_kind? do
|
43
43
|
unless Settings.file_uploads_supported
|
44
|
-
raise '[rails_admin_settings] File
|
44
|
+
raise '[rails_admin_settings] File kind requires either CarrierWave or Paperclip. Check that rails_admin_settings is below them in Gemfile'
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
base.before_validation if: :
|
48
|
+
base.before_validation if: :url_kind? do
|
49
49
|
require_addressable do
|
50
50
|
self.raw = Addressable::URI.heuristic_parse(self.raw) unless self.raw.blank?
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
base.before_validation if: :
|
54
|
+
base.before_validation if: :domain_kind? do
|
55
55
|
require_addressable do
|
56
56
|
self.raw = Addressable::URI.heuristic_parse(self.raw).host unless self.raw.blank?
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
if Object.const_defined?('Geocoder')
|
61
|
-
|
62
|
-
|
61
|
+
if RailsAdminSettings.mongoid?
|
62
|
+
base.field(:coordinates, type: Array)
|
63
|
+
base.send(:include, Geocoder::Model::Mongoid)
|
64
|
+
end
|
63
65
|
base.geocoded_by(:raw)
|
64
|
-
base.after_validation(:geocode, if: :
|
66
|
+
base.after_validation(:geocode, if: :address_kind?)
|
65
67
|
end
|
66
68
|
|
67
|
-
base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw, if: :
|
69
|
+
base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw, if: :color_kind?)
|
68
70
|
|
69
|
-
base.validate if: :
|
71
|
+
base.validate if: :yaml_kind? do
|
70
72
|
require_safe_yaml do
|
71
73
|
unless raw.blank?
|
72
74
|
begin
|
data/lib/rails_admin_settings.rb
CHANGED
@@ -1,9 +1,28 @@
|
|
1
1
|
require "rails_admin_settings/version"
|
2
2
|
|
3
3
|
module RailsAdminSettings
|
4
|
+
class << self
|
5
|
+
def orm
|
6
|
+
if defined?(::Mongoid)
|
7
|
+
:mongoid
|
8
|
+
else
|
9
|
+
:active_record
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def mongoid?
|
14
|
+
orm == :mongoid
|
15
|
+
end
|
16
|
+
|
17
|
+
def active_record?
|
18
|
+
orm == :active_record
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
4
22
|
class PersistenceException < Exception
|
5
23
|
end
|
6
24
|
|
25
|
+
autoload :Mongoid, "rails_admin_settings/mongoid"
|
7
26
|
autoload :Fallback, "rails_admin_settings/fallback"
|
8
27
|
autoload :Namespaced, "rails_admin_settings/namespaced"
|
9
28
|
autoload :Processing, "rails_admin_settings/processing"
|
@@ -14,17 +33,48 @@ module RailsAdminSettings
|
|
14
33
|
autoload :HexColorValidator, "rails_admin_settings/hex_color_validator"
|
15
34
|
autoload :Dumper, "rails_admin_settings/dumper"
|
16
35
|
|
36
|
+
def self.migrate!
|
37
|
+
if RailsAdminSettings.mongoid?
|
38
|
+
RailsAdminSettings::Setting.where(:ns.exists => false).update_all(ns: 'main')
|
39
|
+
RailsAdminSettings::Setting.all.each do |s|
|
40
|
+
s.kind = s.read_attribute(:type) if !s.read_attribute(:type).blank? && s.kind != s.read_attribute(:type)
|
41
|
+
s.save! if s.changed?
|
42
|
+
s.unset(:type)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
if Settings.table_exists?
|
46
|
+
RailsAdminSettings::Setting.where("ns IS NULL").update_all(ns: 'main')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
17
51
|
def self.track_history!
|
18
|
-
|
19
|
-
|
52
|
+
return false unless Settings.table_exists?
|
53
|
+
|
54
|
+
if mongoid?
|
55
|
+
if Mongoid.const_defined?('History')
|
56
|
+
RailsAdminSettings::Setting.send(:include, ::Mongoid::History::Trackable)
|
57
|
+
RailsAdminSettings::Setting.send(:track_history, {track_create: true, track_destroy: true})
|
58
|
+
else
|
59
|
+
puts "[rails_admin_settings] WARN unable to track_history: Mongoid::History not loaded!"
|
60
|
+
end
|
61
|
+
if Mongoid.const_defined?('Userstamp')
|
62
|
+
RailsAdminSettings::Setting.send(:include, ::Mongoid::Userstamp)
|
63
|
+
else
|
64
|
+
puts "[rails_admin_settings] WARN unable to track_history: Mongoid::Userstamp not loaded!"
|
65
|
+
end
|
66
|
+
elsif active_record?
|
67
|
+
if defined?(PaperTrail) && PaperTrail::Version.table_exists?
|
68
|
+
RailsAdminSettings::Setting.send(:has_paper_trail)
|
69
|
+
end
|
70
|
+
end
|
20
71
|
end
|
21
72
|
end
|
22
73
|
|
23
|
-
require "rails_admin_settings/
|
74
|
+
require "rails_admin_settings/kinds"
|
24
75
|
require "rails_admin_settings/settings"
|
25
76
|
|
26
77
|
if Object.const_defined?('Rails')
|
27
|
-
require "rails_admin_settings/railtie"
|
28
78
|
require "rails_admin_settings/engine"
|
29
79
|
else
|
30
80
|
require File.dirname(__FILE__) + '/../app/models/rails_admin_settings/setting.rb'
|
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = RailsAdminSettings::VERSION
|
9
9
|
spec.authors = ["Gleb Tv"]
|
10
10
|
spec.email = ["glebtv@gmail.com"]
|
11
|
-
spec.description = %q{Mongoid / RailsAdmin App Settings management}
|
12
|
-
spec.summary = %q{
|
11
|
+
spec.description = %q{Mongoid / ActiveRecord + RailsAdmin App Settings management}
|
12
|
+
spec.summary = %q{}
|
13
13
|
spec.homepage = "https://github.com/rs-pro/rails_admin_settings"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -18,8 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "mongoid", [">= 3.0", "< 4.1"]
|
22
21
|
|
22
|
+
spec.add_development_dependency "mongoid", [">= 3.0", "< 4.1"]
|
23
|
+
spec.add_development_dependency "rails"
|
23
24
|
spec.add_development_dependency "bundler"
|
24
25
|
spec.add_development_dependency "rake"
|
25
26
|
spec.add_development_dependency "rspec"
|
data/spec/carrierwave_spec.rb
CHANGED
@@ -10,8 +10,8 @@ describe "Uploads" do
|
|
10
10
|
File.unlink(f)
|
11
11
|
end
|
12
12
|
end
|
13
|
-
it 'supports file
|
14
|
-
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"),
|
13
|
+
it 'supports file kind' do
|
14
|
+
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'file')
|
15
15
|
|
16
16
|
# because we're not inside Rails
|
17
17
|
Settings.get(:file).file.root = '/'
|
@@ -21,8 +21,8 @@ describe "Uploads" do
|
|
21
21
|
expect(File.exists?(Settings.root_file_path.join("uploads/1024x768.gif"))).to be_truthy
|
22
22
|
end
|
23
23
|
|
24
|
-
it 'supports image
|
25
|
-
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"),
|
24
|
+
it 'supports image kind' do
|
25
|
+
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'image')
|
26
26
|
|
27
27
|
# because we're not inside Rails
|
28
28
|
Settings.get(:file).file.root = '/'
|
data/spec/defaults_spec.rb
CHANGED
@@ -14,11 +14,11 @@ describe 'Settings loading defaults' do
|
|
14
14
|
|
15
15
|
it 'sets value' do
|
16
16
|
expect(Settings.footer).to eq 'test <b></b>'
|
17
|
-
expect(Settings.get(:footer).
|
17
|
+
expect(Settings.get(:footer).kind).to eq 'html'
|
18
18
|
end
|
19
19
|
|
20
|
-
it 'sets
|
21
|
-
expect(Settings.get(:phone).
|
20
|
+
it 'sets kind' do
|
21
|
+
expect(Settings.get(:phone).phone_kind?).to be_truthy
|
22
22
|
expect(Settings.get(:phone).val.city).to eq '906'
|
23
23
|
expect(Settings.get(:phone).val.formatted_subscriber).to eq '111-11-11'
|
24
24
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Migrating from old versions' do
|
6
|
+
it 'sets ns' do
|
7
|
+
RailsAdminSettings::Setting.collection.insert({enabled: true, key: 'test', raw: '9060000000', type: 'phone'})
|
8
|
+
RailsAdminSettings.migrate!
|
9
|
+
RailsAdminSettings::Setting.first.key.should eq 'test'
|
10
|
+
RailsAdminSettings::Setting.first.raw.should eq '9060000000'
|
11
|
+
RailsAdminSettings::Setting.first.ns.should eq 'main'
|
12
|
+
RailsAdminSettings::Setting.first.kind.should eq 'phone'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
data/spec/model_spec.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe RailsAdminSettings::Setting do
|
6
|
-
it { is_expected.to have_fields(:enabled, :key, :
|
6
|
+
it { is_expected.to have_fields(:enabled, :key, :kind, :raw) }
|
7
7
|
|
8
8
|
it "correctly return content when enabled" do
|
9
9
|
setting = FactoryGirl.create(:setting)
|
@@ -32,13 +32,13 @@ describe RailsAdminSettings::Setting do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'return html_safe string when in html mode' do
|
35
|
-
setting = FactoryGirl.create(:setting, raw: '© {{year}} company',
|
35
|
+
setting = FactoryGirl.create(:setting, raw: '© {{year}} company', kind: 'html')
|
36
36
|
expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company"
|
37
37
|
expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'sanitize html when in sanitized mode' do
|
41
|
-
setting = FactoryGirl.create(:setting, raw: '© {{year}} company <a href="javascript:alert()">test</a>',
|
41
|
+
setting = FactoryGirl.create(:setting, raw: '© {{year}} company <a href="javascript:alert()">test</a>', kind: 'sanitized')
|
42
42
|
expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company <a>test</a>"
|
43
43
|
expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
|
44
44
|
end
|
data/spec/namespaced_spec.rb
CHANGED
@@ -24,17 +24,17 @@ describe 'Namespaced settings' do
|
|
24
24
|
expect(Settings.ns(:other).test).to eq ''
|
25
25
|
end
|
26
26
|
|
27
|
-
it 'sets
|
27
|
+
it 'sets kind' do
|
28
28
|
expect {
|
29
|
-
Settings.ns(:other).set(:phone, 'test',
|
29
|
+
Settings.ns(:other).set(:phone, 'test', kind: 'phone')
|
30
30
|
}.to raise_error
|
31
|
-
Settings.ns(:other).set(:phone, '906 111 11 11',
|
32
|
-
expect(Settings.get(:phone, ns: 'other').
|
31
|
+
Settings.ns(:other).set(:phone, '906 111 11 11', kind: 'phone')
|
32
|
+
expect(Settings.get(:phone, ns: 'other').phone_kind?).to be_truthy
|
33
33
|
expect(Settings.get(:phone, ns: 'other').val.city).to eq '906'
|
34
34
|
expect(Settings.get(:phone, ns: 'other').val.formatted_subscriber).to eq '111-11-11'
|
35
35
|
|
36
36
|
ns = Settings.ns(:other)
|
37
|
-
expect(ns.get(:phone).
|
37
|
+
expect(ns.get(:phone).phone_kind?).to be_truthy
|
38
38
|
expect(ns.get(:phone).val.city).to eq '906'
|
39
39
|
expect(ns.get(:phone).val.formatted_subscriber).to eq '111-11-11'
|
40
40
|
end
|
data/spec/paperclip_spec.rb
CHANGED
@@ -12,8 +12,8 @@ describe "Uploads" do
|
|
12
12
|
File.unlink(f)
|
13
13
|
end
|
14
14
|
end
|
15
|
-
it 'supports file
|
16
|
-
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"),
|
15
|
+
it 'supports file kind' do
|
16
|
+
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'file')
|
17
17
|
expect(Settings.get(:file).file_file_name).to eq '1024x768.gif'
|
18
18
|
expect(Settings.get(:file).file_file_size).to eq 4357
|
19
19
|
expect(Settings.file[0..21]).to eq '/uploads/1024x768.gif?'
|
@@ -21,8 +21,8 @@ describe "Uploads" do
|
|
21
21
|
expect(File.exists?("#{File.dirname(__FILE__)}/../uploads/1024x768.gif")).to be_truthy
|
22
22
|
end
|
23
23
|
|
24
|
-
it 'supports image
|
25
|
-
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"),
|
24
|
+
it 'supports image kind' do
|
25
|
+
Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'image')
|
26
26
|
expect(Settings.get(:file).file_file_name).to eq '1024x768.gif'
|
27
27
|
expect(Settings.get(:file).file_file_size).to eq 4357
|
28
28
|
expect(Settings.file[0..21]).to eq '/uploads/1024x768.gif?'
|
data/spec/settings_spec.rb
CHANGED
@@ -43,10 +43,10 @@ describe 'Settings' do
|
|
43
43
|
expect(Settings.loaded).to eq false
|
44
44
|
end
|
45
45
|
|
46
|
-
it 'should work with
|
47
|
-
expect(Settings.phone(
|
46
|
+
it 'should work with kind and default' do
|
47
|
+
expect(Settings.phone(kind: 'phone', default: '906 111 11 11')).to eq '+7 (906) 111-11-11'
|
48
48
|
Settings.phone = '906 222 22 22'
|
49
|
-
expect(Settings.phone(
|
49
|
+
expect(Settings.phone(kind: 'phone', default: '906 111 11 11')).to eq '+7 (906) 222-22-22'
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'should properly store settings to DB' do
|
@@ -60,16 +60,16 @@ describe 'Settings' do
|
|
60
60
|
expect(Settings.loaded).to eq true
|
61
61
|
end
|
62
62
|
|
63
|
-
it 'should support yaml
|
64
|
-
Settings.tdata(
|
63
|
+
it 'should support yaml kind' do
|
64
|
+
Settings.tdata(kind: 'yaml')
|
65
65
|
Settings.tdata = ['one', 'two', 'three']
|
66
66
|
expect(YAML.safe_load(Settings.get(:tdata).raw)).to eq ['one', 'two', 'three']
|
67
67
|
expect(Settings.tdata).to eq ['one', 'two', 'three']
|
68
68
|
end
|
69
69
|
|
70
70
|
it '#enabled? sets defaults' do
|
71
|
-
expect(Settings.enabled?(:phone,
|
72
|
-
expect(Settings.get(:phone).
|
71
|
+
expect(Settings.enabled?(:phone, kind: 'phone')).to eq true
|
72
|
+
expect(Settings.get(:phone).kind).to eq 'phone'
|
73
73
|
end
|
74
74
|
|
75
75
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,7 +9,11 @@ require 'pry'
|
|
9
9
|
require 'bundler/setup'
|
10
10
|
require 'active_support'
|
11
11
|
require 'active_support/core_ext'
|
12
|
-
|
12
|
+
|
13
|
+
unless ENV['ACTIVERECORD']
|
14
|
+
require 'mongoid'
|
15
|
+
end
|
16
|
+
|
13
17
|
require 'database_cleaner'
|
14
18
|
require 'factory_girl'
|
15
19
|
require 'mongoid-rspec'
|
data/spec/support/defaults.yml
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
main:
|
2
2
|
footer:
|
3
|
-
|
3
|
+
kind: html
|
4
4
|
value: 'test <b></b>'
|
5
5
|
|
6
6
|
phone:
|
7
|
-
|
7
|
+
kind: 'phone'
|
8
8
|
value: '906 1111111'
|
9
9
|
|
10
10
|
disabled:
|
@@ -15,5 +15,5 @@ other:
|
|
15
15
|
value: 'zzz'
|
16
16
|
|
17
17
|
img:
|
18
|
-
|
18
|
+
kind: 'image'
|
19
19
|
value: 'spec/support/1024x768.gif'
|
data/spec/types_spec.rb
CHANGED
@@ -2,73 +2,73 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe 'Settings
|
5
|
+
describe 'Settings kind' do
|
6
6
|
it 'html' do
|
7
|
-
expect(Settings.get(:email,
|
7
|
+
expect(Settings.get(:email, kind: 'html', default: 'test@example.com').to_s).to eq 'test@example.com'
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'integer' do
|
11
|
-
expect(Settings.get(:testint,
|
12
|
-
expect(Settings.get(:testint, default: 5,
|
13
|
-
expect(Settings.get(:testint2, default: 5,
|
11
|
+
expect(Settings.get(:testint, kind: 'integer').value).to eq 0
|
12
|
+
expect(Settings.get(:testint, default: 5, kind: 'integer').value).to eq 0
|
13
|
+
expect(Settings.get(:testint2, default: 5, kind: 'integer').value).to eq 5
|
14
14
|
expect(Settings.testint2).to eq 5
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'yaml' do
|
18
|
-
Settings.set(:data, '[one, two, three]',
|
18
|
+
Settings.set(:data, '[one, two, three]', kind: 'yaml')
|
19
19
|
expect(Settings.get(:data).raw).to eq '[one, two, three]'
|
20
20
|
expect(Settings.data).to eq ['one', 'two', 'three']
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'phone' do
|
24
|
-
Settings.set(:tphone, '906 111 11 11',
|
24
|
+
Settings.set(:tphone, '906 111 11 11', kind: 'phone')
|
25
25
|
expect(Settings.get(:tphone).val.class.name).to eq 'RussianPhone::Number'
|
26
26
|
expect(Settings.tphone.class.name).to eq 'RussianPhone::Number'
|
27
27
|
expect(Settings.tphone).to eq '906 111 11 11'
|
28
|
-
expect(Settings.get(:tphone).
|
28
|
+
expect(Settings.get(:tphone).phone_kind?).to be_truthy
|
29
29
|
expect(Settings.get(:tphone).val.city).to eq '906'
|
30
30
|
expect(Settings.get(:tphone).val.formatted_subscriber).to eq '111-11-11'
|
31
31
|
end
|
32
32
|
|
33
|
-
it 'supports phones
|
34
|
-
Settings.set(:tphone, ['906 111 11 11', '907 111 11 11'] * "\n",
|
33
|
+
it 'supports phones kind' do
|
34
|
+
Settings.set(:tphone, ['906 111 11 11', '907 111 11 11'] * "\n", kind: 'phones')
|
35
35
|
expect(Settings.get(:tphone).val.class.name).to eq 'Array'
|
36
36
|
expect(Settings.tphone.class.name).to eq 'Array'
|
37
37
|
expect(Settings.get(:tphone).val.first.class.name).to eq 'RussianPhone::Number'
|
38
38
|
expect(Settings.tphone.first.class.name).to eq 'RussianPhone::Number'
|
39
39
|
expect(Settings.tphone.first).to eq '906 111 11 11'
|
40
|
-
expect(Settings.get(:tphone).
|
40
|
+
expect(Settings.get(:tphone).phones_kind?).to be_truthy
|
41
41
|
expect(Settings.get(:tphone).val.first.city).to eq '906'
|
42
42
|
expect(Settings.get(:tphone).val.last.city).to eq '907'
|
43
43
|
expect(Settings.get(:tphone).val.first.formatted_subscriber).to eq '111-11-11'
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'defaults for phone' do
|
47
|
-
Settings.dphone(
|
48
|
-
expect(Settings.get(:dphone).
|
47
|
+
Settings.dphone(kind: 'phone')
|
48
|
+
expect(Settings.get(:dphone).phone_kind?).to be_truthy
|
49
49
|
expect(Settings.dphone.formatted_area).to eq ''
|
50
50
|
expect(Settings.dphone.formatted_subscriber).to eq ''
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'email validates' do
|
54
|
-
Settings.eml(
|
55
|
-
expect(Settings.get(:eml).
|
54
|
+
Settings.eml(kind: 'email')
|
55
|
+
expect(Settings.get(:eml).email_kind?).to be_truthy
|
56
56
|
expect { Settings.eml = '1' }.to raise_error
|
57
57
|
Settings.eml = 'test@example.com'
|
58
58
|
expect(Settings.eml).to eq 'test@example.com'
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'url processing' do
|
62
|
-
Settings.url(
|
63
|
-
expect(Settings.get(:url).
|
64
|
-
expect(Settings.get(:url).
|
62
|
+
Settings.url(kind: 'url')
|
63
|
+
expect(Settings.get(:url).url_kind?).to be_truthy
|
64
|
+
expect(Settings.get(:url).color_kind?).to be_falsey
|
65
65
|
Settings.url = 'test.ru'
|
66
66
|
expect(Settings.url).to eq 'http://test.ru'
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'color' do
|
70
|
-
Settings.col(
|
71
|
-
expect(Settings.get(:col).
|
70
|
+
Settings.col(kind: 'color')
|
71
|
+
expect(Settings.get(:col).color_kind?).to be_truthy
|
72
72
|
expect { Settings.col = 'test'}.to raise_error
|
73
73
|
Settings.col = 'ffffff'
|
74
74
|
expect(Settings.col).to eq 'ffffff'
|