Class: Goldencobra::Setting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/goldencobra/setting.rb

Constant Summary collapse

SettingsDataTypes =
["string","date","datetime","boolean","array"]
@@key_value =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.absolute_base_urlObject



37
38
39
# File 'app/models/goldencobra/setting.rb', line 37

def self.absolute_base_url
  Goldencobra::Url.to_s
end

.for_key(name, cacheable = true) ⇒ Object

Goldencobra::Setting.for_key(“test.foo.bar”)



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/goldencobra/setting.rb', line 50

def self.for_key(name, cacheable=true)
  @@mtime_setting ||= {}
  mtime = get_cache_modification_time(name)

  if cacheable && @@mtime_setting[name].present? && @@mtime_setting[name] >= mtime
    @@key_value ||= {}
    @@key_value[name] ||= for_key_helper(name)
  else
    @@mtime_setting[name] = mtime
    for_key_helper(name)
  end

end

.for_key_helper(name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/goldencobra/setting.rb', line 64

def self.for_key_helper(name)
if ActiveRecord::Base.connection.table_exists?("goldencobra_settings")
  setting_title = name.split(".").last
  settings = Goldencobra::Setting.where(title: setting_title)
  if settings.count == 1
    return settings.first.value
  elsif settings.count > 1
    settings.each do |set|
      if [set.ancestors.map(&:title).join("."),setting_title].compact.join('.') == name
        return set.value
      end
    end
  else
    return setting_title
  end
end
end

.import_default_settings(path_file_name) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'app/models/goldencobra/setting.rb', line 103

def self.import_default_settings(path_file_name)
  if ActiveRecord::Base.connection.table_exists?("goldencobra_settings")
    require 'yaml'
    raise "Settings File '#{path_file_name}' does not exist" if !File.exists?(path_file_name)
    imports = open(path_file_name) { |f| YAML.load(f) }
    imports.each_key do |key|
      generate_default_setting(key, imports)
    end
  end
end

.regenerate_active_adminObject



41
42
43
44
45
46
47
# File 'app/models/goldencobra/setting.rb', line 41

def self.regenerate_active_admin
  if defined?(ActiveAdmin) and ActiveAdmin.application
    ActiveAdmin.application.unload!
    ActiveSupport::Dependencies.clear
    ActiveAdmin.application.load!
  end
end

.set_value_for_key(value, name, data_type_name = "string") ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/goldencobra/setting.rb', line 82

def self.set_value_for_key(value, name, data_type_name="string")
  @@key_value = nil
  if ActiveRecord::Base.connection.table_exists?("goldencobra_settings")
    setting_title = name.split(".").last
    settings = Goldencobra::Setting.where(title: setting_title)
    if settings.count == 1
      settings.first.update_attributes(value: value, data_type: data_type_name)
      true
    elsif settings.count > 1
      settings.each do |set|
        if [set.ancestors.map(&:title).join("."),setting_title].compact.join('.') == name
          set.update_attributes(value: value, data_type: data_type_name)
          true
        end
      end
    else
      false
    end
  end
end

Instance Method Details

#has_childrenObject



122
123
124
# File 'app/models/goldencobra/setting.rb', line 122

def has_children
  self.has_children?
end

#parent_namesObject



114
115
116
# File 'app/models/goldencobra/setting.rb', line 114

def parent_names
  self.ancestors.map(&:title).join(".")
end

#path_nameObject



118
119
120
# File 'app/models/goldencobra/setting.rb', line 118

def path_name
  self.path.map(&:title).join(".")
end