Skip to content

Commit

Permalink
Expose an aggregate count of pending submissions. Close #129.
Browse files Browse the repository at this point in the history
Add methods to link auser to all the groups they lead and feeds they own.  Use those methods to create an instance variable with the count of pending submissions.  We'll refactor this implementation to be more effecient later.
  • Loading branch information
bamnet committed Jul 11, 2012
1 parent f5bb4ea commit 0ab32b3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
14 changes: 14 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ class ApplicationController < ActionController::Base
before_filter :set_locale
before_filter :check_for_initial_install
before_filter :set_version
before_filter :compute_pending_moderation

# Expose a instance variable counting the number of pending submissions
# a user can moderate. 0 indicates no pending submissions.
# @pending_submissions_count
def compute_pending_moderation
@pending_submissions_count = 0
if user_signed_in?
feeds = current_user.owned_feeds
feeds.each do |f|
@pending_submissions_count += f.submissions.pending.count
end
end
end

def set_version
require 'concerto/version'
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/feeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def index

def moderate
@feeds = Feed.all
auth!
@feeds.reject!{|f| not f.pending_contents.count > 0}
auth!(:action => :edit)

respond_to do |format|
format.js { }
Expand Down
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class User < ActiveRecord::Base
has_many :groups, :through => :memberships
has_many :screens, :as => :owner

has_many :leading_groups, :through => :memberships, :source => :group, :conditions => {"memberships.level" => Membership::LEVELS[:leader]}

#Validations
validates :email, :presence => true, :uniqueness => true
validates :first_name, :presence => true
Expand All @@ -29,5 +31,10 @@ def name
def in_group?(group)
groups.include?(group)
end

# Return an array of all the feeds a user owns.
def owned_feeds
leading_groups.collect{|g| g.feeds}.flatten
end

end
6 changes: 4 additions & 2 deletions app/views/elements/_topmenu_contents.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
<div class="top-menu-sectop">
<%= link_to t(:browse), feeds_path, :class => "btn #{ current_page?(feeds_path) || (controller.controller_name == 'submissions' && ['index', 'show'].include?(controller.action_name)) ? 'selected' : '' }" %>
<%= link_to t(:add), new_content_path, :class => "btn #{ current_page?(new_content_path) ? 'selected' : '' }" if can? :create, Content %>
<%= link_to moderate_path, :class => "btn" do %>
<%= t(:moderate) %>&nbsp;<span class="badge alert">##</span>
<% if @pending_submissions_count %>
<%= link_to moderate_path, :class => "btn" do %>
<%= t(:moderate) %>&nbsp;<span class="badge alert"><%= @pending_submissions_count %></span>
<% end %>
<% end %>
</div>
<div class="top-menu-secbot">
Expand Down
2 changes: 1 addition & 1 deletion app/views/feeds/moderate.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="viewblock-header_right">
<div class="status-block">
<label>PENDING</label>
<span>##</span>
<span><%= @pending_submissions_count %></span>
</div>
</div>
<div class="default-padding">
Expand Down
28 changes: 28 additions & 0 deletions test/functional/feeds_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'test_helper'

class FeedsControllerTest < ActionController::TestCase
include Devise::TestHelpers

def setup
request.env["devise.mapping"] = Devise.mappings[:user]
end

test "must sign in before new" do
get :new
assert_login_failure
end

test "not signed in user has nothing to moderate" do
get :index
assert assigns(:pending_submissions_count)
assert_equal assigns(:pending_submissions_count), 0
end

test "moderator has pending submissions" do
sign_in users(:katie)
get :index
assert assigns(:pending_submissions_count)
assert_equal assigns(:pending_submissions_count), 1
end

end

0 comments on commit 0ab32b3

Please sign in to comment.