Module: ThecoreSettings

Defined in:
lib/thecore_settings.rb,
lib/thecore_settings/kinds.rb,
lib/thecore_settings/dumper.rb,
lib/thecore_settings/engine.rb,
lib/thecore_settings/mongoid.rb,
lib/thecore_settings/uploads.rb,
lib/thecore_settings/version.rb,
lib/thecore_settings/fallback.rb,
lib/thecore_settings/namespaced.rb,
lib/thecore_settings/processing.rb,
lib/thecore_settings/validation.rb,
app/models/thecore_settings/setting.rb,
app/models/thecore_settings/setting.rb,
lib/thecore_settings/require_helpers.rb,
lib/thecore_settings/rails_admin_config.rb,
lib/thecore_settings/hex_color_validator.rb,
lib/thecore_settings/storage/shrine_uploader.rb,
lib/thecore_settings/storage/carrier_wave_uploader.rb

Overview

require “thecore_settings/version”

Defined Under Namespace

Modules: Dumper, Mongoid, Processing, RailsAdminConfig, RequireHelpers, Uploads, Validation Classes: Engine, Fallback, HexColorValidator, Namespaced, NoRailsError, PersistenceException, Setting

Constant Summary collapse

VERSION =
"3.0.6".freeze
@@scrubber =
Rails::Html::WhiteListSanitizer.new

Class Method Summary collapse

Class Method Details

.active_record?Boolean

Returns:

  • (Boolean)


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

def active_record?
  orm == :active_record
end

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



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/thecore_settings.rb', line 40

def apply_defaults!(file, verbose = false)
  if File.file?(file)
    puts "[settings] Loading from #{file}" if verbose
    if defined?(Psych) && Psych.respond_to?(:safe_load)
      yaml = Psych.safe_load(File.read(file))
    else
      yaml = YAML.load(File.read(file), safe: true)
    end
    yaml.each_pair do |namespace, vals|
      process_defaults(namespace, vals, verbose)
    end
  end
end

.kindsObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/thecore_settings/kinds.rb', line 2

def self.kinds
  [
    'string',
    'text',
    'integer',
    'float',
    'boolean',
    'html',
    'code',
    'sanitized',
    'yaml',
    'phone',
    'phones',
    'email',
    'address',
    'file',
    'image',
    'url',
    'domain',
    'color',
    'strip_tags',
    'sanitize',
    'sanitize_code',
    'simple_format',
    'simple_format_raw',
    'json',
  ]
end

.migrate!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/thecore_settings.rb', line 73

def migrate!
  if ThecoreSettings.mongoid?
    ThecoreSettings::Setting.where(:ns.exists => false).update_all(ns: 'main')
    ThecoreSettings::Setting.all.each do |s|
      s.kind = s.read_attribute(:type) if !s.read_attribute(:type).blank? && s.kind != s.read_attribute(:type)
      s.save! if s.changed?
      s.unset(:type)
    end
  else
    if Settings.table_exists?
      ThecoreSettings::Setting.where("ns IS NULL").update_all(ns: 'main')
    end
  end
end

.mongoid?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/thecore_settings.rb', line 32

def mongoid?
  orm == :mongoid
end

.ormObject



24
25
26
27
28
29
30
# File 'lib/thecore_settings.rb', line 24

def orm
  if defined?(::Mongoid)
    :mongoid
  else
    :active_record
  end
end

.process_defaults(namespace, vals, verbose = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/thecore_settings.rb', line 54

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

.track_history!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/thecore_settings.rb', line 88

def track_history!
  return false unless Settings.table_exists?

  if mongoid?
    if ::Mongoid.const_defined?('History')
      ThecoreSettings::Setting.send(:include, ::Mongoid::History::Trackable)
      ThecoreSettings::Setting.send(:track_history, {track_create: true, track_destroy: true})
    else
      puts "[thecore_settings] WARN unable to track_history: Mongoid::History not loaded!"
    end
    if ::Mongoid.const_defined?('Userstamp')
      ThecoreSettings::Setting.send(:include, ::Mongoid::Userstamp)
    else
      puts "[thecore_settings] WARN unable to track_history: Mongoid::Userstamp not loaded!"
    end
  elsif active_record?
    if defined?(PaperTrail) && PaperTrail::Version.table_exists?
      ThecoreSettings::Setting.send(:has_paper_trail)
    end
  end
end

.typesObject



31
32
33
# File 'lib/thecore_settings/kinds.rb', line 31

def self.types
  self.kinds
end