updated readme

This commit is contained in:
Mike Kelley
2011-08-27 01:41:48 -06:00
commit 17fee3c6d5
98 changed files with 11421 additions and 0 deletions

20
app/models/ability.rb Normal file
View File

@@ -0,0 +1,20 @@
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :manage, :all if user.admin?
can :read, Category, :state => true
can :read, Forum, :state => true, :category => { :state => true }
can :read, Topic, :forum => { :state => true, :category => { :state => true } }
can :read, Post, :topic => { :forum => { :state => true, :category => { :state => true } } }
can :update, Post, :user_id => user.id
can :destroy, [Topic,Post], :user_id => user.id
can :create, Post unless user.new_record?
can :create, Topic unless user.new_record?
end
end

14
app/models/category.rb Normal file
View File

@@ -0,0 +1,14 @@
class Category < ActiveRecord::Base
# Associations
has_many :forums, :dependent => :destroy
# Accessors
attr_accessible :title, :state, :position, :category_id
# Scopes
default_scope :order => 'position ASC'
# Validations
validates :title, :presence => true
end

19
app/models/forum.rb Normal file
View File

@@ -0,0 +1,19 @@
class Forum < ActiveRecord::Base
# Associations
has_many :topics, :dependent => :destroy
has_many :posts, :through => :topics
belongs_to :category
# Accessors
attr_accessible :title, :description, :state, :position, :category_id
# Scopes
default_scope :order => 'position ASC'
# Validations
validates :title, :presence => true
validates :description, :presence => true
validates :category_id, :presence => true
end

34
app/models/post.rb Normal file
View File

@@ -0,0 +1,34 @@
class Post < ActiveRecord::Base
# Associations
belongs_to :forum, :counter_cache => true
belongs_to :topic, :counter_cache => true, :touch => true
belongs_to :user, :class_name => "User", :counter_cache => true
# Accessors
attr_accessible :body
# Validations
validates :body, :presence => true
validates :user, :presence => true
# Default Scope
default_scope :order => 'created_at ASC'
# Scope to display only the last n posts. Used for "Recent Posts" display
scope :recent, lambda {
|c| reorder('created_at desc').limit(c)
}
# Callbacks
before_save :topic_locked?
# Methods
private
def topic_locked?
if topic.locked?
errors.add(:base, "That topic is locked")
false
end
end
end

37
app/models/topic.rb Normal file
View File

@@ -0,0 +1,37 @@
class Topic < ActiveRecord::Base
# Associations
has_many :posts, :dependent => :destroy
belongs_to :forum, :counter_cache => true
belongs_to :user, :class_name => "User", :counter_cache => true
# Accessors
attr_accessor :body
attr_accessible :title, :body, :sticky, :locked
# Validations
validates :title, :presence => true
validates :body, :presence => true, :on => :create
validates :posts, :presence => true, :allow_nil => false, :on => :update
validates :user, :presence => true
# Scopes
default_scope :order => 'sticky DESC, updated_at DESC'
# Callbacks
after_create :create_initial_post
# Methods
def hit!
self.class.increment_counter :hits, id
end
private
def create_initial_post
returning self.posts.build(:body => self.body) do |post|
post.forum = self.forum
post.user = self.user
post.save
end
end
end

21
app/models/user.rb Normal file
View File

@@ -0,0 +1,21 @@
class User < ActiveRecord::Base
include Gravtastic
gravtastic :size => 165, :filetype => :png, :rating => 'R'
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :username, :presence => true, :uniqueness => true
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
has_many :topics, :dependent => :destroy
has_many :posts, :dependent => :destroy
def admin?
true if self.username == 'codezombie'
end
end