This repository has been archived by the owner on Sep 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
preference.rb
70 lines (61 loc) · 2.06 KB
/
preference.rb
1
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'validate'
# Handles storing of preferences for the application.
#
# This is an internal structure mostly, which is useful to access / save
# things from the GUI.
#
# Prefs are used all over to handle decisions that we'd rather
# not use config files for.
#
class Preference
include DataMapper::Resource
include DataMapper::Validate
# Types can hold strings, booleans, or pointers to
# other records (like country)
CC_PROCESSORS = ['Authorize.net', 'PayPal IPN']
MAIL_AUTH = ['none', 'plain', 'login', 'cram_md5']
property :id, Fixnum, :serial => true
property :name, String, :default => "", :nullable => false
property :value, String, :default => ""
validates_presence_of :name, :type
# FIXME: fix validates_uniqueness_of
#validates_uniqueness_of :name
def self.find_by_name(name)
first(:name => name)
end
# Can throw an error if these items aren't set.
# Make sure to wrap any block that calls this
def self.init_mail_settings
# SET MAIL SERVER SETTINGS FROM PREFERENCES
mail_host = find_by_name('mail_host').value
mail_server_settings = {
:address => mail_host,
:domain => mail_host,
:port => find_by_name('mail_port').value,
}
mail_auth_type = find_by_name('mail_auth_type').value
if !mail_auth_type != 'none'
mail_server_settings[:authentication] = mail_auth_type.to_sym
mail_server_settings[:user_name] = find_by_name('mail_username').value
mail_server_settings[:password] = find_by_name('mail_password').value
end
ActionMailer::Base.smtp_settings = mail_server_settings
end
# Saves preferences passed in from our form.
#
def self.save_settings(settings)
logger.info "SERVER SETTINGS..."
logger.info settings.inspect
settings.each do |name, value|
update_all("value = '#{value}'", "name = '#{name}'")
end
end
# Determines if a preference is "true" or not.
# This is the ghetto, bootleg way to determine booleans.
def is_true?
if self.value == '1' || self.value == 'true'
return true
end
return false
end
end