public
Fork of courtenay/altered_beast
Description: Ground-up rewrite of Beast, a Ruby on Rails forum.
Homepage: http://activereload.lighthouseapp.com/projects/7537-altered-beast/
Clone URL: git://github.com/technoweenie/altered_beast.git
add User#revise for updating topics
technoweenie (author)
Tue Jan 29 15:08:55 -0800 2008
commit  f56a5d57f41fa80158ffe091152b6cef5f1f439a
tree    0459763a7f6e485b50813cf73aaf24670c1cb974
parent  4af06296b3b435e6ced2f8479464013de26197c7
...
56
57
58
 
59
60
 
61
62
63
...
56
57
58
59
60
 
61
62
63
64
0
@@ -56,8 +56,9 @@ class TopicsController < ApplicationController
0
   end
0
 
0
   def update
0
+ current_user.revise @topic, params[:topic]
0
     respond_to do |format|
0
- if @topic.update_attributes(params[:topic])
0
+ if @topic.errors.empty?
0
         flash[:notice] = 'Topic was successfully updated.'
0
         format.html { redirect_to(forum_topic_path(@forum, @topic)) }
0
         format.xml { head :ok }
...
65
66
67
 
 
 
 
68
...
65
66
67
68
69
70
71
72
0
@@ -65,4 +65,8 @@ module ApplicationHelper
0
   def jstime(time, format = nil)
0
     content_tag 'span', time.strftime(format || @@default_jstime_format), :class => 'time'
0
   end
0
+
0
+ def for_moderators_of(record, &block)
0
+ moderator_of?(record) && concat(capture(&block), block.binding)
0
+ end
0
 end
...
31
32
33
34
 
35
36
37
...
31
32
33
 
34
35
36
37
0
@@ -31,7 +31,7 @@ class User < ActiveRecord::Base
0
   end
0
 
0
   def moderator_of?(forum)
0
- Moderatorship.exists?(:user_id => id, :forum_id => forum.id)
0
+ admin? || Moderatorship.exists?(:user_id => id, :forum_id => forum.id)
0
   end
0
 
0
   def display_name
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,5 +1,5 @@
0
 module User::Editable
0
   def editable_by?(user)
0
- user && (user.id == user_id || user.admin? || user.moderator_of?(forum_id))
0
+ user && (user.id == user_id || user.moderator_of?(forum_id))
0
   end
0
 end
0
\ No newline at end of file
...
6
7
8
9
10
11
12
 
13
14
15
 
16
17
18
...
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
28
...
6
7
8
 
 
 
 
9
10
11
 
12
13
14
15
...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -6,13 +6,10 @@ class User
0
   #
0
   def post(forum, attributes)
0
     attributes.symbolize_keys!
0
- returning Topic.new(attributes) do |topic|
0
- if admin? || moderator_of?(forum)
0
- topic.sticky, topic.locked = attributes[:sticky], attributes[:locked]
0
- end
0
+ Topic.new(attributes) do |topic|
0
       topic.forum = forum
0
       topic.user = self
0
- topic.save
0
+ revise_topic topic, attributes
0
     end
0
   end
0
 
0
@@ -24,4 +21,19 @@ class User
0
       post.save
0
     end
0
   end
0
+
0
+ def revise(record, attributes)
0
+ case record
0
+ when Topic then revise_topic(record, attributes)
0
+ when Post then post.save
0
+ else raise "Invalid record to revise: #{record.class.name.inspect}"
0
+ end
0
+ record
0
+ end
0
+
0
+protected
0
+ def revise_topic(topic, attributes)
0
+ topic.sticky, topic.locked = attributes[:sticky], attributes[:locked] if moderator_of?(topic.forum)
0
+ topic.save
0
+ end
0
 end
0
\ No newline at end of file
...
2
3
4
5
 
6
7
8
...
10
11
12
13
14
15
16
...
2
3
4
 
5
6
7
8
...
10
11
12
 
13
14
15
0
@@ -2,7 +2,7 @@
0
 <label for="topic_title"><%= 'Title'[:title_title] %></label><br />
0
 <%= form.text_field :title, :onchange => "/*TopicForm.editNewTitle(this);*/", :class => "primary", :tabindex => 10 %>
0
 
0
-<% if admin? || current_user.moderator_of?(@topic.forum) -%>
0
+<% for_moderators_of @topic do -%>
0
 <label style="margin-left:1em;">
0
 <%= form.check_box :sticky %> <%= 'Sticky'[:sticky_title] %>
0
 </label>
0
@@ -10,7 +10,6 @@
0
 <label style="margin-left:1em;">
0
 <%= form.check_box :locked %> <%= 'Locked'[:locked_title] %>
0
 </label>
0
-
0
 <% end -%>
0
 
0
 </p>
...
26
27
28
 
 
 
 
 
 
 
29
30
31
...
104
105
106
107
 
108
109
110
...
26
27
28
29
30
31
32
33
34
35
36
37
38
...
111
112
113
 
114
115
116
117
0
@@ -26,6 +26,13 @@ module AuthenticatedSystem
0
       logged_in? && current_user.admin?
0
     end
0
     
0
+ def moderator_of?(record)
0
+ return true if admin?
0
+ return false unless logged_in?
0
+ forum = record.respond_to?(:forum) ? record.forum : record
0
+ current_user.moderator_of? forum
0
+ end
0
+
0
     # Check if the user is authorized
0
     #
0
     # Override this method in your controllers if you want to restrict access
0
@@ -104,7 +111,7 @@ module AuthenticatedSystem
0
     # Inclusion hook to make #current_user and #logged_in?
0
     # available as ActionView helper methods.
0
     def self.included(base)
0
- base.send :helper_method, :current_user, :logged_in?, :current_site, :admin?
0
+ base.send :helper_method, :current_user, :logged_in?, :current_site, :admin?, :moderator_of?
0
     end
0
 
0
     # Called from #current_user. First attempt to login by the user id stored in the session.
...
204
205
206
 
207
208
209
210
211
212
 
213
214
215
...
217
218
219
220
 
221
222
223
...
229
230
231
232
 
233
234
235
...
241
242
243
244
 
245
246
247
...
253
254
255
256
 
257
258
259
...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
...
219
220
221
 
222
223
224
225
...
231
232
233
 
234
235
236
237
...
243
244
245
 
246
247
248
249
...
255
256
257
 
258
259
260
261
0
@@ -204,12 +204,14 @@ end
0
 
0
 describe TopicsController, "PUT #update" do
0
   before do
0
+ login_as :default
0
     @forum = forums(:default)
0
     Forum.stub!(:find_by_permalink).with('1').and_return(@forum)
0
     @attributes = {}
0
     @topic = topics(:default)
0
     @forum.stub!(:topics).and_return([])
0
     @forum.topics.stub!(:find_by_permalink).with('1').and_return(@topic)
0
+ @user.stub!(:revise).with(@topic, @attributes)
0
   end
0
   
0
   describe TopicsController, "(successful save)" do
0
@@ -217,7 +219,7 @@ describe TopicsController, "PUT #update" do
0
     act! { put :update, :forum_id => 1, :id => 1, :topic => @attributes }
0
 
0
     before do
0
- @topic.stub!(:save).and_return(true)
0
+ @topic.stub!(:errors).and_return([])
0
     end
0
     
0
     it_assigns :topic, :flash => { :notice => :not_nil }
0
@@ -229,7 +231,7 @@ describe TopicsController, "PUT #update" do
0
     act! { put :update, :forum_id => 1, :id => 1, :topic => @attributes }
0
 
0
     before do
0
- @topic.stub!(:save).and_return(false)
0
+ @topic.stub!(:errors).and_return([1])
0
     end
0
     
0
     it_assigns :topic
0
@@ -241,7 +243,7 @@ describe TopicsController, "PUT #update" do
0
     act! { put :update, :forum_id => 1, :id => 1, :topic => @attributes, :format => 'xml' }
0
 
0
     before do
0
- @topic.stub!(:save).and_return(true)
0
+ @topic.stub!(:errors).and_return([])
0
     end
0
     
0
     it_assigns :topic
0
@@ -253,7 +255,7 @@ describe TopicsController, "PUT #update" do
0
     act! { put :update, :forum_id => 1, :id => 1, :topic => @attributes, :format => 'xml' }
0
 
0
     before do
0
- @topic.stub!(:save).and_return(false)
0
+ @topic.stub!(:errors).and_return([@topic])
0
     end
0
     
0
     it_assigns :topic
...
79
80
81
82
83
84
85
...
90
91
92
93
 
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
79
80
81
 
82
83
84
...
89
90
91
 
92
93
94
95
96
 
97
98
99
100
101
102
 
103
104
105
0
@@ -79,7 +79,6 @@ describe Post, "#editable_by?" do
0
   end
0
 
0
   it "restricts user for other post" do
0
- @user.should_receive(:admin?).and_return(false)
0
     @user.should_receive(:moderator_of?).and_return(false)
0
     @post.should_not be_editable_by(@user)
0
   end
0
@@ -90,19 +89,17 @@ describe Post, "#editable_by?" do
0
   end
0
   
0
   it "allows admin" do
0
- @user.should_receive(:admin?).and_return(true)
0
+ @user.should_receive(:moderator_of?).and_return(true)
0
     @post.should be_editable_by(@user)
0
   end
0
   
0
   it "restricts moderator for other forum" do
0
- @user.should_receive(:admin?).and_return(false)
0
     @user.should_receive(:moderator_of?).with(1).and_return(false)
0
     @post.forum_id = 1
0
     @post.should_not be_editable_by(@user)
0
   end
0
   
0
   it "allows moderator" do
0
- @user.should_receive(:admin?).and_return(false)
0
     @user.should_receive(:moderator_of?).with(2).and_return(true)
0
     @post.forum_id = 2
0
     @post.should be_editable_by(@user)
...
156
157
158
159
160
161
162
...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
...
156
157
158
 
159
160
161
...
165
166
167
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
170
171
0
@@ -156,7 +156,6 @@ describe Topic, "#editable_by?" do
0
   end
0
 
0
   it "restricts user for other topic" do
0
- @user.should_receive(:admin?).and_return(false)
0
     @user.should_receive(:moderator_of?).and_return(false)
0
     @topic.should_not be_editable_by(@user)
0
   end
0
@@ -166,20 +165,7 @@ describe Topic, "#editable_by?" do
0
     @topic.should be_editable_by(@user)
0
   end
0
   
0
- it "allows admin" do
0
- @user.should_receive(:admin?).and_return(true)
0
- @topic.should be_editable_by(@user)
0
- end
0
-
0
- it "restricts moderator for other forum" do
0
- @user.should_receive(:admin?).and_return(false)
0
- @user.should_receive(:moderator_of?).with(1).and_return(false)
0
- @topic.forum_id = 1
0
- @topic.should_not be_editable_by(@user)
0
- end
0
-
0
   it "allows moderator" do
0
- @user.should_receive(:admin?).and_return(false)
0
     @user.should_receive(:moderator_of?).with(2).and_return(true)
0
     @topic.forum_id = 2
0
     @topic.should be_editable_by(@user)
...
114
115
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
118
119
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
0
@@ -114,6 +114,78 @@ describe User, "#post for admins" do
0
   end
0
 end
0
 
0
+module TopicUpdatePostHelper
0
+ def self.included(base)
0
+ base.define_models
0
+
0
+ base.before do
0
+ @user = users(:default)
0
+ @topic = topics(:default)
0
+ @attributes = {:body => 'booya'}
0
+ end
0
+ end
0
+
0
+ def revise!
0
+ @user.revise @topic, @attributes
0
+ end
0
+end
0
+
0
+describe User, "#revise(topic) for users" do
0
+ include TopicUpdatePostHelper
0
+
0
+ it "ignores sticky bit" do
0
+ @attributes[:sticky] = 1
0
+ revise!
0
+ @topic.should_not be_sticky
0
+ end
0
+
0
+ it "ignores locked bit" do
0
+ @attributes[:locked] = true
0
+ revise!
0
+ @topic.should_not be_locked
0
+ end
0
+end
0
+
0
+describe User, "#revise(topic) for moderators" do
0
+ include TopicUpdatePostHelper
0
+
0
+ before do
0
+ @user.stub!(:moderator_of?).and_return(true)
0
+ end
0
+
0
+ it "sets sticky bit" do
0
+ @attributes[:sticky] = 1
0
+ revise!
0
+ @topic.should be_sticky
0
+ end
0
+
0
+ it "sets locked bit" do
0
+ @attributes[:locked] = true
0
+ revise!
0
+ @topic.should be_locked
0
+ end
0
+end
0
+
0
+describe User, "#revise(topic) for admins" do
0
+ include TopicUpdatePostHelper
0
+
0
+ before do
0
+ @user.admin = true
0
+ end
0
+
0
+ it "sets sticky bit" do
0
+ @attributes[:sticky] = 1
0
+ revise!
0
+ @topic.should be_sticky
0
+ end
0
+
0
+ it "sets locked bit" do
0
+ @attributes[:locked] = true
0
+ revise!
0
+ @topic.should be_locked
0
+ end
0
+end
0
+
0
 describe User, "#reply" do
0
   define_models
0
   

Comments

    No one has commented yet.