Module: SystemSettings

Defined in:
lib/system_settings.rb,
lib/system_settings/engine.rb,
lib/system_settings/version.rb

Defined Under Namespace

Modules: ApplicationHelper, Errors, Type Classes: ApplicationController, ApplicationRecord, BooleanSetting, Configurator, DecimalListSetting, DecimalSetting, Engine, IntegerListSetting, IntegerSetting, ListOfDecimalsValidator, ListOfIntegersValidator, ListOfStringsValidator, Setting, SettingsController, StringListSetting, StringSetting

Constant Summary collapse

VERSION =
"0.9.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.instrumenterObject

Returns the value of attribute instrumenter.



8
9
10
# File 'lib/system_settings.rb', line 8

def instrumenter
  @instrumenter
end

.settings_file_pathObject

Returns the value of attribute settings_file_path.



8
9
10
# File 'lib/system_settings.rb', line 8

def settings_file_path
  @settings_file_path
end

Class Method Details

.[](name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/system_settings.rb', line 11

def self.[](name)
  instrument("system_settings.find", name: name) do |payload|
    record = Setting.find_by(name: name)
    unless record
      message = <<~MESSAGE.strip
        Couldn't find system setting #{name}

        It might not be loaded from settings file(#{settings_file_path}).
        To load missing settings with their initial values you can call SystemSettings.load from your Rails environment or run Rails task:

            bin/rails system_settings:load RAILS_ENV=#{::Rails.env}
      MESSAGE
      raise(Errors::NotFoundError, message)
    end
    payload[:value] = record.value
  end
end

.instrument(name, payload = {}, &block) ⇒ Object



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

def self.instrument(name, payload = {}, &block)
  if instrumenter
    instrumenter.instrument(name, payload, &block)
  else
    yield(payload)
  end
end

.load(*names) ⇒ Object



29
30
31
32
33
34
# File 'lib/system_settings.rb', line 29

def self.load(*names)
  instrument("system_settings.load", path: settings_file_path) do |payload|
    configurator = Configurator.from_file(settings_file_path)
    payload[:success] = configurator.persist(only: names)
  end
end

.reset_to_defaultsObject



36
37
38
39
40
41
42
43
# File 'lib/system_settings.rb', line 36

def self.reset_to_defaults
  instrument("system_settings.reset_to_defaults", path: settings_file_path) do |payload|
    configurator = Configurator.from_file(settings_file_path)
    Setting.transaction do
      payload[:success] = configurator.purge && configurator.persist
    end
  end
end