Class: Setting

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

Overview

Schema Information

Table name: settings

id         :integer          not null, primary key
title      :string(255)
value      :string(255)
ancestry   :string(255)
created_at :datetime         not null
updated_at :datetime         not null
data_type  :string(255)      default("string")

Constant Summary collapse

SettingsDataTypes =
["string","date","datetime","boolean","array"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.boo?(val = nil) ⇒ Boolean

Se o conteudo for do tipo boolean retorna em booleano

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'app/models/setting.rb', line 56

def self.boo?(val = nil)
  if defined?(val.boolean?) and val.boolean?
    return val.to_boolean
  else
    return val
  end
end

.cleancacheObject

limpa o cache



50
51
52
53
# File 'app/models/setting.rb', line 50

def self.cleancache
  logger.debug "====> Clean settings cache\n"
  @@key_value = {}
end

.for_key(name, cacheable = true) ⇒ Object



64
65
66
67
68
69
70
71
# File 'app/models/setting.rb', line 64

def self.for_key(name, cacheable=true)
  if cacheable
    @@key_value ||= {}
    @@key_value[name] ||= boo?(for_key_helper(name))
  else
    boo?(for_key_helper(name))
  end
end

.for_key_helper(name, object = false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/setting.rb', line 77

def self.for_key_helper(name, object=false)
  if ActiveRecord::Base.connection.table_exists?("settings")
    setting_title = name.split(".").last
    settings = Setting.where(:title => setting_title)
    if settings.count == 1
      return settings.first if object
      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 if object
          return set.value
        end
      end
    else
      return setting_title
    end
  end
end

.get_object(name) ⇒ Object



73
74
75
# File 'app/models/setting.rb', line 73

def self.get_object(name)
  for_key_helper(name, true)
end

.import_default_settings(path_file_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'app/models/setting.rb', line 118

def self.import_default_settings(path_file_name)
  if ActiveRecord::Base.connection.table_exists?("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/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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/setting.rb', line 97

def self.set_value_for_key(value, name, data_type_name="string")
  @@key_value = nil
  if ActiveRecord::Base.connection.table_exists?("settings")
    setting_title = name.split(".").last
    settings = 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_namesObject



129
130
131
# File 'app/models/setting.rb', line 129

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