From 0af1678515b3a63b8f103a99d6e19430acc3fa5f Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Fri, 15 Feb 2013 19:10:18 +0100 Subject: [PATCH] limits the number of members initially shown, adds ajax reloading for more --- .../my_projects_overviews_controller.rb | 61 ++++++++++++++++--- .../my_projects_overviews/_members.html.erb | 7 +++ .../blocks/_members.rhtml | 14 +++-- app/views/my_projects_overviews/index.rhtml | 1 + .../my_projects_overviews/page_layout.rhtml | 1 + assets/javascripts/my_project_page.js | 22 +++++++ config/routes.rb | 1 + init.rb | 3 +- 8 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 app/views/my_projects_overviews/_members.html.erb create mode 100644 assets/javascripts/my_project_page.js diff --git a/app/controllers/my_projects_overviews_controller.rb b/app/controllers/my_projects_overviews_controller.rb index 71be3e4b04..11e6a62f98 100644 --- a/app/controllers/my_projects_overviews_controller.rb +++ b/app/controllers/my_projects_overviews_controller.rb @@ -142,7 +142,16 @@ class MyProjectsOverviewsController < ApplicationController render :partial => 'page_layout_attachments' end + def show_all_members + respond_to do |format| + format.js { render :partial => "members", + :locals => { :user_by_role => users_by_role(nil), + :count_users_by_role => count_users_by_role } } + end + end + helper_method :users_by_role, + :count_users_by_role, :childprojects, :recent_news, :trackers, @@ -159,13 +168,6 @@ class MyProjectsOverviewsController < ApplicationController :render_block, :object_callback - def render_block name - end - - def users_by_role - @users_by_role ||= project.users_by_role - end - def childprojects @childprojects ||= project.children.visible.all end @@ -202,6 +204,51 @@ class MyProjectsOverviewsController < ApplicationController :order => "#{IssuePriority.table_name}.position DESC, #{Issue.table_name}.updated_on DESC") end + def users_by_role(limit = 100) + + @users_by_role ||= if limit + sql_string = all_roles.map do |r| + %Q{ (Select users.*, member_roles.role_id from users + JOIN members on users.id = members.user_id + JOIN member_roles on member_roles.member_id = members.id + WHERE members.project_id = #{ project.id } AND member_roles.role_id = #{ r.id } + LIMIT #{ limit } ) } + end.join(" UNION ALL ") + + User.find_by_sql(sql_string).group_by(&:role_id).inject({}) do |hash, (role_id, users)| + hash[all_roles.detect{ |r| r.id == role_id.to_i }] = users + hash + end + else + + project.users_by_role + + end + end + + def count_users_by_role + @count_users_per_role ||= begin + sql_string = all_roles.map do |r| + %Q{ (Select COUNT(users.id) count, member_roles.role_id role_id from users + JOIN members on users.id = members.user_id + JOIN member_roles on member_roles.member_id = members.id + WHERE members.project_id = #{ project.id } AND member_roles.role_id = #{ r.id } ) } + end.join(" UNION ALL ") + + role_count = {} + + ActiveRecord::Base.connection.execute(sql_string).each do |count, role_id| + role_count[all_roles.detect{ |r| r.id == role_id.to_i }] = count.to_i if count.to_i > 0 + end + + role_count + end + end + + def all_roles + @all_roles = Role.all + end + def total_hours if User.current.allowed_to?(:view_time_entries, project) @total_hours ||= TimeEntry.visible.sum(:hours, :include => :project, :conditions => subproject_condition).to_f diff --git a/app/views/my_projects_overviews/_members.html.erb b/app/views/my_projects_overviews/_members.html.erb new file mode 100644 index 0000000000..00f184968c --- /dev/null +++ b/app/views/my_projects_overviews/_members.html.erb @@ -0,0 +1,7 @@ +<% users_by_role.keys.sort.each do |role| %> + <%=h role %>: <%= users_by_role[role].sort.collect{ |u| link_to_user u }.join("; ") %> + <% if users_by_role[role].count < count_users_by_role[role] %> + ... (<%= count_users_by_role[role] - users_by_role[role].count %> more) + <% end %> +
+<% end %> diff --git a/app/views/my_projects_overviews/blocks/_members.rhtml b/app/views/my_projects_overviews/blocks/_members.rhtml index febe3b8f6d..1ef92c7ef9 100644 --- a/app/views/my_projects_overviews/blocks/_members.rhtml +++ b/app/views/my_projects_overviews/blocks/_members.rhtml @@ -1,11 +1,17 @@

<%=l(:label_member_plural)%>

<%=l(:label_member_plural)%>

<% if users_by_role.any? %> -
+

- <% users_by_role.keys.sort.each do |role| %> - <%=h role %>: <%= users_by_role[role].sort.collect{ |u| link_to_user u }.join(", ") %>
- <% end %> + <%= render :partial => "members", + :locals => { :users_by_role => users_by_role(50), + :count_users_by_role => count_users_by_role } %> +

+

+ <%= link_to("Show all", { :controller => 'my_projects_overviews', + :action => 'show_all_members', + :id => project.id }, + :class => "all" ) %>

<% end %> diff --git a/app/views/my_projects_overviews/index.rhtml b/app/views/my_projects_overviews/index.rhtml index 43d3a1876a..541830fe2d 100644 --- a/app/views/my_projects_overviews/index.rhtml +++ b/app/views/my_projects_overviews/index.rhtml @@ -58,6 +58,7 @@ <% end %> <% content_for :header_tags do %> + <%= javascript_include_tag "my_project_page", :plugin => 'chiliproject_my_project_page' %> <%= auto_discovery_link_tag(:atom, {:controller => 'activities', :action => 'index', :id => @project, :format => 'atom', :key => User.current.rss_key}) %> <%= stylesheet_link_tag "my_projects_overview.css", :plugin => "chiliproject_my_project_page", :media => 'all' %> <% end %> diff --git a/app/views/my_projects_overviews/page_layout.rhtml b/app/views/my_projects_overviews/page_layout.rhtml index 19bbaad211..76b3554877 100644 --- a/app/views/my_projects_overviews/page_layout.rhtml +++ b/app/views/my_projects_overviews/page_layout.rhtml @@ -155,6 +155,7 @@ function addBlock() { <%= javascript_tag "updateSelect()" %> <% content_for :header_tags do %> + <%= javascript_include_tag "my_project_page", :plugin => 'chiliproject_my_project_page' %> <%= stylesheet_link_tag "my_projects_overview.css", :plugin => "chiliproject_my_project_page", :media => 'all' %> <% heads_for_wiki_formatter %> <% end %> diff --git a/assets/javascripts/my_project_page.js b/assets/javascripts/my_project_page.js new file mode 100644 index 0000000000..bbc3cf02bb --- /dev/null +++ b/assets/javascripts/my_project_page.js @@ -0,0 +1,22 @@ +/*globals jQuery, I18n*/ + +var MyProjectPage = (function ($) { + var init; + + init = function () { + $('#users_per_role .all').click(function () { + $('#users_per_role').html(''); + }); + + $.ajaxAppend({ + trigger: '.all', + indicator_class: 'ajax-indicator', + load_target: '#users_per_role', + loading_text: I18n.t("js.ajax.loading"), + loading_class: 'box loading' + }); + }; + + $('document').ready(init); + +}(jQuery)); diff --git a/config/routes.rb b/config/routes.rb index 6c559453cf..03a0f7568c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,5 +17,6 @@ ActionController::Routing::Routes.draw do |map| my.connect 'my_projects_overview/:id/page_layout/order_blocks', :action => 'order_blocks' my.connect 'my_projects_overview/:id/page_layout/update_custom_element', :action => 'update_custom_element' my.connect 'my_projects_overview/:id/page_layout/destroy_attachment', :action => 'destroy_attachment', :conditions => {:method => :post} + my.connect 'my_projects_overview/:id/page_layout/show_all_members', :action => 'show_all_members' end end diff --git a/init.rb b/init.rb index 08c2b12688..b411d1246f 100644 --- a/init.rb +++ b/init.rb @@ -7,7 +7,8 @@ Redmine::Plugin.register :chiliproject_my_project_page do description 'This plugin replaces the old overview page for projects with something similar to the "My Page"' version MyProjectPage::Version.full - Redmine::AccessControl.permission(:view_project).actions << "my_projects_overviews/index" + Redmine::AccessControl.permission(:view_project).actions << "my_projects_overviews/index" << + "my_projects_overviews/show_all_members" Redmine::AccessControl.permission(:edit_project).actions << "my_projects_overviews/page_layout" << "my_projects_overviews/add_block" << "my_projects_overviews/remove_block" <<