From 20a249b33d0cea8584d86643ec4991ae89fc8a97 Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Sun, 9 Sep 2018 11:14:48 +0000 Subject: [PATCH 5/7] load changesets and time entries tabs async --- app/controllers/issues_controller.rb | 29 +++++++++++++++++++++------- app/helpers/issues_helper.rb | 5 +++-- app/views/common/_tabs.html.erb | 4 ++-- app/views/issues/_changesets.html.erb | 20 ------------------- app/views/issues/tabs/_changesets.html.erb | 2 +- app/views/issues/tabs/_time_entries.html.erb | 2 +- config/routes.rb | 1 + lib/redmine.rb | 2 +- public/javascripts/application.js | 18 +++++++++++++++++ test/functional/issues_controller_test.rb | 15 +++++++++++--- test/integration/routing/issues_test.rb | 2 ++ 11 files changed, 63 insertions(+), 37 deletions(-) delete mode 100644 app/views/issues/_changesets.html.erb mode change 100644 => 100755 config/routes.rb mode change 100644 => 100755 lib/redmine.rb mode change 100644 => 100755 test/integration/routing/issues_test.rb diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 6d02dbd..74e95c1 100755 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -18,7 +18,7 @@ class IssuesController < ApplicationController default_search_scope :issues - before_action :find_issue, :only => [:show, :edit, :update] + before_action :find_issue, :only => [:show, :edit, :update, :issue_tab] before_action :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] before_action :authorize, :except => [:index, :new, :create] before_action :find_optional_project, :only => [:index, :new, :create] @@ -84,13 +84,10 @@ class IssuesController < ApplicationController def show @journals = @issue.visible_journals_with_index - @changesets = @issue.changesets.visible.preload(:repository, :user).to_a + @has_changesets = @issue.changesets.visible.preload(:repository, :user).exists? @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } - if User.current.wants_comments_in_reverse_order? - @journals.reverse! - @changesets.reverse! - end + @journals.reverse! if User.current.wants_comments_in_reverse_order? if User.current.allowed_to?(:view_time_entries, @project) Issue.load_visible_spent_hours([@issue]) @@ -107,7 +104,10 @@ class IssuesController < ApplicationController retrieve_previous_and_next_issue_ids render :template => 'issues/show' } - format.api + format.api { + @changesets = @issue.changesets.visible.preload(:repository, :user).to_a + @changesets.reverse! if User.current.wants_comments_in_reverse_order? + } format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } format.pdf { send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf" @@ -192,6 +192,21 @@ class IssuesController < ApplicationController end end + def issue_tab + return render_error :status => 422 unless request.xhr? + tab = params[:name] + + case tab + when 'time_entries' + @time_entries = @issue.time_entries.visible.preload(:activity, :user).to_a + render :partial => 'issues/tabs/time_entries', :locals => {:time_entries => @time_entries} + when 'changesets' + @changesets = @issue.changesets.visible.preload(:repository, :user).to_a + changesets.reverse! if User.current.wants_comments_in_reverse_order? + render :partial => 'issues/tabs/changesets', :locals => {:changesets => @changesets} + end + end + # Bulk edit/copy a set of issues def bulk_edit @issues.sort! diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 2e59f65..613be04 100755 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -554,8 +554,9 @@ module IssuesHelper tabs << {:name => 'notes', :label => :label_issue_history_notes, :onclick => 'showIssueHistory("notes", this.href)'} if journals_with_notes.any? tabs << {:name => 'properties', :label => :label_issue_history_properties, :onclick => 'showIssueHistory("properties", this.href)'} if journals_without_notes.any? end - tabs << {:name => 'time_entries', :label => :label_time_entry_plural, :partial => 'issues/tabs/time_entries', :locals => {:time_entries => @time_entries}} if User.current.allowed_to?(:view_time_entries, @project) && @issue.spent_hours > 0 - tabs << {:name => 'changesets', :label => :label_associated_revisions, :partial => 'issues/tabs/changesets', :locals => {:changesets => @changesets}} if @changesets.present? + tabs << {:name => 'time_entries', :label => :label_time_entry_plural, :remote => true, :onclick => "getRemoteTab('time_entries', '#{tab_issue_path(@issue, :name => 'time_entries')}', '#{issue_path(@issue, :tab => 'time_entries')}')"} if User.current.allowed_to?(:view_time_entries, @project) && @issue.spent_hours > 0 + tabs << {:name => 'changesets', :label => :label_associated_revisions, :remote => true, :onclick => "getRemoteTab('changesets', '#{tab_issue_path(@issue, :name => 'changesets')}', '#{issue_path(@issue, :tab => 'changesets')}')"} if @has_changesets tabs end + end diff --git a/app/views/common/_tabs.html.erb b/app/views/common/_tabs.html.erb index df7513e..def21ff 100644 --- a/app/views/common/_tabs.html.erb +++ b/app/views/common/_tabs.html.erb @@ -18,10 +18,10 @@ <% tabs.each do |tab| -%> - <%= content_tag('div', render(:partial => tab[:partial], :locals => {:tab => tab} ), + <%= content_tag('div', (render(:partial => tab[:partial], :locals => {:tab => tab}) if tab[:partial]) , :id => "tab-content-#{tab[:name]}", :style => (tab[:name] != selected_tab ? 'display:none' : nil), - :class => 'tab-content') if tab[:partial] %> + :class => 'tab-content') if tab[:partial] || tab[:remote] %> <% end -%> <%= javascript_tag default_action if default_action %> diff --git a/app/views/issues/_changesets.html.erb b/app/views/issues/_changesets.html.erb deleted file mode 100644 index 3bd775c..0000000 --- a/app/views/issues/_changesets.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -<% changesets.each do |changeset| %> -
-

<%= link_to_revision(changeset, changeset.repository, - :text => "#{l(:label_revision)} #{changeset.format_identifier}") %> - <% if changeset.filechanges.any? && User.current.allowed_to?(:browse_repository, changeset.project) %> - (<%= link_to(l(:label_diff), - :controller => 'repositories', - :action => 'diff', - :id => changeset.project, - :repository_id => changeset.repository.identifier_param, - :path => "", - :rev => changeset.identifier) %>) - <% end %> -
- <%= authoring(changeset.committed_on, changeset.author) %>

-
- <%= format_changeset_comments changeset %> -
-
-<% end %> diff --git a/app/views/issues/tabs/_changesets.html.erb b/app/views/issues/tabs/_changesets.html.erb index 7e2b01a..f869a5d 100644 --- a/app/views/issues/tabs/_changesets.html.erb +++ b/app/views/issues/tabs/_changesets.html.erb @@ -1,4 +1,4 @@ -<% tab[:locals][:changesets].each do |changeset| %> +<% @changesets.each do |changeset| %>

<%= avatar(changeset.user, :size => "24") %> diff --git a/app/views/issues/tabs/_time_entries.html.erb b/app/views/issues/tabs/_time_entries.html.erb index 931a8df..c34edc0 100644 --- a/app/views/issues/tabs/_time_entries.html.erb +++ b/app/views/issues/tabs/_time_entries.html.erb @@ -1,4 +1,4 @@ -<% for time_entry in tab[:locals][:time_entries] %> +<% for time_entry in time_entries%>
<% if time_entry.editable_by?(User.current) -%>
diff --git a/config/routes.rb b/config/routes.rb old mode 100644 new mode 100755 index 0344982..af21d0f --- a/config/routes.rb +++ b/config/routes.rb @@ -184,6 +184,7 @@ Rails.application.routes.draw do member do # Used when updating the form of an existing issue patch 'edit', :to => 'issues#edit' + get 'tab/:name', :action => 'issue_tab', :as => 'tab' end collection do match 'bulk_edit', :via => [:get, :post] diff --git a/lib/redmine.rb b/lib/redmine.rb old mode 100644 new mode 100755 index 6e7d109..6313f60 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -90,7 +90,7 @@ Redmine::AccessControl.map do |map| map.project_module :issue_tracking do |map| # Issues - map.permission :view_issues, {:issues => [:index, :show], + map.permission :view_issues, {:issues => [:index, :show, :issue_tab], :auto_complete => [:issues], :context_menus => [:issues], :versions => [:index, :show, :status_by], diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 9869dd9..ecb0d6e 100755 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -377,6 +377,24 @@ function showIssueHistory(journal, url) { return false; } +function getRemoteTab(name, remote_url, url) { + $('#tab-content-' + name).parent().find('.tab-content').hide(); + $('#tab-content-' + name).parent().find('div.tabs a').removeClass('selected'); + $('#tab-' + name).addClass('selected') + + replaceInHistory(url) + + $.ajax({ + url: remote_url, + type: 'get', + success: function(data){ + $('#tab-content-' + name).html(data).show(); + } + }); + + return false; +} + //replaces current URL with the "href" attribute of the current link //(only triggered if supported by browser) function replaceInHistory(url) { diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 2878b84..36119c7 100755 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -2108,9 +2108,11 @@ class IssuesControllerTest < Redmine::ControllerTest project.disable_module! :repository @request.session[:user_id] = 2 - get :show, :params => { - :id => issue.id - } + get :issue_tab, :params => { + :id => issue.id, + :name => 'changesets' + }, + :xhr => true assert_select 'a[href=?]', '/projects/ecookbook/repository/10/revisions/3' end @@ -2503,6 +2505,13 @@ class IssuesControllerTest < Redmine::ControllerTest assert_select 'div.tabs a[id=?]', 'tab-time_entries', :text => 'Spent time' end + get :issue_tab, :params => { + :id => 3, + :name => 'time_entries' + }, + :xhr => true + assert_response :success + assert_select 'div[id=?]', 'time-entry-3' do assert_select 'a[title=?][href=?]', 'Edit', '/time_entries/3/edit' assert_select 'a[title=?][href=?]', 'Delete', '/time_entries/3' diff --git a/test/integration/routing/issues_test.rb b/test/integration/routing/issues_test.rb old mode 100644 new mode 100755 index 2ca9a75..8ba9dd2 --- a/test/integration/routing/issues_test.rb +++ b/test/integration/routing/issues_test.rb @@ -33,6 +33,8 @@ class RoutingIssuesTest < Redmine::RoutingTest should_route 'GET /issues/64/edit' => 'issues#edit', :id => '64' should_route 'PUT /issues/64' => 'issues#update', :id => '64' should_route 'DELETE /issues/64' => 'issues#destroy', :id => '64' + + should_route "GET /issues/3/tab/time_entries" => 'issues#issue_tab', :id => '3', :name => 'time_entries' end def test_issues_bulk_edit -- 2.1.4