Class: Settings

Inherits:
BasicObject
Defined in:
lib/rails_admin_settings/settings.rb

Overview

we are inheriting from BasicObject so we don’t get a bunch of methods from Kernel or Object

Constant Summary collapse

@@file_uploads_supported =
false
@@file_uploads_engine =
false
@@namespaces =
{}
@@mutex =
::Mutex.new
@@ns_default =
'main'
@@ns_fallback =
nil

Class Method Summary collapse

Class Method Details

.apply_defaults!(file, verbose = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails_admin_settings/settings.rb', line 66

def apply_defaults!(file, verbose = false)
  if File.file?(file)
    puts "[settings] Loading from #{file}".freeze if verbose
    yaml = YAML.load(File.read(file), safe: true)
    yaml.each_pair do |namespace, vals|
      vals.symbolize_keys!
      n = ns(namespace)
      vals.each_pair do |key, val|
        val.symbolize_keys!
        if !val[:kind].nil? && (val[:kind] == 'file' || val[:kind] == 'image')
          unless @@file_uploads_supported
            ::Kernel.raise ::RailsAdminSettings::PersistenceException, "Fatal: setting #{key} is #{val[:type]} but file upload engine is not detected".freeze
          end
          value = File.open(root_file_path.join(val.delete(:value)))
        else
          value = val.delete(:value)
        end
        puts "#{key} - default '#{value}' current '#{Settings.get(key).raw}'".freeze if verbose
        n.set(key, value, val.merge(overwrite: false))
      end
      n.unload!
    end
  end
end

.create_setting(key, value, options = {}) ⇒ Object



105
106
107
# File 'lib/rails_admin_settings/settings.rb', line 105

def create_setting(key, value, options = {})
  set(key, nil, options.merge(overwrite: false))
end

.destroy_all!Object



53
54
55
56
# File 'lib/rails_admin_settings/settings.rb', line 53

def destroy_all!
  RailsAdminSettings::Setting.destroy_all
  unload!
end

.get(key, options = {}) ⇒ Object



91
92
93
94
# File 'lib/rails_admin_settings/settings.rb', line 91

def get(key, options = {})
  options.symbolize_keys!
  ns(options[:ns], options).get(key, options)
end

.get_default_nsObject



36
37
38
# File 'lib/rails_admin_settings/settings.rb', line 36

def get_default_ns
  ns(nil, fallback: @@ns_fallback)
end

.method_missing(*args) ⇒ Object



109
110
111
# File 'lib/rails_admin_settings/settings.rb', line 109

def method_missing(*args)
  get_default_ns.__send__(*args)
end

.ns(name, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_admin_settings/settings.rb', line 19

def ns(name, options = {})
  options.symbolize_keys!
  if name.nil? || name == @@ns_default
    name = @@ns_default.to_s
  else
    name = name.to_s
  end
  if options.key?(:type)
    options[:kind] = options.delete(:type)
  end
  @@mutex.synchronize do
    @@namespaces[name] ||= ::RailsAdminSettings::Namespaced.new(name.to_s)
  end
  fallback = options.key?(:fallback) ? options[:fallback] : @@ns_fallback
  ::RailsAdminSettings::Fallback.new(@@namespaces[name], fallback)
end

.root_file_pathObject



58
59
60
61
62
63
64
# File 'lib/rails_admin_settings/settings.rb', line 58

def root_file_path
  if Object.const_defined?('Rails')
    Rails.root
  else
    Pathname.new(File.dirname(__FILE__)).join('../..')
  end
end

.save_default(key, value, options = {}) ⇒ Object



101
102
103
# File 'lib/rails_admin_settings/settings.rb', line 101

def save_default(key, value, options = {})
  set(key, value, options.merge(overwrite: false))
end

.set(key, value = nil, options = {}) ⇒ Object



96
97
98
99
# File 'lib/rails_admin_settings/settings.rb', line 96

def set(key, value = nil, options = {})
  options.symbolize_keys!
  ns(options[:ns], options).set(key, value, options)
end

.table_exists?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rails_admin_settings/settings.rb', line 40

def table_exists?
  RailsAdminSettings.mongoid? || RailsAdminSettings::Setting.table_exists?
end

.unload!Object



44
45
46
47
48
49
50
51
# File 'lib/rails_admin_settings/settings.rb', line 44

def unload!
  @@mutex.synchronize do
    @@namespaces.values.map(&:unload!)
    @@namespaces = {}
    @@ns_default = 'main'
    @@ns_fallback = nil
  end
end