technoweenie / altered_beast forked from courtenay/altered_beast

Ground-up rewrite of Beast, a Ruby on Rails forum.

This URL has Read+Write access

altered_beast / app / models / site.rb
100644 41 lines (32 sloc) 0.941 kb
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
class Site < ActiveRecord::Base
  class UndefinedError < StandardError; end
 
  has_many :users, :conditions => {:state => 'active'}
  has_many :all_users, :class_name => 'User'
  
  has_many :forums
  has_many :topics, :through => :forums
  has_many :posts, :through => :forums
  
  validates_presence_of :name
  validates_uniqueness_of :host
  
  attr_readonly :posts_count, :users_count, :topics_count
  
  def host=(value)
    write_attribute :host, value.to_s.downcase
  end
  
  def self.main
    @main ||= find :first, :conditions => {:host => ''}
  end
  
  def self.find_by_host(name)
    return nil if name.nil?
    name.downcase!
    name.strip!
    name.sub! /^www\./, ''
    sites = find :all, :conditions => ['host = ? or host = ?', name, '']
    sites.reject { |s| s.default? }.first || sites.first
  end
  
  # <3 rspec
  def ordered_forums(*args)
    forums.ordered(*args)
  end
  
  def default?
    host.blank?
  end
end