ack_rails_admin_settings 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
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,65 @@
1
+ module RailsAdminSettings
2
+ module RequireHelpers
3
+ private
4
+
5
+ def require_russian_phone
6
+ begin
7
+ require 'russian_phone'
8
+ yield
9
+ rescue LoadError => e
10
+ e.message << " [rails_admin_settings] Please add gem 'russian_phone' to use phone settings".freeze
11
+ raise e
12
+ end
13
+ end
14
+
15
+ def require_safe_yaml
16
+ begin
17
+ require 'safe_yaml'
18
+ yield
19
+ rescue LoadError => e
20
+ e.message << " [rails_admin_settings] Please add gem 'safe_yaml' to your Gemfile to use yaml settings".freeze
21
+ raise e
22
+ end
23
+ end
24
+
25
+ def require_sanitize
26
+ begin
27
+ require 'sanitize'
28
+ yield
29
+ rescue LoadError => e
30
+ e.message << " [rails_admin_settings] Please add gem 'sanitize' to your Gemfile to use sanitized settings".freeze
31
+ raise e
32
+ end
33
+ end
34
+
35
+ def require_validates_email_format_of
36
+ begin
37
+ require 'validates_email_format_of'
38
+ yield
39
+ rescue LoadError => e
40
+ e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email kind settings".freeze
41
+ raise e
42
+ end
43
+ end
44
+
45
+ def require_geocoder
46
+ begin
47
+ require 'geocoder'
48
+ yield
49
+ rescue LoadError => e
50
+ e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email kind settings".freeze
51
+ raise e
52
+ end
53
+ end
54
+
55
+ def require_addressable
56
+ begin
57
+ require 'addressable/uri'
58
+ yield
59
+ rescue LoadError => e
60
+ e.message << " [rails_admin_settings] Please add gem 'addressable' to your Gemfile to use url/domain kind settings".freeze
61
+ raise e
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,113 @@
1
+ require 'thread'
2
+
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
7
+ @@file_uploads_supported = false
8
+ @@file_uploads_engine = false
9
+ @@namespaces = {}
10
+ @@mutex = ::Mutex.new
11
+
12
+ @@ns_default = 'main'
13
+ @@ns_fallback = nil
14
+ cattr_accessor :ns_default, :ns_fallback, :namespaces
15
+
16
+ cattr_reader :mutex
17
+
18
+ class << self
19
+ def ns(name, options = {})
20
+ options.symbolize_keys!
21
+ if name.nil? || name == @@ns_default
22
+ name = @@ns_default.to_s
23
+ else
24
+ name = name.to_s
25
+ end
26
+ if options.key?(:type)
27
+ options[:kind] = options.delete(:type)
28
+ end
29
+ @@mutex.synchronize do
30
+ @@namespaces[name] ||= ::RailsAdminSettings::Namespaced.new(name.to_s)
31
+ end
32
+ fallback = options.key?(:fallback) ? options[:fallback] : @@ns_fallback
33
+ ::RailsAdminSettings::Fallback.new(@@namespaces[name], fallback)
34
+ end
35
+
36
+ def get_default_ns
37
+ ns(nil, fallback: @@ns_fallback)
38
+ end
39
+
40
+ def table_exists?
41
+ RailsAdminSettings.mongoid? || RailsAdminSettings::Setting.table_exists?
42
+ end
43
+
44
+ def unload!
45
+ @@mutex.synchronize do
46
+ @@namespaces.values.map(&:unload!)
47
+ @@namespaces = {}
48
+ @@ns_default = 'main'
49
+ @@ns_fallback = nil
50
+ end
51
+ end
52
+
53
+ def destroy_all!
54
+ RailsAdminSettings::Setting.destroy_all
55
+ unload!
56
+ end
57
+
58
+ def root_file_path
59
+ if Object.const_defined?('Rails')
60
+ Rails.root
61
+ else
62
+ Pathname.new(File.dirname(__FILE__)).join('../..')
63
+ end
64
+ end
65
+
66
+ def apply_defaults!(file, verbose = false)
67
+ if File.file?(file)
68
+ puts "[settings] Loading from #{file}".freeze if verbose
69
+ yaml = YAML.load(File.read(file), safe: true)
70
+ yaml.each_pair do |namespace, vals|
71
+ vals.symbolize_keys!
72
+ n = ns(namespace)
73
+ vals.each_pair do |key, val|
74
+ val.symbolize_keys!
75
+ if !val[:kind].nil? && (val[:kind] == 'file' || val[:kind] == 'image')
76
+ unless @@file_uploads_supported
77
+ ::Kernel.raise ::RailsAdminSettings::PersistenceException, "Fatal: setting #{key} is #{val[:type]} but file upload engine is not detected".freeze
78
+ end
79
+ value = File.open(root_file_path.join(val.delete(:value)))
80
+ else
81
+ value = val.delete(:value)
82
+ end
83
+ puts "#{key} - default '#{value}' current '#{Settings.get(key).raw}'".freeze if verbose
84
+ n.set(key, value, val.merge(overwrite: false))
85
+ end
86
+ n.unload!
87
+ end
88
+ end
89
+ end
90
+
91
+ def get(key, options = {})
92
+ options.symbolize_keys!
93
+ ns(options[:ns], options).get(key, options)
94
+ end
95
+
96
+ def set(key, value = nil, options = {})
97
+ options.symbolize_keys!
98
+ ns(options[:ns], options).set(key, value, options)
99
+ end
100
+
101
+ def save_default(key, value, options = {})
102
+ set(key, value, options.merge(overwrite: false))
103
+ end
104
+
105
+ def create_setting(key, value, options = {})
106
+ set(key, nil, options.merge(overwrite: false))
107
+ end
108
+
109
+ def method_missing(*args)
110
+ get_default_ns.__send__(*args)
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,9 @@
1
+ module RailsAdminSettings
2
+ module Uploads
3
+ class CarrierWave < CarrierWave::Uploader::Base
4
+ def extension_white_list
5
+ %w(jpg jpeg gif png tiff psd ai txt rtf doc docx xls xlsx ppt pptx odt odx zip rar 7z pdf)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
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; use rake settings:dump[production] to create env-specific template".freeze
15
+ task :dump, [:as_env] => ['settings:require'] do |t, args|
16
+ if args.empty? || args[:as_env].blank?
17
+ path = Settings.root_file_path.join('config/settings.yml')
18
+ else
19
+ path = Settings.root_file_path.join("config/settings.#{args[:as_env]}.yml")
20
+ end
21
+ RailsAdminSettings::Dumper.dump(path)
22
+ puts "dumped settings to #{path}"
23
+ end
24
+
25
+ desc "Load settings from config/settings.yml without overwriting current values".freeze
26
+ task :load => ['settings:require'] do
27
+ Settings.apply_defaults!(Rails.root.join("config/settings.#{Rails.env.to_s}.yml"), true)
28
+ Settings.apply_defaults!(Rails.root.join('config/settings.yml'), true)
29
+ end
30
+
31
+ desc "Delete all settings".freeze
32
+ task :delete => ['settings:require'] do
33
+ Settings.destroy_all!
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ module RailsAdminSettings
2
+ module Uploads
3
+ autoload :CarrierWave, "rails_admin_settings/storage/carrierwave"
4
+ def self.included(base)
5
+ # carrierwave
6
+ if base.respond_to?(:mount_uploader)
7
+ # puts "[rails_admin_settings] CarrierWave detected"
8
+ # base.field(:file, type: String)
9
+ base.mount_uploader(:file, RailsAdminSettings::Uploads::CarrierWave)
10
+ Settings.file_uploads_supported = true
11
+ Settings.file_uploads_engine = :carrierwave
12
+ # mongoid-paperclip
13
+ elsif RailsAdminSettings.mongoid? && ::Mongoid.const_defined?('Paperclip')
14
+ base.send(:include, ::Mongoid::Paperclip)
15
+ # puts "[rails_admin_settings] PaperClip detected"
16
+ base.field(:file, type: String)
17
+ if defined?(Rails)
18
+ base.has_mongoid_attached_file(:file)
19
+ else
20
+ base.has_mongoid_attached_file(:file, path: "#{File.dirname(__FILE__)}/../../uploads/:filename", url: '/uploads/:filename')
21
+ end
22
+ if base.respond_to?(:do_not_validate_attachment_file_type)
23
+ base.do_not_validate_attachment_file_type :file
24
+ end
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
37
+ Settings.file_uploads_supported = true
38
+ Settings.file_uploads_engine = :paperclip
39
+ else
40
+ # puts "[rails_admin_settings] Uploads disabled"
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,84 @@
1
+ module RailsAdminSettings
2
+ module Validation
3
+ def self.included(base)
4
+ base.before_validation do
5
+ self.raw = default_serializable_value if raw.blank?
6
+ end
7
+ base.before_validation :sanitize_value, if: :sanitized_kind?
8
+ base.validates_uniqueness_of :key, scope: :ns
9
+ base.validates_inclusion_of :kind, in: RailsAdminSettings.kinds
10
+ base.validates_numericality_of :raw, if: :integer_kind?
11
+
12
+ base.validate if: :phone_kind? do
13
+ require_russian_phone do
14
+ errors.add(:raw, I18n.t('admin.settings.phone_invalid')) unless raw.blank? || RussianPhone::Number.new(raw).valid?
15
+ end
16
+ end
17
+
18
+ base.validate if: :phones_kind? do
19
+ require_russian_phone do
20
+ unless raw.blank?
21
+ invalid_phones = raw.gsub("\r", '').split("\n").inject([]) do |memo, value|
22
+ memo << value unless RussianPhone::Number.new(value).valid?
23
+ memo
24
+ end
25
+ errors.add(:raw, I18n.t('admin.settings.phones_invalid', phones: invalid_phones * ', ')) unless invalid_phones.empty?
26
+ end
27
+ end
28
+ end
29
+
30
+ base.validate if: :email_kind? do
31
+ require_validates_email_format_of do
32
+ errors.add(:raw, I18n.t('admin.settings.email_invalid')) unless raw.blank? || ValidatesEmailFormatOf.validate_email_format(raw).nil?
33
+ end
34
+ end
35
+
36
+ base.validate if: :address_kind? do
37
+ require_geocoder do
38
+ # just raise error if we are trying to use address kind without geocoder
39
+ end
40
+ end
41
+
42
+ base.validate if: :file_kind? do
43
+ unless Settings.file_uploads_supported
44
+ raise '[rails_admin_settings] File kind requires either CarrierWave or Paperclip. Check that rails_admin_settings is below them in Gemfile'.freeze
45
+ end
46
+ end
47
+
48
+ base.before_validation if: :url_kind? do
49
+ require_addressable do
50
+ self.raw = Addressable::URI.heuristic_parse(self.raw) unless self.raw.blank?
51
+ end
52
+ end
53
+
54
+ base.before_validation if: :domain_kind? do
55
+ require_addressable do
56
+ self.raw = Addressable::URI.heuristic_parse(self.raw).host unless self.raw.blank?
57
+ end
58
+ end
59
+
60
+ if Object.const_defined?('Geocoder')
61
+ if RailsAdminSettings.mongoid?
62
+ base.field(:coordinates, type: Array)
63
+ base.send(:include, Geocoder::Model::Mongoid)
64
+ end
65
+ base.geocoded_by(:raw)
66
+ base.after_validation(:geocode, if: :address_kind?)
67
+ end
68
+
69
+ base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw, if: :color_kind?)
70
+
71
+ base.validate if: :yaml_kind? do
72
+ require_safe_yaml do
73
+ unless raw.blank?
74
+ begin
75
+ YAML.safe_load(raw)
76
+ rescue Psych::SyntaxError => e
77
+ errors.add(:raw, I18n.t('admin.settings.yaml_invalid'))
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminSettings
2
+ VERSION = "1.1.4".freeze
3
+ end
@@ -0,0 +1,81 @@
1
+ require "rails_admin_settings/version"
2
+
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
+
22
+ class PersistenceException < Exception
23
+ end
24
+
25
+ autoload :Mongoid, "rails_admin_settings/mongoid"
26
+ autoload :Fallback, "rails_admin_settings/fallback"
27
+ autoload :Namespaced, "rails_admin_settings/namespaced"
28
+ autoload :Processing, "rails_admin_settings/processing"
29
+ autoload :Validation, "rails_admin_settings/validation"
30
+ autoload :RequireHelpers, "rails_admin_settings/require_helpers"
31
+ autoload :RailsAdminConfig, "rails_admin_settings/rails_admin_config"
32
+ autoload :Uploads, "rails_admin_settings/uploads"
33
+ autoload :HexColorValidator, "rails_admin_settings/hex_color_validator"
34
+ autoload :Dumper, "rails_admin_settings/dumper"
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
+
51
+ def self.track_history!
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!".freeze
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!".freeze
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
71
+ end
72
+ end
73
+
74
+ require "rails_admin_settings/kinds"
75
+ require "rails_admin_settings/settings"
76
+
77
+ if Object.const_defined?('Rails')
78
+ require "rails_admin_settings/engine"
79
+ else
80
+ require File.dirname(__FILE__) + '/../app/models/rails_admin_settings/setting.rb'
81
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_admin_settings/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ack_rails_admin_settings"
8
+ spec.version = RailsAdminSettings::VERSION
9
+ spec.authors = ["Alexander Kiseliev", "Gleb Tv"]
10
+ spec.email = ['i43ack@gmail.com', "glebtv@gmail.com"]
11
+ spec.description = %q{Mongoid / ActiveRecord + RailsAdmin App Settings management}
12
+ spec.summary = %q{}
13
+ spec.homepage = "https://github.com/ack43/rails_admin_settings"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+
22
+ spec.add_development_dependency "mongoid", [">= 3.0", "< 6.0"]
23
+ spec.add_development_dependency "rails"
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "mongoid-rspec"
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "database_cleaner"
30
+ spec.add_development_dependency "factory_girl"
31
+ spec.add_development_dependency "safe_yaml"
32
+ spec.add_development_dependency "russian_phone"
33
+ spec.add_development_dependency "sanitize"
34
+ spec.add_development_dependency "validates_email_format_of"
35
+ spec.add_development_dependency "geocoder"
36
+ spec.add_development_dependency "addressable"
37
+ spec.add_development_dependency "glebtv-carrierwave-mongoid"
38
+ spec.add_development_dependency "glebtv-mongoid-paperclip"
39
+ spec.add_development_dependency "pry"
40
+ end
data/release.sh ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/bash
2
+ bundle update
3
+ git add --all .
4
+ git commit -am "${*:1}"
5
+ git push
6
+ rake release
@@ -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 rails_admin_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
+ RailsAdminSettings::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
+ RailsAdminSettings::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
+ RailsAdminSettings::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
+ Settings.ns(:main).phone.should eq '906 1111111'
56
+ Settings.ns(:main).true_setting.should be true
57
+ Settings.ns(:main).false_setting.should be false
58
+ Settings.ns(:other).footer.should eq 'zzz'
59
+ Settings.ns(:main).footer.should eq 'test <b></b>'
60
+ Settings.footer.should 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