Class: Goldencobra::Setting
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Goldencobra::Setting
- Defined in:
- app/models/goldencobra/setting.rb
Constant Summary collapse
- SettingsDataTypes =
["string","date","datetime","boolean","array"]
- @@key_value =
{}
Class Method Summary collapse
- .absolute_base_url ⇒ Object
- .for_key(name, cacheable = true) ⇒ Object
- .for_key_helper(name) ⇒ Object
- .import_default_settings(path_file_name) ⇒ Object
- .regenerate_active_admin ⇒ Object
- .set_value_for_key(value, name, data_type_name = "string") ⇒ Object
Instance Method Summary collapse
Class Method Details
.absolute_base_url ⇒ Object
35 36 37 38 39 40 41 |
# File 'app/models/goldencobra/setting.rb', line 35 def self.absolute_base_url if Goldencobra::Setting.for_key("goldencobra.use_ssl") == "true" "https://#{Goldencobra::Setting.for_key('goldencobra.url')}" else "http://#{Goldencobra::Setting.for_key('goldencobra.url')}" end end |
.for_key(name, cacheable = true) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'app/models/goldencobra/setting.rb', line 52 def self.for_key(name, cacheable=true) if cacheable @@key_value ||= {} @@key_value[name] ||= for_key_helper(name) else for_key_helper(name) end end |
.for_key_helper(name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/goldencobra/setting.rb', line 61 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
100 101 102 103 104 105 106 107 108 109 |
# File 'app/models/goldencobra/setting.rb', line 100 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_admin ⇒ Object
43 44 45 46 47 48 49 |
# File 'app/models/goldencobra/setting.rb', line 43 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/goldencobra/setting.rb', line 79 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
#parent_names ⇒ Object
111 112 113 |
# File 'app/models/goldencobra/setting.rb', line 111 def parent_names self.ancestors.map(&:title).join(".") end |