forked from IsaacLewis/eol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
community.rb
96 lines (79 loc) · 3.46 KB
/
community.rb
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Community < ActiveRecord::Base
include EOL::Feedable
has_one :collection #TODO - didn't work? , :as => :focus
alias :focus :collection
has_many :collections # A bit confusing, this, but the singular is aliased.
has_many :members
has_many :roles
has_many :collection_items, :as => :object
after_create :attatch_focus
# These are for will_paginate:
cattr_reader :per_page
@@per_page = 30
validates_presence_of :name, :message => I18n.t(:cannot_be_empty)
validates_length_of :name, :maximum => 127, :message => I18n.t(:must_be_less_than_128_characters_long)
validates_uniqueness_of :name, :message => I18n.t(:has_already_been_taken)
def self.special
special = cached_find(:name, $SPECIAL_COMMUNITY_NAME)
raise "Special Community is missing. Perhaps you forgot to load it?" if special.nil? # For tests.
special
end
def self.create_special
special = nil
begin
special = Community.special
rescue
special = Community.create(:name => $SPECIAL_COMMUNITY_NAME,
:description => 'This is a special community for the curtors and admins of EOL.',
:show_special_privileges => 1)
special.add_default_roles
end
special_roles = {$ADMIN_ROLE_NAME => 20, $CURATOR_ROLE_NAME => 10, $ASSOCIATE_ROLE_NAME => 1}
special_roles.keys.each do |key|
role = Role.find(:first, :conditions => ['title = ? and community_id = ?', key, special.id])
role ||= Role.create(:community_id => special.id, :title => key)
# TODO - we really should change Language#english to Language#default and have it set in a config file.
# TODO - this is stupid, but migrations break because Language (the model) doesn't point to the right DB when
# this runs, so it is undefined after recreating the DB (in which case we hardly need it anyway):
begin
role.privileges = Privilege.find(:all, :conditions => ["level <= ? and special = ?", special_roles[key], true])
rescue ActiveRecord::StatementInvalid => e
# Do nothing; this should only happen when developers recreate the databases and thus it hardly matters now.
end
end
end
# TODO - test
# Adds the default roles, auto-joins the user to the community, and makes that person the owner.
def initialize_as_created_by(user)
new_roles = Role.add_defaults_to_community(self)
user.join_community(self).add_role(new_roles.first)
end
def special?
show_special_privileges > 0
end
def add_default_roles
Role.add_defaults_to_community(self)
end
# Returns the new member.
def add_member(user)
member = Member.create!(:user_id => user.id, :community_id => id)
members << member
feed.post(I18n.t("user_joined_community_note", :username => user.username), :feed_item_type_id => FeedItemType.content_update.id, :user_id => user.id)
member
end
def remove_member(user)
member = Member.find_by_user_id_and_community_id(user.id, id)
raise I18n.t(:could_not_find_user) unless member
member.destroy
feed.post(I18n.t("user_left_community_note", :username => user.username), :feed_item_type_id => FeedItemType.content_update.id, :user_id => user.id)
self.reload
end
def has_member?(user)
members.map {|m| m.user_id}.include?(user.id)
end
private
def attatch_focus
# TODO - i18n
Collection.create(:name => "#{self.name}'s Focus", :special_collection_id => SpecialCollection.focus.id, :community_id => self.id)
end
end