@@ -4,35 +4,48 @@ class CommunitiesController < ApplicationController
4
4
layout "site"
5
5
before_action :authorize_web
6
6
7
- before_action :set_community , :only => [ :edit , :show , :update ]
7
+ before_action :set_community , :only => [ :edit , :show , :step_up , : update]
8
8
9
9
helper_method :recent_changesets
10
10
11
11
load_and_authorize_resource :except => [ :create , :new ]
12
12
authorize_resource
13
13
14
14
def index
15
+ @critical_mass = 2
15
16
display_name = params [ :user_display_name ]
16
17
if display_name
17
18
@user = User . active . find_by ( :display_name => display_name )
18
19
if @user
19
20
@title = t ".title" , :display_name => @user . display_name
20
- @communities_organized = @user . communities_organized
21
+ @communities_leading = @user . communities_lead
21
22
else
22
23
render_unknown_user display_name
23
24
return
24
25
end
25
26
elsif current_user
26
27
@title = t ".title" , :display_name => current_user . display_name
27
- @communities_organized = current_user . communities_organized
28
+ @communities_leading = current_user . communities_lead
28
29
end
29
30
30
- @all_communities = Community . order ( :name )
31
+ # Only list out communities that have at least n members in order to mitigate spam. In order to get
32
+ # a community listed, the organizer must find n members and give them the link to the page manually.
33
+ @all_communities = Community
34
+ . joins ( :community_members )
35
+ . group ( "communities.id" )
36
+ . having ( "COUNT(communities.id) > #{ @critical_mass } " )
37
+
38
+ @my_communities = current_user ? current_user . communities : [ ]
31
39
end
32
40
33
41
# GET /communities/mycity
34
42
# GET /communities/mycity.json
35
- def show ; end
43
+ def show
44
+ # for existing or new member
45
+ @current_user_membership = CommunityMember . find_or_initialize_by (
46
+ :community => @community , :user_id => current_user &.id
47
+ )
48
+ end
36
49
37
50
def new
38
51
@title = t ".title"
@@ -43,8 +56,8 @@ def edit; end
43
56
44
57
def create
45
58
@community = Community . new ( community_params )
46
- @community . organizer = current_user
47
- if @community . save
59
+ @community . leader = current_user
60
+ if @community . save && add_first_organizer
48
61
redirect_to @community , :notice => t ( ".success" )
49
62
else
50
63
render "new"
@@ -60,8 +73,34 @@ def update
60
73
end
61
74
end
62
75
76
+ def step_up
77
+ message = nil
78
+ if @community . organizers . empty?
79
+ if @community . member? ( current_user )
80
+ message = t ".you_have_stepped_up"
81
+ add_first_organizer
82
+ else
83
+ message = t ".only_members_can_step_up"
84
+ end
85
+ else
86
+ message = t ".already_has_organizer"
87
+ end
88
+ redirect_to @community , :notice => message
89
+ end
90
+
63
91
private
64
92
93
+ def add_first_organizer
94
+ membership = CommunityMember . new (
95
+ {
96
+ :community_id => @community . id ,
97
+ :user_id => current_user . id ,
98
+ :role => CommunityMember ::Roles ::ORGANIZER
99
+ }
100
+ )
101
+ membership . save
102
+ end
103
+
65
104
def recent_changesets
66
105
bbox = @community . bbox . to_scaled
67
106
Changeset
@@ -72,6 +111,9 @@ def recent_changesets
72
111
73
112
def set_community
74
113
@community = Community . friendly . find ( params [ :id ] )
114
+ rescue ActiveRecord ::RecordNotFound
115
+ @not_found_community = params [ :id ]
116
+ render "no_such_community" , :status => :not_found
75
117
end
76
118
77
119
def community_params
0 commit comments