Updated Rake tasks and test, spec to reflect change in how Merb environments
[merb_mart.git] / app / models / preference.rb
blob72f911f7525c1474006c65bffb6ce344b8b87cbc
1 # Handles storing of preferences for the application.
3 # This is an internal structure mostly, which is useful to access / save
4 # things from the GUI.
6 # Prefs are used all over to handle decisions that we'd rather
7 # not use config files for.
9 class Preference
10   
11   include DataMapper::Persistable
12   
13   # Types can hold strings, booleans, or pointers to
14   # other records (like country)
15   CC_PROCESSORS = ['Authorize.net', 'PayPal IPN']
16   MAIL_AUTH = ['none', 'plain', 'login', 'cram_md5']
17   
18   property :name,  :string, :default => "", :nullable => false
19   property :value, :string, :default => ""
20   
21   #validates_presence_of :name, :type
22   #validates_uniqueness_of :name
23   
24   
25   def self.find_by_name(name)
26     first(:name => name)
27   end
28   
29   # Can throw an error if these items aren't set.
30   # Make sure to wrap any block that calls this
31   def self.init_mail_settings
32     # SET MAIL SERVER SETTINGS FROM PREFERENCES
33     mail_host = find_by_name('mail_host').value
34     mail_server_settings = {
35       :address => mail_host,
36       :domain => mail_host,
37       :port => find_by_name('mail_port').value,
38     }
39     mail_auth_type = find_by_name('mail_auth_type').value
40     if !mail_auth_type != 'none'
41       mail_server_settings[:authentication] = mail_auth_type.to_sym
42       mail_server_settings[:user_name] = find_by_name('mail_username').value
43       mail_server_settings[:password] = find_by_name('mail_password').value
44     end
45     ActionMailer::Base.smtp_settings = mail_server_settings
46   end
47   
48   # Saves preferences passed in from our form.
49   #
50   def self.save_settings(settings)
51     logger.info "SERVER SETTINGS..."
52     logger.info settings.inspect
53     settings.each do |name, value|
54       update_all("value = '#{value}'", "name = '#{name}'")
55     end
56   end
57   
58   # Determines if a preference is "true" or not.
59   # This is the ghetto, bootleg way to determine booleans.
60   def is_true?
61     if self.value == '1' || self.value == 'true'
62       return true
63     end
64     return false
65   end
66 end