Skip to content

Commit

Permalink
default_scope apparently doesn't like conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Jun 27, 2009
1 parent a6ee64c commit 773439d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 36 deletions.
4 changes: 2 additions & 2 deletions TODO.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
<p class="p7"><span class="s1"><b>FORUM PERMISSIONS</b></span></p>
<p class="p12"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s1">Limit forum access (control over who can post, reply, and view forums)</span></p>
<p class="p10"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s1">Forum access is controlled on a user group basis and</span><span class="s5"> </span><span class="s1">also on a forum basis</span></p>
<p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Moderator levels (control which moderation tasks moderators have access to)</span></p>
<p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>User groups (used for moderation levels, and also forum access—user groups can be based on post count, length of membership, automatic (i.e., all new members are added), or custom (i.e., mods/admins must place users in groups manually)</span></p>
<p class="p13"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s1">Moderator levels (control which moderation tasks moderators have access to)</span></p>
<p class="p13"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s1">User groups (used for moderation levels, and also forum access—user groups can be based on post count, length of membership, automatic (i.e., all new members are added), or custom (i.e., mods/admins must place users in groups manually)</span></p>
<p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2">Forum function permissions</span></p>
<p class="p9"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2">Signatures (control over number of lines/characters, what bbcode is allowed (i.e., to disallow image code))</span><span class="s1"> (Though it would be VERY nice to forbit IMG tags in sigs somehow—image sigatures really make a forum hard to read)</span></p>
<p class="p9"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2">Avatars (size, dimensions, filetype)</span><span class="s1"> (Only necessary if we have avatars!)</span></p>
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/forums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ForumsController < ApplicationController
def index
if @category
@forums = @category.forums.without_parent
@forums = @forums.regardless_of_active if current_user.can?(:see_inactive_forums)
@forums = @forums.active if !current_user.can?(:see_inactive_forums)

else
# TODO: I encourage allcomers to find a better way.
Expand Down Expand Up @@ -40,14 +40,15 @@ def show

private
def find_forum
@forums = current_user.can?(:see_inactive_forums) ? Forum.regardless_of_active : Forum
@forums = current_user.can?(:see_inactive_forums) ? Forum : Forum.active
@forum = @forums.find(params[:id], :include => [{ :topics => :posts }, :moderations, :permissions])
if !current_user.can?(:see_forum, @forum)
flash[:notice] = t(:forum_permission_denied)
redirect_to forums_path
end
rescue ActiveRecord::RecordNotFound
flash[:notice] = t(:forum_not_found_or_inactive)
redirect_to forums_path
end

def find_category
Expand Down
7 changes: 4 additions & 3 deletions app/models/forum.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
class Forum < ActiveRecord::Base
default_scope :order => "title asc", :conditions => { :active => true }
default_scope :order => "title asc"

acts_as_list :scope => :parent_id
acts_as_tree :order => :position

named_scope :without_category, :conditions => { :category_id => nil }, :include => :permissions, :order => "position"
named_scope :without_parent, :conditions => { :parent_id => nil }, :include => :permissions, :order => "position"
named_scope :regardless_of_active, :conditions => ["active = ? OR active = ?", true, false]
named_scope :active, :conditions => { :active => true }


has_many :moderations
has_many :posts, :through => :topics, :source => :posts, :order => "posts.created_at desc"
has_many :topics, :order => "topics.created_at desc", :dependent => :destroy
has_many :permissions
has_many :groups, :through => :permissions


belongs_to :category
belongs_to :last_post, :class_name => "Post"
Expand Down
53 changes: 29 additions & 24 deletions app/views/admin/forums/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
<tbody>
<tr>
<td><%= f.label :title, t(:Title) %></td>
<td><%= f.text_field "title" %></td>
</tr>

<tr>
<td valign='top'><%= f.label :description, t(:Description) %></td>
<td><%= f.text_area "description", :cols => 40, :rows => 5 %></td>
</tr>

<% unless @forums.empty? %>
<tr>
<td><%= f.label :parent_id, t(:Child_of) %></td>
<td><%= f.select "parent_id", @forums.map { |forum| [select_display(forum),forum.id] }, :include_blank => true %></td>
</tr>
<% end %>

<% unless @category %>
<tr>
<td><%= f.label :category_id, t(:Category) %></td>
<td><%= f.select "category_id", @categories.map { |category| [select_display(category, 'name'), category.id] }, :include_blank => true %></td>
</tr>
<% end %>
<tbody>
<tr>
<td><%= f.label :title, t(:Title) %></td>
<td><%= f.text_field "title" %></td>
</tr>

<tr>
<td valign='top'><%= f.label :description, t(:Description) %></td>
<td><%= f.text_area "description", :cols => 40, :rows => 5 %></td>
</tr>

<% unless @forums.empty? %>
<tr>
<td><%= f.label :parent_id, t(:Child_of) %></td>
<td><%= f.select "parent_id", @forums.map { |forum| [select_display(forum),forum.id] }, :include_blank => true %></td>
</tr>
<% end %>

<% unless @category %>
<tr>
<td><%= f.label :category_id, t(:Category) %></td>
<td><%= f.select "category_id", @categories.map { |category| [select_display(category, 'name'), category.id] }, :include_blank => true %></td>
</tr>
<% end %>

<tr>
<td><%= f.label :active, t(:Active) %></td>
<td><%= f.check_box :active %></td>
</tr>
</tbody>
10 changes: 8 additions & 2 deletions features/forums.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Feature: Manage forums
When I follow "Public Forum"
Then I should see "Viewing forum: Public Forum"


Scenario: Registered users should be able to post new topics
Given I am logged in as "registered_user"
And I am on the forums page
Expand All @@ -25,11 +24,18 @@ Feature: Manage forums
When I fill in "Text" with "This is just a tribute"
When I press "Create"
Then I should see "rBoard -> Public Forum -> Tribute"

Scenario: Registered users should not be able to see hidden forums
Given I am logged in as "registered_user"
And there is an inactive forum
Then I should not see "Hidden Forum"
And I am on the forum page for "Hidden Forum"
Then I should see "The forum you were looking for could not be found, or is inactive."

Scenario: Logged in as a user who can see inactive forums
Given I am logged in as "registered_user"
And there is an inactive forum
And I can see inactive forums
And I am on the forums page
Then I should see "Inactive Forum"
Then I should see "Hidden Forum"

4 changes: 2 additions & 2 deletions features/step_definitions/app_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
When "I fill in \"password\" with \"password\""
When "I press \"Login\""
Then "I should see \"Logged in successfully.\""
@user = assigns[:user]
puts @user.inspect
# To get the latest user
@user = User.first(:order => "updated_at DESC")
end

Given /^there is the usual setup$/ do
Expand Down
3 changes: 3 additions & 0 deletions features/step_definitions/forums_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Given /^there is an inactive forum$/ do
Forum.create(:title => "Hidden Forum", :description => "This is a hidden forum", :active => false)
end
4 changes: 4 additions & 0 deletions features/step_definitions/users_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Given /^I can see inactive forums$/ do
@permission = @user.permissions.global
@permission.update_attribute(:can_see_inactive_forums, true)
end
2 changes: 2 additions & 0 deletions features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def path_to(page_name)
'/'
when /the forums page/
forums_path
when /the forum page for "(.*?)"/
forum_path(Forum.find_by_title($1))
when /login page/
login_path
when /administrator's group page/
Expand Down
2 changes: 1 addition & 1 deletion lib/rboard/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def login
redirect_back_or_default(forums_path) and return false
end
return unless request.post?
self.current_user = User.authenticate(params[:login], params[:password])
self.current_user = @user = User.authenticate(params[:login], params[:password])
if logged_in?
# #remember_me calls save internally, so don't bother saving it twice
if params[:remember_me] == "1"
Expand Down

0 comments on commit 773439d

Please sign in to comment.