Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.76 KB | None | 0 0
  1. class Site < ActiveRecord::Base
  2.   validates_presence_of :name, :user
  3.  
  4.   belongs_to :user
  5.  
  6.   has_many :site_aliases, :class_name => "SiteAlias"
  7.  
  8.   accepts_nested_attributes_for :site_aliases, :reject_if => proc { |attributes| attributes['name'].blank? }, :allow_destroy => true
  9.  
  10.   before_save :create_database_name
  11.   before_save :create_database_username
  12.   before_save :create_database_password
  13.  
  14.   after_save :queue_configuration_update
  15.   after_destroy :queue_configuration_destroy
  16.  
  17.   # Get a proxy object for apache operations
  18.   # related to current site
  19.   #
  20.   def proxy_for_apache
  21.     return ApacheProxy.new(self)
  22.   end
  23.  
  24.   # Get a proxy object for MySQL database operations
  25.   # related to current site
  26.   #
  27.   def proxy_for_database
  28.     return DatabaseProxy.new(self)
  29.   end
  30.  
  31.   # Get a proxy object for awstats operations
  32.   # related to current site
  33.   #
  34.   def proxy_for_awstats
  35.     return AwstatsProxy.new(self)
  36.   end
  37.  
  38.   # Generate database name from site name
  39.   #
  40.   def create_database_name
  41.     self.database = "#{name.parameterize.underscore}" unless database.present?
  42.   end
  43.  
  44.   # Generate random database password for site
  45.   #
  46.   def create_database_password
  47.     self.database_password = ActiveSupport::SecureRandom.base64(6) unless database_password.present?
  48.   end
  49.  
  50.   # Generate database name
  51.   #
  52.   def create_database_username
  53.     self.database_username = name.parameterize.underscore unless database_username.present?
  54.   end
  55.  
  56.   # Make sure to write a new apache configuration
  57.   #
  58.   def queue_configuration_update
  59.     ApacheJob.new.apply_configuration self
  60.     DatabaseJob.new.ensure_user self
  61.   end
  62.  
  63.   # Make sure to delete configuration files
  64.   #
  65.   def queue_configuration_destroy
  66.     ApacheJob.new.delete_configuration self
  67.   end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement