]> source.dussan.org Git - sonarqube.git/commitdiff
Use ajax instead of reloading the page when toggling issues favourite
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 18 Jul 2013 12:13:58 +0000 (14:13 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 18 Jul 2013 12:44:29 +0000 (14:44 +0200)
sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java
sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/views/issues/manage.html.erb
sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssueFilterServiceTest.java

index 6b092d4a65f3a2958238a43566f451825fcc9032..52c557a2e041e68a2ec83fe555078f0699fe5e1d 100644 (file)
@@ -555,10 +555,8 @@ public class InternalRubyIssueService implements ServerComponent {
     return issueFilterService.findFavoriteFilters(UserSession.get());
   }
 
-  public Result toggleFavouriteIssueFilter(Long issueFilterId) {
-    Result result = Result.of();
-    issueFilterService.toggleFavouriteIssueFilter(issueFilterId, UserSession.get());
-    return result;
+  public boolean toggleFavouriteIssueFilter(Long issueFilterId) {
+    return issueFilterService.toggleFavouriteIssueFilter(issueFilterId, UserSession.get());
   }
 
   /**
index cff4e165ce92c3aa0ca02ccf2336526c5243dc2d..0b54f6edf76e88a918d257da51c0f2ac63b7618d 100644 (file)
@@ -166,14 +166,19 @@ public class IssueFilterService implements ServerComponent {
     return toIssueFilters(filterDao.selectFavoriteFiltersByUser(getLoggedLogin(userSession)));
   }
 
-  public void toggleFavouriteIssueFilter(Long filterId, UserSession userSession) {
+  /**
+   * Return true if favorite is added, false if favorite is removed
+   */
+  public boolean toggleFavouriteIssueFilter(Long filterId, UserSession userSession) {
     String user = getLoggedLogin(userSession);
     findIssueFilterDto(filterId, user);
     IssueFilterFavouriteDto issueFilterFavouriteDto = selectFavouriteFilterForUser(filterId, user);
     if (issueFilterFavouriteDto == null) {
       addFavouriteIssueFilter(filterId, user);
+      return true;
     } else {
       deleteFavouriteIssueFilter(issueFilterFavouriteDto);
+      return false;
     }
   }
 
index 98f965c30b6848ba57a9828c271e4992c0f60a82..da39a9472f860d58ba8b096351858761106632a1 100644 (file)
@@ -22,7 +22,7 @@ require 'set'
 class IssuesController < ApplicationController
 
   before_filter :init_options
-  before_filter :load_fav_filters, :only => [:index, :search, :filter, :manage, :toggle_fav]
+  before_filter :load_fav_filters, :only => [:index, :search, :filter, :manage, :favourites, :toggle_fav]
 
   PAGE_SIZE = 100
 
@@ -139,12 +139,17 @@ class IssuesController < ApplicationController
     redirect_to :action => 'manage'
   end
 
+  # GET /issues/favourites
+  def favourites
+    verify_ajax_request
+    render :partial => 'issues/filter_favourites'
+  end
+
   # POST /issues/toggle_fav/<filter id>
   def toggle_fav
     verify_ajax_request
     require_parameters :id
-    Internal.issues.toggleFavouriteIssueFilter(params[:id].to_i)
-    render :text => '', :status => 200
+    render :text => Internal.issues.toggleFavouriteIssueFilter(params[:id].to_i), :status => 200
   end
 
   # GET /issues/bulk_change_form?[&criteria]
index e83daf4af9a32171fc78ceac046ea6f980e44fdd..76c587377bc58aab86278441f9e83cbac842b402 100644 (file)
@@ -143,10 +143,11 @@ class MeasuresController < ApplicationController
       @filter.user = User.find_by_login(params[:owner])
     end
 
-    # SONAR-4469
-    # If filter become unshared then remove all favorite filters linked to it, expect favorite of filter's owner
-    MeasureFilterFavourite.delete_all(['user_id<>? and measure_filter_id=?', @filter.user.id, params[:id]]) if params[:shared]!='true'
     if @filter.save
+      # SONAR-4469
+      # If filter become unshared then remove all favorite filters linked to it, expect favorite of filter's owner
+      MeasureFilterFavourite.delete_all(['user_id<>? and measure_filter_id=?', @filter.user.id, params[:id]]) if params[:shared]!='true'
+
       render :text => @filter.id.to_s, :status => 200
     else
       render :partial => 'measures/edit_form', :status => 400
index 7ed6a92d2d47693e5df72d8ddf42bec90078f3c4..cee24e286550bfdb2d1890e27f8a1b6bba9d7e3d 100644 (file)
@@ -3,12 +3,20 @@
     $j(document).ready(function () {
       $j(".issue-filter-star").click(function () {
         var filterId = $j(this).attr('filter-id');
+        var star = $j(this);
         $j.ajax({
           type: 'POST',
           url: baseUrl + "/issues/toggle_fav",
           data: {id: filterId},
           success: function (data) {
-            window.location = baseUrl + '/issues/manage';
+            if (data == 'true') {
+              star.removeClass('notfav').addClass('fav');
+              star.attr('title', '<%= escape_javascript message('click_to_remove_from_favourites') -%>');
+            } else {
+              star.removeClass('fav').addClass('notfav');
+              star.attr('title', '<%= escape_javascript message('click_to_add_to_favourites') -%>');
+            }
+            $j('#sidebar-favourites').load(baseUrl + '/issues/favourites');
           }
         });
       });
index f2a7c891e50b9b497adc0b2014a3140283bb0194..7edbdb503ccf48567a453302c3a84c51fdc67c9a 100644 (file)
@@ -525,8 +525,7 @@ public class InternalRubyIssueServiceTest {
 
   @Test
   public void should_toggle_favourite_issue_filter() {
-    Result result = service.toggleFavouriteIssueFilter(10L);
-    assertThat(result.ok()).isTrue();
+    service.toggleFavouriteIssueFilter(10L);
     verify(issueFilterService).toggleFavouriteIssueFilter(eq(10L), any(UserSession.class));
   }
 
index a2cc7bd783c1682a8f13637cf0d2402ec7731507..0cd775e1f329396f0b5e906af04d17b1bb9670cd 100644 (file)
@@ -583,7 +583,8 @@ public class IssueFilterServiceTest {
     when(issueFilterFavouriteDao.selectByFilterId(1L)).thenReturn(Collections.<IssueFilterFavouriteDto> emptyList());
 
     ArgumentCaptor<IssueFilterFavouriteDto> issueFilterFavouriteDtoCaptor = ArgumentCaptor.forClass(IssueFilterFavouriteDto.class);
-    service.toggleFavouriteIssueFilter(1L, userSession);
+    boolean result = service.toggleFavouriteIssueFilter(1L, userSession);
+    assertThat(result).isTrue();
     verify(issueFilterFavouriteDao).insert(issueFilterFavouriteDtoCaptor.capture());
 
     IssueFilterFavouriteDto issueFilterFavouriteDto = issueFilterFavouriteDtoCaptor.getValue();
@@ -598,7 +599,8 @@ public class IssueFilterServiceTest {
     when(issueFilterFavouriteDao.selectByFilterId(1L)).thenReturn(Collections.<IssueFilterFavouriteDto> emptyList());
 
     ArgumentCaptor<IssueFilterFavouriteDto> issueFilterFavouriteDtoCaptor = ArgumentCaptor.forClass(IssueFilterFavouriteDto.class);
-    service.toggleFavouriteIssueFilter(1L, userSession);
+    boolean result = service.toggleFavouriteIssueFilter(1L, userSession);
+    assertThat(result).isTrue();
     verify(issueFilterFavouriteDao).insert(issueFilterFavouriteDtoCaptor.capture());
 
     IssueFilterFavouriteDto issueFilterFavouriteDto = issueFilterFavouriteDtoCaptor.getValue();
@@ -612,7 +614,8 @@ public class IssueFilterServiceTest {
     // The filter is in the favorite list --> remove favorite
     when(issueFilterFavouriteDao.selectByFilterId(1L)).thenReturn(newArrayList(new IssueFilterFavouriteDto().setId(10L).setUserLogin("john").setIssueFilterId(1L)));
 
-    service.toggleFavouriteIssueFilter(1L, userSession);
+    boolean result = service.toggleFavouriteIssueFilter(1L, userSession);
+    assertThat(result).isFalse();
     verify(issueFilterFavouriteDao).delete(10L);
   }