ack_rails_admin_settings 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +1 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +17 -0
  7. data/CHANGELOG.md +53 -0
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +192 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +133 -0
  12. data/Rakefile +9 -0
  13. data/app/models/rails_admin_settings/setting.rb +78 -0
  14. data/app/views/rails_admin/main/_setting_value.html.haml +104 -0
  15. data/config/locales/en.yml +28 -0
  16. data/config/locales/ru.yml +28 -0
  17. data/gemfiles/mongoid-3.1.gemfile +5 -0
  18. data/gemfiles/mongoid-4.0.gemfile +5 -0
  19. data/lib/ack_rails_admin_settings.rb +1 -0
  20. data/lib/generators/rails_admin_settings/migration_generator.rb +15 -0
  21. data/lib/generators/rails_admin_settings/templates/migration.rb +26 -0
  22. data/lib/rails_admin_settings/dumper.rb +12 -0
  23. data/lib/rails_admin_settings/engine.rb +17 -0
  24. data/lib/rails_admin_settings/fallback.rb +21 -0
  25. data/lib/rails_admin_settings/hex_color_validator.rb +11 -0
  26. data/lib/rails_admin_settings/kinds.rb +30 -0
  27. data/lib/rails_admin_settings/mongoid.rb +31 -0
  28. data/lib/rails_admin_settings/namespaced.rb +218 -0
  29. data/lib/rails_admin_settings/processing.rb +159 -0
  30. data/lib/rails_admin_settings/rails_admin_config.rb +74 -0
  31. data/lib/rails_admin_settings/require_helpers.rb +65 -0
  32. data/lib/rails_admin_settings/settings.rb +113 -0
  33. data/lib/rails_admin_settings/storage/carrierwave.rb +9 -0
  34. data/lib/rails_admin_settings/tasks.rb +35 -0
  35. data/lib/rails_admin_settings/uploads.rb +45 -0
  36. data/lib/rails_admin_settings/validation.rb +84 -0
  37. data/lib/rails_admin_settings/version.rb +3 -0
  38. data/lib/rails_admin_settings.rb +81 -0
  39. data/rails_admin_settings.gemspec +40 -0
  40. data/release.sh +6 -0
  41. data/spec/advanced_usage_spec.rb +11 -0
  42. data/spec/carrierwave_spec.rb +41 -0
  43. data/spec/database_trickery_spec.rb +48 -0
  44. data/spec/defaults_spec.rb +87 -0
  45. data/spec/enabling_spec.rb +29 -0
  46. data/spec/factories/setting.rb +8 -0
  47. data/spec/label_spec.rb +18 -0
  48. data/spec/migration_spec.rb +15 -0
  49. data/spec/model_spec.rb +45 -0
  50. data/spec/namespaced_spec.rb +67 -0
  51. data/spec/paperclip_spec.rb +39 -0
  52. data/spec/settings_spec.rb +75 -0
  53. data/spec/spec_helper.rb +42 -0
  54. data/spec/support/1024x768.gif +0 -0
  55. data/spec/support/database_cleaner.rb +10 -0
  56. data/spec/support/defaults.yml +23 -0
  57. data/spec/support/defaults_w_file.yml +19 -0
  58. data/spec/support/mongoid.rb +6 -0
  59. data/spec/support/mongoid.yml +6 -0
  60. data/spec/types_spec.rb +88 -0
  61. metadata +383 -0
@@ -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
+ FactoryGirl.define do
4
+ factory :setting, class: RailsAdminSettings::Setting do
5
+ sequence(:key){|n| "setting_#{n}" }
6
+ raw "Контент 1"
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings label' do
6
+
7
+ it "should have label" do
8
+ label = "E-Mail"
9
+ Settings.email(label: label, default: "my@mail.ru")
10
+ expect(Settings.get(:email).name).to eq(label)
11
+ end
12
+
13
+ it "should properly set label as key if blank" do
14
+ Settings.email(default: "my@mail.ru")
15
+ expect(Settings.get(:email).name).to eq('email')
16
+ end
17
+
18
+ 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
+
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RailsAdminSettings::Setting do
6
+ it { is_expected.to have_fields(:enabled, :key, :kind, :raw) }
7
+
8
+ it "correctly return content when enabled" do
9
+ setting = FactoryGirl.create(:setting)
10
+ expect(setting.to_s).to eq "Контент 1"
11
+ end
12
+
13
+ it "return empty string when disabled" do
14
+ setting = FactoryGirl.create(:setting, enabled: false)
15
+ expect(setting.to_s).to eq ""
16
+ end
17
+
18
+ it "correctly process {{year}}" do
19
+ setting = FactoryGirl.create(:setting, raw: '© {{year}} company')
20
+ expect(setting.val).to eq "© #{Time.now.strftime('%Y')} company"
21
+ end
22
+
23
+ it "correctly process {{year|2010}}" do
24
+ setting = FactoryGirl.create(:setting, raw: '© {{year|2010}} company')
25
+ expect(setting.val).to eq "© 2010-#{Time.now.strftime('%Y')} company"
26
+ end
27
+
28
+ it "correctly process {{year|current_year}}" do
29
+ setting = FactoryGirl.create(:setting, raw: '© {{year|' + Time.now.strftime('%Y') + '}} company')
30
+ expect(setting.val).to eq "© #{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 = FactoryGirl.create(:setting, raw: '© {{year}} company', kind: 'html')
36
+ expect(setting.val).to eq "© #{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 = FactoryGirl.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
+ 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
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe "Uploads" do
5
+ if Settings.file_uploads_engine != :paperclip
6
+ pending "paperclip not detected, skipped. To run use UPLOADS=paperclip rspec"
7
+ else
8
+ Paperclip.options[:log] = false
9
+ before :each do
10
+ f = "#{File.dirname(__FILE__)}/../uploads/1024x768.gif"
11
+ if File.file?(f)
12
+ File.unlink(f)
13
+ end
14
+ end
15
+ it 'supports file kind' do
16
+ Settings.set('file', File.open("#{File.dirname(__FILE__)}/support/1024x768.gif"), kind: 'file')
17
+ expect(Settings.get(:file).file_file_name).to eq '1024x768.gif'
18
+ expect(Settings.get(:file).file_file_size).to eq 4357
19
+ expect(Settings.file[0..21]).to eq '/uploads/1024x768.gif?'
20
+
21
+ expect(File.exists?("#{File.dirname(__FILE__)}/../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
+ expect(Settings.get(:file).file_file_name).to eq '1024x768.gif'
27
+ expect(Settings.get(:file).file_file_size).to eq 4357
28
+ expect(Settings.file[0..21]).to eq '/uploads/1024x768.gif?'
29
+
30
+ expect(File.exists?("#{File.dirname(__FILE__)}/../uploads/1024x768.gif")).to be_truthy
31
+ end
32
+
33
+ it 'supports defaults' do
34
+ Settings.apply_defaults!(File.join(File.dirname(__FILE__), 'support/defaults_w_file.yml'))
35
+ expect(File.exists?(Settings.root_file_path.join("uploads/1024x768.gif"))).to be_truthy
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings' do
6
+ it "should works as RailsSettings" do
7
+ Settings.destroy_all!
8
+
9
+ email = "my@mail.ru"
10
+ Settings.email = email
11
+ expect(Settings.email).to eq(email)
12
+ end
13
+
14
+ it '#get should return new setting when setting does not exist' do
15
+ t = Settings.get(:test)
16
+ expect(t.class.name).to eq 'RailsAdminSettings::Setting'
17
+ expect(t.persisted?).to eq true
18
+ expect(t.value).to eq ''
19
+ end
20
+
21
+ it '#name should return empty string when setting does not exist' do
22
+ expect(Settings.test).to eq ''
23
+ expect(Settings['test'].value).to eq ''
24
+ end
25
+
26
+ it "should save default" do
27
+ Settings.destroy_all!
28
+
29
+ email = "my@mail.ru"
30
+ email2 = "my2@mail.ru"
31
+ Settings.save_default(:email, email)
32
+ expect(Settings.email).to eq(email)
33
+ Settings.email = email2
34
+ expect(Settings.email).to eq(email2)
35
+ Settings.save_default(:email, email)
36
+ expect(Settings.email).to eq(email2)
37
+ end
38
+
39
+ it 'should properly unload' do
40
+ Settings.load!
41
+ expect(Settings.loaded).to eq true
42
+ Settings.unload!
43
+ expect(Settings.loaded).to eq false
44
+ end
45
+
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
+ Settings.phone = '906 222 22 22'
49
+ expect(Settings.phone(kind: 'phone', default: '906 111 11 11')).to eq '+7 (906) 222-22-22'
50
+ end
51
+
52
+ it 'should properly store settings to DB' do
53
+ Settings.unload!
54
+ expect(Settings.loaded).to eq false
55
+ Settings.temp = '123'
56
+ expect(Settings.loaded).to eq true
57
+ Settings.unload!
58
+ expect(Settings.loaded).to eq false
59
+ expect(Settings.temp).to eq '123'
60
+ expect(Settings.loaded).to eq true
61
+ end
62
+
63
+ it 'should support yaml kind' do
64
+ Settings.tdata(kind: 'yaml')
65
+ Settings.tdata = ['one', 'two', 'three']
66
+ expect(YAML.safe_load(Settings.get(:tdata).raw)).to eq ['one', 'two', 'three']
67
+ expect(Settings.tdata).to eq ['one', 'two', 'three']
68
+ end
69
+
70
+ it '#enabled? sets defaults' do
71
+ expect(Settings.enabled?(:phone, kind: 'phone')).to eq true
72
+ expect(Settings.get(:phone).kind).to eq 'phone'
73
+ end
74
+
75
+ end
@@ -0,0 +1,42 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+
8
+ require 'pry'
9
+ require 'bundler/setup'
10
+ require 'active_support'
11
+ require 'active_support/core_ext'
12
+
13
+ unless ENV['ACTIVERECORD']
14
+ require 'mongoid'
15
+ end
16
+
17
+ require 'database_cleaner'
18
+ require 'factory_girl'
19
+ require 'mongoid-rspec'
20
+
21
+ require "glebtv-mongoid-paperclip" if ENV['UPLOADS'] == 'paperclip'
22
+ if ENV['UPLOADS'] == 'carrierwave'
23
+ require "glebtv-carrierwave-mongoid"
24
+ CarrierWave.configure do |config|
25
+ config.asset_host = proc do |file|
26
+ "http://localhost"
27
+ end
28
+ end
29
+ end
30
+
31
+ I18n.enforce_available_locales = true
32
+ I18n.load_path << File.join(File.dirname(__FILE__), "..", "config", "locales", "en.yml")
33
+
34
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
35
+ require f
36
+ end
37
+
38
+ require 'rails_admin_settings'
39
+
40
+ Dir["#{File.dirname(__FILE__)}/factories/**/*.rb"].each do |f|
41
+ require f
42
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ RSpec.configure do |config|
2
+ config.before :suite do
3
+ DatabaseCleaner.strategy = :truncation
4
+ end
5
+ config.after :each do
6
+ DatabaseCleaner.clean
7
+ Settings.destroy_all!
8
+ end
9
+ end
10
+
@@ -0,0 +1,23 @@
1
+ main:
2
+ footer:
3
+ kind: html
4
+ value: 'test <b></b>'
5
+
6
+ phone:
7
+ kind: 'phone'
8
+ value: '906 1111111'
9
+
10
+ true_setting:
11
+ kind: 'boolean'
12
+ value: true
13
+
14
+ false_setting:
15
+ kind: 'boolean'
16
+ value: false
17
+
18
+ disabled:
19
+ enabled: false
20
+
21
+ other:
22
+ footer:
23
+ value: 'zzz'
@@ -0,0 +1,19 @@
1
+ main:
2
+ footer:
3
+ kind: html
4
+ value: 'test <b></b>'
5
+
6
+ phone:
7
+ kind: 'phone'
8
+ value: '906 1111111'
9
+
10
+ disabled:
11
+ enabled: false
12
+
13
+ other:
14
+ footer:
15
+ value: 'zzz'
16
+
17
+ img:
18
+ kind: 'image'
19
+ value: 'spec/support/1024x768.gif'
@@ -0,0 +1,6 @@
1
+ ENV["MONGOID_ENV"] = "test"
2
+ Mongoid.load!("spec/support/mongoid.yml")
3
+
4
+ RSpec.configure do |configuration|
5
+ configuration.include Mongoid::Matchers
6
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: rails_admin_settings
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings kind' do
6
+ it 'boolean' do
7
+ Settings.get(:testbool, kind: 'boolean').value.should be false
8
+ Settings.get(:testbool, default: true, kind: 'boolean').value.should be false
9
+ Settings.get(:testbool2, default: true, kind: 'boolean').value.should be true
10
+ Settings.testbool2.should be true
11
+ Settings.set(:testbool3, true, kind: 'boolean')
12
+ Settings.testbool3.should be true
13
+ end
14
+
15
+ it 'html' do
16
+ expect(Settings.get(:email, kind: 'html', default: 'test@example.com').to_s).to eq 'test@example.com'
17
+ end
18
+
19
+ it 'integer' do
20
+ expect(Settings.get(:testint, kind: 'integer').value).to eq 0
21
+ expect(Settings.get(:testint, default: 5, kind: 'integer').value).to eq 0
22
+ expect(Settings.get(:testint2, default: 5, kind: 'integer').value).to eq 5
23
+ expect(Settings.testint2).to eq 5
24
+ end
25
+
26
+ it 'yaml' do
27
+ Settings.set(:data, '[one, two, three]', kind: 'yaml')
28
+ expect(Settings.get(:data).raw).to eq '[one, two, three]'
29
+ expect(Settings.data).to eq ['one', 'two', 'three']
30
+ end
31
+
32
+ it 'phone' do
33
+ Settings.set(:tphone, '906 111 11 11', kind: 'phone')
34
+ expect(Settings.get(:tphone).val.class.name).to eq 'RussianPhone::Number'
35
+ expect(Settings.tphone.class.name).to eq 'RussianPhone::Number'
36
+ expect(Settings.tphone).to eq '906 111 11 11'
37
+ expect(Settings.get(:tphone).phone_kind?).to be_truthy
38
+ expect(Settings.get(:tphone).val.city).to eq '906'
39
+ expect(Settings.get(:tphone).val.formatted_subscriber).to eq '111-11-11'
40
+ end
41
+
42
+ it 'supports phones kind' do
43
+ Settings.set(:tphone, ['906 111 11 11', '907 111 11 11'] * "\n", kind: 'phones')
44
+ expect(Settings.get(:tphone).val.class.name).to eq 'Array'
45
+ expect(Settings.tphone.class.name).to eq 'Array'
46
+ expect(Settings.get(:tphone).val.first.class.name).to eq 'RussianPhone::Number'
47
+ expect(Settings.tphone.first.class.name).to eq 'RussianPhone::Number'
48
+ expect(Settings.tphone.first).to eq '906 111 11 11'
49
+ expect(Settings.get(:tphone).phones_kind?).to be_truthy
50
+ expect(Settings.get(:tphone).val.first.city).to eq '906'
51
+ expect(Settings.get(:tphone).val.last.city).to eq '907'
52
+ expect(Settings.get(:tphone).val.first.formatted_subscriber).to eq '111-11-11'
53
+ end
54
+
55
+ it 'defaults for phone' do
56
+ Settings.dphone(kind: 'phone')
57
+ expect(Settings.get(:dphone).phone_kind?).to be_truthy
58
+ expect(Settings.dphone.formatted_area).to eq ''
59
+ expect(Settings.dphone.formatted_subscriber).to eq ''
60
+ end
61
+
62
+ it 'email validates' do
63
+ Settings.eml(kind: 'email')
64
+ expect(Settings.get(:eml).email_kind?).to be_truthy
65
+ expect { Settings.eml = '1' }.to raise_error
66
+ Settings.eml = 'test@example.com'
67
+ expect(Settings.eml).to eq 'test@example.com'
68
+ end
69
+
70
+ it 'url processing' do
71
+ Settings.url(kind: 'url')
72
+ expect(Settings.get(:url).url_kind?).to be_truthy
73
+ expect(Settings.get(:url).color_kind?).to be_falsey
74
+ Settings.url = 'test.ru'
75
+ expect(Settings.url).to eq 'http://test.ru'
76
+ end
77
+
78
+ it 'color' do
79
+ Settings.col(kind: 'color')
80
+ expect(Settings.get(:col).color_kind?).to be_truthy
81
+ expect { Settings.col = 'test'}.to raise_error
82
+ Settings.col = 'ffffff'
83
+ expect(Settings.col).to eq 'ffffff'
84
+ expect {
85
+ Settings.col = 'zzzzzz'
86
+ }.to raise_error
87
+ end
88
+ end