我正在实施一个名为Rborard的论坛系统。代码可在以下位置查看:
http://github.com/radar/rboard
有关资料如下:
class Category < ActiveRecord::Base
has_many :permissions
has_many :groups, :through => :permissions
has_many :forums
end
论坛模式
class Forum < ActiveRecord::Base
has_many :permissions
has_many :groups, :through => :permissions
belongs_to :category
end
群模型
class Group < ActiveRecord::Base
has_many :group_users
has_many :users, :through => :group_users
belongs_to :owner, :class_name => "User"
end
权限模型
class Permission < ActiveRecord::Base
belongs_to :forum
belongs_to :category
belongs_to :group
end
class User < ActiveRecord::Base
include Rboard::Permissions
has_many :group_users
# Note: I am using nested_has_many_through
has_many :groups, :through => :group_users
has_many :permissions, :through => :groups
end
权限模块
module Rboard::Permissions
THINGS = ['forum', 'category']
def self.included(klass)
klass.class_eval do
# Here we can pass an object to check if the user or any the user's groups
# has permissions on that particular option.
def overall_permissions(thing = nil)
conditions = if thing.nil?
THINGS.map do |t|
"permissions.#{t}_id " + (thing.nil? ? " IS NULL" : "= #{thing.id}") + " OR permissions.#{t}_id IS NULL"
end.join(" AND ")
else
association = thing.class.to_s.downcase
"permissions.#{association}_id = #{thing.id} OR permissions.#{association}_id IS NULL"
end
permissions.all(:conditions => conditions)
end
def can?(action, thing = nil)
permissions = overall_permissions(thing)
!!permissions.detect { |p| p.send("can_#{action}") }
end
end
end
end
希望有了它,您能够找出权限表中的字段如下
can_see_forum
forum_id
,
category_id
和
default
(默认值当前未使用)