thecore_settings 1.1.15 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/Dockerfile +20 -0
  3. data/.devcontainer/devcontainer.json +33 -0
  4. data/.github/workflows/gempush.yml +34 -0
  5. data/.gitignore +542 -0
  6. data/.rakeTasks +7 -0
  7. data/.rspec +1 -0
  8. data/.ruby-version +1 -0
  9. data/.travis.yml +31 -0
  10. data/CHANGELOG.md +81 -0
  11. data/Gemfile +4 -0
  12. data/Gemfile.lock +275 -0
  13. data/LICENSE.txt +22 -0
  14. data/README.md +169 -0
  15. data/README.rdoc +68 -0
  16. data/Rakefile +6 -34
  17. data/app/assets/images/thecore_settings/.keep +0 -0
  18. data/app/assets/javascripts/thecore_settings/.keep +0 -0
  19. data/app/assets/stylesheets/thecore_settings/.keep +0 -0
  20. data/app/controllers/.keep +0 -0
  21. data/app/helpers/.keep +0 -0
  22. data/app/mailers/.keep +0 -0
  23. data/app/models/.keep +0 -0
  24. data/app/models/rails_admin_settings/setting.rb +77 -0
  25. data/app/views/.keep +0 -0
  26. data/app/views/rails_admin/main/_setting_value.html.haml +41 -0
  27. data/assets/javascripts/rails_admin/custom/thecore_settings.rb +2 -0
  28. data/bin/rails +13 -0
  29. data/config/locales/en.yml +29 -0
  30. data/config/locales/pt-BR.yml +28 -0
  31. data/config/locales/ru.yml +29 -0
  32. data/gemfiles/mongoid-6.0.gemfile +5 -0
  33. data/gemfiles/mongoid-6.3.gemfile +5 -0
  34. data/lib/generators/rails_admin_settings/migration_generator.rb +15 -0
  35. data/lib/generators/rails_admin_settings/templates/db/migrate/20161227101954_create_thecore_settings.rb +29 -0
  36. data/lib/generators/rails_admin_settings/templates/migration.rb +29 -0
  37. data/lib/thecore_settings/determine_mime_type.rb +193 -0
  38. data/lib/thecore_settings/dumper.rb +12 -0
  39. data/lib/thecore_settings/engine.rb +17 -3
  40. data/lib/thecore_settings/fallback.rb +21 -0
  41. data/lib/thecore_settings/hex_color_validator.rb +11 -0
  42. data/lib/thecore_settings/kinds.rb +34 -0
  43. data/lib/thecore_settings/mongoid.rb +19 -0
  44. data/lib/thecore_settings/namespaced.rb +210 -0
  45. data/lib/thecore_settings/processing.rb +217 -0
  46. data/lib/thecore_settings/rails_admin_config.rb +71 -0
  47. data/lib/thecore_settings/require_helpers.rb +86 -0
  48. data/lib/thecore_settings/settings.rb +95 -0
  49. data/lib/thecore_settings/storage/carrier_wave_uploader.rb +9 -0
  50. data/lib/thecore_settings/storage/shrine_uploader.rb +14 -0
  51. data/lib/thecore_settings/tasks.rb +35 -0
  52. data/lib/thecore_settings/uploads.rb +57 -0
  53. data/lib/thecore_settings/validation.rb +126 -0
  54. data/lib/thecore_settings/version.rb +1 -1
  55. data/lib/thecore_settings.rb +115 -123
  56. data/log/development.log +0 -0
  57. data/spec/advanced_usage_spec.rb +11 -0
  58. data/spec/carrierwave_spec.rb +41 -0
  59. data/spec/database_trickery_spec.rb +48 -0
  60. data/spec/defaults_spec.rb +87 -0
  61. data/spec/enabling_spec.rb +29 -0
  62. data/spec/factories/setting.rb +8 -0
  63. data/spec/label_spec.rb +16 -0
  64. data/spec/migration_spec.rb +20 -0
  65. data/spec/model_spec.rb +105 -0
  66. data/spec/namespaced_spec.rb +67 -0
  67. data/spec/paperclip_spec.rb +38 -0
  68. data/spec/settings_spec.rb +75 -0
  69. data/spec/shrine_spec.rb +34 -0
  70. data/spec/spec_helper.rb +85 -0
  71. data/spec/support/1024x768.gif +0 -0
  72. data/spec/support/database_cleaner.rb +10 -0
  73. data/spec/support/defaults.yml +23 -0
  74. data/spec/support/defaults_w_file.yml +19 -0
  75. data/spec/support/mongoid.rb +6 -0
  76. data/spec/support/mongoid.yml +6 -0
  77. data/spec/types_spec.rb +101 -0
  78. data/test/dummy/Rakefile +6 -0
  79. data/test/dummy/app/assets/config/manifest.js +4 -0
  80. data/test/dummy/app/assets/images/.keep +0 -0
  81. data/test/dummy/app/assets/javascripts/application.js +13 -0
  82. data/test/dummy/app/assets/javascripts/cable.js +13 -0
  83. data/test/dummy/app/assets/javascripts/channels/.keep +0 -0
  84. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  85. data/test/dummy/app/channels/application_cable/channel.rb +4 -0
  86. data/test/dummy/app/channels/application_cable/connection.rb +4 -0
  87. data/test/dummy/app/controllers/application_controller.rb +3 -0
  88. data/test/dummy/app/controllers/concerns/.keep +0 -0
  89. data/test/dummy/app/helpers/application_helper.rb +2 -0
  90. data/test/dummy/app/jobs/application_job.rb +2 -0
  91. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  92. data/test/dummy/app/models/application_record.rb +3 -0
  93. data/test/dummy/app/models/concerns/.keep +0 -0
  94. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  95. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  96. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  97. data/test/dummy/bin/bundle +3 -0
  98. data/test/dummy/bin/rails +4 -0
  99. data/test/dummy/bin/rake +4 -0
  100. data/test/dummy/bin/setup +34 -0
  101. data/test/dummy/bin/update +29 -0
  102. data/test/dummy/config/application.rb +15 -0
  103. data/test/dummy/config/boot.rb +5 -0
  104. data/test/dummy/config/cable.yml +9 -0
  105. data/test/dummy/config/database.yml +25 -0
  106. data/test/dummy/config/environment.rb +5 -0
  107. data/test/dummy/config/environments/development.rb +54 -0
  108. data/test/dummy/config/environments/production.rb +86 -0
  109. data/test/dummy/config/environments/test.rb +42 -0
  110. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  111. data/test/dummy/config/initializers/assets.rb +11 -0
  112. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  113. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  114. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  115. data/test/dummy/config/initializers/inflections.rb +16 -0
  116. data/test/dummy/config/initializers/mime_types.rb +4 -0
  117. data/test/dummy/config/initializers/new_framework_defaults.rb +24 -0
  118. data/test/dummy/config/initializers/session_store.rb +3 -0
  119. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  120. data/test/dummy/config/locales/en.yml +23 -0
  121. data/test/dummy/config/puma.rb +47 -0
  122. data/test/dummy/config/routes.rb +3 -0
  123. data/test/dummy/config/secrets.yml +22 -0
  124. data/test/dummy/config/spring.rb +6 -0
  125. data/test/dummy/config.ru +5 -0
  126. data/test/dummy/db/development.sqlite3 +0 -0
  127. data/test/dummy/lib/assets/.keep +0 -0
  128. data/test/dummy/log/.keep +0 -0
  129. data/test/dummy/log/development.log +16 -0
  130. data/test/dummy/public/404.html +67 -0
  131. data/test/dummy/public/422.html +67 -0
  132. data/test/dummy/public/500.html +66 -0
  133. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  134. data/test/dummy/public/apple-touch-icon.png +0 -0
  135. data/test/dummy/public/favicon.ico +0 -0
  136. data/test/dummy/tmp/.keep +0 -0
  137. data/test/integration/navigation_test.rb +8 -0
  138. data/test/test_helper.rb +19 -0
  139. data/test/thecore_settings_test.rb +7 -0
  140. data/thecore_settings.gemspec +44 -0
  141. metadata +457 -54
File without changes
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings advanced usage' do
6
+ it 'with defaults' do
7
+ s = Settings.email(default: 'test@example.com')
8
+ expect(s).to eq 'test@example.com'
9
+ expect(Settings.get(:email).to_s).to eq 'test@example.com'
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Uploads" do
4
+ if Settings.file_uploads_engine != :carrierwave
5
+ pending "paperclip not detected, skipped. To run use UPLOADS=carrierwave rspec"
6
+ else
7
+ before :each do
8
+ f = "#{File.dirname(__FILE__)}/../uploads/1024x768.gif"
9
+ if File.file?(f)
10
+ File.unlink(f)
11
+ end
12
+ end
13
+ it 'supports file kind' do
14
+ Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'file')
15
+
16
+ # because we're not inside Rails
17
+ Settings.get(:file).file.root = '/'
18
+
19
+ expect(Settings.get(:file).file.file.file).to eq "#{File.dirname(__FILE__).gsub('/spec', '/')}uploads/1024x768.gif"
20
+
21
+ expect(File.exists?(Settings.root_file_path.join("uploads/1024x768.gif"))).to be_truthy
22
+ end
23
+
24
+ it 'supports image kind' do
25
+ Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'image')
26
+
27
+ # because we're not inside Rails
28
+ Settings.get(:file).file.root = '/'
29
+
30
+ expect(Settings.get(:file).file.file.file).to eq "#{File.dirname(__FILE__).gsub('/spec', '/')}uploads/1024x768.gif"
31
+
32
+ expect(File.exists?(Settings.root_file_path.join("uploads/1024x768.gif"))).to be_truthy
33
+ end
34
+
35
+ it 'supports defaults' do
36
+ Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults_w_file.yml'))
37
+ expect(File.exists?(Settings.root_file_path.join("uploads/1024x768.gif"))).to be_truthy
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ # this tests check how well thecore_settings handles settings disappearing from DB during execution
6
+ # real usage: app specs with database_cleaner enabled
7
+ describe 'Database trickery' do
8
+
9
+ it "should handle settings disappearing from DB" do
10
+ email = "my@mail.ru"
11
+ email2 = "my2@mail.ru"
12
+ Settings.email = email
13
+ expect(Settings.email).to eq(email)
14
+ ThecoreSettings::Setting.destroy_all
15
+ # settings are still cached
16
+ expect(Settings.email).to eq(email)
17
+
18
+ Settings.email = email2
19
+ expect(Settings.email).to eq(email2)
20
+ end
21
+
22
+ it "should handle settings appearing in DB when settings are loaded" do
23
+ expect(Settings.tst2).to eq('')
24
+ ThecoreSettings::Setting.create!(key: 'tst', raw: 'tst')
25
+ # settings are still cached, but when we try to create a setting it sees updated value in DB
26
+ expect(Settings.tst).to eq('tst')
27
+ end
28
+
29
+ it "should handle settings appearing in DB when settings are not loaded" do
30
+ ThecoreSettings::Setting.create(key: 'tst', raw: 'tst')
31
+ Settings.tst = 'str'
32
+ expect(Settings.tst).to eq('str')
33
+ end
34
+
35
+ it "#destroy_all!" do
36
+ Settings.tst = 'str'
37
+ Settings.destroy_all!
38
+ expect(Settings.tst).to eq('')
39
+ end
40
+
41
+ it "#destroy!" do
42
+ Settings.tst = 'str'
43
+ Settings.tst2 = 'str2'
44
+ Settings.destroy!(:tst)
45
+ expect(Settings.tst).to eq('')
46
+ expect(Settings.tst2).to eq('str2')
47
+ end
48
+ end
@@ -0,0 +1,87 @@
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
+ expect(Settings.footer).to eq 'test <b></b>'
17
+ expect(Settings.get(:footer).kind).to eq 'html'
18
+ end
19
+
20
+ it 'sets kind' do
21
+ expect(Settings.get(:phone).phone_kind?).to be_truthy
22
+ expect(Settings.get(:phone).val.city).to eq '906'
23
+ expect(Settings.get(:phone).val.formatted_subscriber).to eq '111-11-11'
24
+ end
25
+
26
+ it 'sets enabled' do
27
+ expect(Settings.phone_enabled?).to eq true
28
+ expect(Settings.disabled_enabled?).to eq false
29
+ expect(Settings.enabled?(:disabled)).to eq false
30
+ end
31
+
32
+ it 'works with namespace' do
33
+ expect(Settings.ns(:main).phone).to eq '906 1111111'
34
+ expect(Settings.ns(:other).footer).to eq 'zzz'
35
+ expect(Settings.footer).to eq 'test <b></b>'
36
+ expect(Settings.ns(:main).footer).to eq 'test <b></b>'
37
+ end
38
+
39
+ it 'works with fallback' do
40
+ expect(Settings.ns(:etc, fallback: :main).phone).to eq '906 1111111'
41
+ expect(Settings.ns(:etc, fallback: :main).footer).to eq 'test <b></b>'
42
+ expect(Settings.ns(:other, fallback: :main).footer).to eq 'zzz'
43
+ expect(Settings.ns(:etc, fallback: :other).footer).to eq 'zzz'
44
+ end
45
+
46
+ it 'works with custom default namespace' do
47
+ Settings.ns_default = 'other'
48
+ Settings.ns_fallback = 'other'
49
+
50
+ expect(Settings.ns(:main).phone).to eq '906 1111111'
51
+ expect(Settings.ns(:other).footer).to eq 'zzz'
52
+ expect(Settings.ns(:main).footer).to eq 'test <b></b>'
53
+ expect(Settings.footer).to eq 'zzz'
54
+
55
+ expect(Settings.ns(:main).phone).to eq '906 1111111'
56
+ expect(Settings.ns(:main).true_setting).to be true
57
+ expect(Settings.ns(:main).false_setting).to be false
58
+ expect(Settings.ns(:other).footer).to eq 'zzz'
59
+ expect(Settings.ns(:main).footer).to eq 'test <b></b>'
60
+ expect(Settings.footer).to eq 'zzz'
61
+
62
+ expect(Settings.ns(:etc, fallback: :main).phone).to eq '906 1111111'
63
+ expect(Settings.ns(:etc, fallback: :main).footer).to eq 'test <b></b>'
64
+ expect(Settings.ns(:other, fallback: :main).footer).to eq 'zzz'
65
+ expect(Settings.ns(:etc, fallback: :other).footer).to eq 'zzz'
66
+
67
+ Settings.ns_default = :etc
68
+ Settings.ns_fallback = :main
69
+ expect(Settings.phone).to eq '906 1111111'
70
+ expect(Settings.footer).to eq 'test <b></b>'
71
+
72
+ Settings.ns_fallback = :other
73
+ expect(Settings.footer).to eq 'zzz'
74
+
75
+ Settings.ns_default = :other
76
+ Settings.ns_fallback = :main
77
+ expect(Settings.footer).to eq 'zzz'
78
+ end
79
+
80
+ it "doesn't overwrite" do
81
+ Settings.ns(:main).phone = '906 2222222'
82
+ Settings.ns(:other).footer = 'xxx'
83
+ Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults.yml'))
84
+ expect(Settings.ns(:main).phone).to eq '906 2222222'
85
+ expect(Settings.ns(:other).footer).to eq 'xxx'
86
+ end
87
+ 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
+ expect(Settings.zzz).to eq '123'
9
+ expect(Settings.get(:zzz).enabled).to eq true
10
+ expect(Settings.enabled?(:zzz)).to eq true
11
+ expect(Settings.zzz_enabled?).to eq true
12
+
13
+ expect(Settings.zzz).to eq '123'
14
+ Settings.zzz_enabled = false
15
+ expect(Settings.zzz_enabled?).to eq false
16
+ expect(Settings.get(:zzz).enabled).to eq false
17
+ expect(Settings.enabled?(:zzz)).to eq false
18
+ expect(Settings.zzz).to eq ''
19
+ Settings.unload!
20
+ expect(Settings.zzz).to eq ''
21
+ expect(Settings.get(:zzz).raw).to eq '123'
22
+
23
+ Settings.zzz_enabled = true
24
+ expect(Settings.zzz).to eq '123'
25
+ expect(Settings.zzz_enabled?).to eq true
26
+ expect(Settings.get(:zzz).enabled).to eq true
27
+ expect(Settings.enabled?(:zzz)).to eq true
28
+ end
29
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+
3
+ FactoryBot.define do
4
+ factory :setting, class: ThecoreSettings::Setting do
5
+ sequence(:key){|n| "setting_#{n}" }
6
+ raw { "Контент 1" }
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings label' do
6
+ it "should have label" do
7
+ label = "E-Mail"
8
+ Settings.email(label: label, default: "my@mail.ru")
9
+ expect(Settings.get(:email).name).to eq(label)
10
+ end
11
+
12
+ it "should properly set label as key if blank" do
13
+ Settings.email(default: "my@mail.ru")
14
+ expect(Settings.get(:email).name).to eq('email')
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Migrating from old versions' do
6
+ it 'sets ns' do
7
+ coll = ThecoreSettings::Setting.collection
8
+ if coll.respond_to?(:insert_one)
9
+ coll.insert_one({enabled: true, key: 'test', raw: '9060000000', type: 'phone'})
10
+ else
11
+ coll.insert({enabled: true, key: 'test', raw: '9060000000', type: 'phone'})
12
+ end
13
+ ThecoreSettings.migrate!
14
+ expect(ThecoreSettings::Setting.first.key).to eq 'test'
15
+ expect(ThecoreSettings::Setting.first.raw).to eq '9060000000'
16
+ expect(ThecoreSettings::Setting.first.ns).to eq 'main'
17
+ expect(ThecoreSettings::Setting.first.kind).to eq 'phone'
18
+ end
19
+ end
20
+
@@ -0,0 +1,105 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ThecoreSettings::Setting do
6
+ it { is_expected.to have_fields(:enabled, :key, :kind, :raw) }
7
+
8
+ it "correctly return content when enabled" do
9
+ setting = FactoryBot.create(:setting)
10
+ expect(setting.to_s).to eq "Контент 1"
11
+ end
12
+
13
+ it "return empty string when disabled" do
14
+ setting = FactoryBot.create(:setting, enabled: false)
15
+ expect(setting.to_s).to eq ""
16
+ end
17
+
18
+ it "correctly process {{year}}" do
19
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year}} company')
20
+ expect(setting.val).to eq "&copy; #{Time.now.strftime('%Y')} company"
21
+ end
22
+
23
+ it "correctly process {{year|2010}}" do
24
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year|2010}} company')
25
+ expect(setting.val).to eq "&copy; 2010-#{Time.now.strftime('%Y')} company"
26
+ end
27
+
28
+ it "correctly process {{year|current_year}}" do
29
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year|' + Time.now.strftime('%Y') + '}} company')
30
+ expect(setting.val).to eq "&copy; #{Time.now.strftime('%Y')} company"
31
+ expect(setting.val.class.name).not_to eq "ActiveSupport::SafeBuffer"
32
+ end
33
+
34
+ it 'return html_safe string when in html mode' do
35
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year}} company', kind: 'html')
36
+ expect(setting.val).to eq "&copy; #{Time.now.strftime('%Y')} company"
37
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
38
+ end
39
+
40
+ it 'sanitize html when in sanitized mode' do
41
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'sanitized')
42
+ expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company <a>test</a>"
43
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
44
+ end
45
+
46
+ it 'sanitize html when in sanitize mode' do
47
+ if defined?(Rails)
48
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'sanitize')
49
+ expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company <a>test</a>"
50
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
51
+ else
52
+ expect {
53
+ FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'sanitize')
54
+ }.to raise_error(ThecoreSettings::NoRailsError)
55
+ end
56
+ end
57
+
58
+ it 'sanitize html when in sanitize_code mode' do
59
+ if defined?(Rails)
60
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'sanitize_code')
61
+ expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company <a>test</a>"
62
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
63
+ else
64
+ expect {
65
+ FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'sanitize_code')
66
+ }.to raise_error(ThecoreSettings::NoRailsError)
67
+ end
68
+ end
69
+
70
+ it 'remove html when in strip_tags mode' do
71
+ if defined?(Rails)
72
+ setting = FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'strip_tags')
73
+ expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company test"
74
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
75
+ else
76
+ expect {
77
+ FactoryBot.create(:setting, raw: '&copy; {{year}} company <a href="javascript:alert()">test</a>', kind: 'strip_tags')
78
+ }.to raise_error(ThecoreSettings::NoRailsError)
79
+ end
80
+ end
81
+
82
+ it 'formats text and cleans html in simple_format mode' do
83
+ if defined?(Rails)
84
+ setting = FactoryBot.create(:setting, raw: "&copy; {{year}}\n\ncompany <a href='javascript:alert()'>test</a>", kind: 'simple_format')
85
+ expect(setting.val).to eq "<p>© #{Time.now.strftime('%Y')}</p>\n\n<p>company <a>test</a></p>"
86
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
87
+ else
88
+ expect {
89
+ FactoryBot.create(:setting, raw: "&copy; {{year}}\n\ncompany <a href='javascript:alert()'>test</a>", kind: 'simple_format')
90
+ }.to raise_error(ThecoreSettings::NoRailsError)
91
+ end
92
+ end
93
+
94
+ it 'formats text and DOESNT html in simple_format_raw mode' do
95
+ if defined?(Rails)
96
+ setting = FactoryBot.create(:setting, raw: "&copy; {{year}}\n\ncompany <a href='javascript:alert()'>test</a>", kind: 'simple_format_raw')
97
+ expect(setting.val).to eq "<p>&copy; #{Time.now.strftime('%Y')}</p>\n\n<p>company <a href='javascript:alert()'>test</a></p>"
98
+ expect(setting.val.class.name).to eq "ActiveSupport::SafeBuffer"
99
+ else
100
+ expect {
101
+ FactoryBot.create(:setting, raw: "&copy; {{year}}\n\ncompany <a href='javascript:alert()'>test</a>", kind: 'simple_format_raw')
102
+ }.to raise_error(ThecoreSettings::NoRailsError)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Namespaced settings' do
6
+ before :each do
7
+ Settings.destroy_all!
8
+ end
9
+
10
+ it 'sets namespaced' do
11
+ Settings.ns(:other).test = 'test'
12
+ end
13
+
14
+ it 'reads namespaced from cache' do
15
+ ns = Settings.ns(:other)
16
+ ns.test = 'test'
17
+ expect(ns.test).to eq 'test'
18
+ end
19
+
20
+ it 'reads namespaced from db' do
21
+ Settings.ns(:other).test = 'test'
22
+ expect(Settings.ns(:other).test).to eq 'test'
23
+ end
24
+
25
+ it 'destroys' do
26
+ Settings.ns(:other).test = 'test'
27
+ Settings.ns(:other).destroy_all!
28
+ expect(Settings.ns(:other).test).to eq ''
29
+ end
30
+
31
+ it 'sets kind' do
32
+ expect {
33
+ Settings.ns(:other).set(:phone, 'test', kind: 'phone')
34
+ }.to raise_error
35
+ Settings.ns(:other).set(:phone, '906 111 11 11', kind: 'phone')
36
+ expect(Settings.get(:phone, ns: 'other').phone_kind?).to be_truthy
37
+ expect(Settings.get(:phone, ns: 'other').val.city).to eq '906'
38
+ expect(Settings.get(:phone, ns: 'other').val.formatted_subscriber).to eq '111-11-11'
39
+
40
+ ns = Settings.ns(:other)
41
+ expect(ns.get(:phone).phone_kind?).to be_truthy
42
+ expect(ns.get(:phone).val.city).to eq '906'
43
+ expect(ns.get(:phone).val.formatted_subscriber).to eq '111-11-11'
44
+ end
45
+
46
+ it 'works with custom defaults' do
47
+ Settings.ns_default = 'hitfood'
48
+ Settings.ns_fallback = 'main'
49
+ expect(Settings.test).to eq ''
50
+ Settings.test = 'zzz'
51
+ expect(Settings.get(:test, ns: 'hitfood').raw).to eq 'zzz'
52
+ expect(Settings.get(:test, ns: 'main').raw).to eq ''
53
+ end
54
+
55
+ it 'falls back to default ns' do
56
+ Settings.ns_default = 'main'
57
+ Settings.ns_fallback = 'main'
58
+
59
+ Settings.ns(:main).test = 'main'
60
+ Settings.ns(:other).test = 'other'
61
+
62
+ expect(Settings.ns('main').test).to eq 'main'
63
+ expect(Settings.ns('other').test).to eq 'other'
64
+ expect(Settings.ns('other1').test).to eq 'main'
65
+ expect(Settings.ns('other2', fallback: nil).test).to eq ''
66
+ end
67
+ end