]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3460 Improve dashboard migration
authorDavid Gageot <david@gageot.net>
Mon, 14 May 2012 15:25:10 +0000 (17:25 +0200)
committerDavid Gageot <david@gageot.net>
Mon, 14 May 2012 16:33:13 +0000 (18:33 +0200)
We should create one dashboard per filter and one active dashboard per active filter.

sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/models/active_dashboard.rb
sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb
sonar-server/src/main/webapp/WEB-INF/db/migrate/302_create_global_dashboards_for_filter.rb

index 5cd31a3d311a097646ee36d8be2244d08922dff9..e1e9fffd5719a55c648302ffb5de8ec748405405 100644 (file)
@@ -151,9 +151,9 @@ class DashboardController < ApplicationController
       elsif params[:name]
         @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['dashboards.name=? AND active_dashboards.user_id=?', params[:name], current_user.id])
       elsif params[:id]
-        @active=ActiveDashboard.user_dashboards(current_user).find { |a| !a.global? }
+        @active=ActiveDashboard.user_dashboards(current_user, false).first
       else
-        @active=ActiveDashboard.user_dashboards(current_user).find { |a| a.global? }
+        @active=ActiveDashboard.user_dashboards(current_user, true).first
       end
     end
 
@@ -164,9 +164,9 @@ class DashboardController < ApplicationController
       elsif params[:name]
         @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['dashboards.name=? AND active_dashboards.user_id IS NULL', params[:name]])
       elsif params[:id]
-        @active=ActiveDashboard.user_dashboards(nil).find { |a| !a.global? }
+        @active=ActiveDashboard.user_dashboards(nil, false).first
       else
-        @active=ActiveDashboard.user_dashboards(nil).find { |a| a.global? }
+        @active=ActiveDashboard.user_dashboards(nil, true).first
       end
     end
     @dashboard=(@active ? @active.dashboard : nil)
index 0cf468771ee1359e2c51546f6be96af5e62451eb..88612ac5dc1ec2c30dcd3b58937b0480aac046c0 100644 (file)
@@ -27,9 +27,7 @@ class DashboardsController < ApplicationController
   def index
     @global = !params[:resource]
 
-    @actives=ActiveDashboard.user_dashboards(current_user)
-    @actives.reject! { |a| a.global? != @global}
-
+    @actives=ActiveDashboard.user_dashboards(current_user, @global)
     @shared_dashboards=Dashboard.find(:all, :conditions => ['(user_id<>? OR user_id IS NULL) AND shared=?', current_user.id, true], :order => 'name ASC')
     active_dashboard_ids=@actives.map(&:dashboard_id)
     @shared_dashboards.reject! { |d| active_dashboard_ids.include?(d.id) }
index 6dd4a976e3dc212b66e1bfeedc16c37cd2441779..9227ce61cf7d577a0f66fe257fa0705cf319d73a 100644 (file)
@@ -51,18 +51,18 @@ class ActiveDashboard < ActiveRecord::Base
     user_id.nil?
   end
 
-  def self.user_dashboards(user)
+  def self.user_dashboards(user, global=false)
     result=nil
     if user && user.id
-      result=find(:all, :include => 'dashboard', :conditions => ['user_id=?', user.id], :order => 'order_index')
+      result=find(:all, :include => 'dashboard', :conditions => ['user_id=?', user.id], :order => 'order_index').select { |a| a.global? == global}
     end
     if result.nil? || result.empty?
-      result=default_dashboards
+      result=default_dashboards(global)
     end
     result
   end
 
-  def self.default_dashboards
-    find(:all, :include => 'dashboard', :conditions => ['user_id IS NULL'], :order => 'order_index')
+  def self.default_dashboards(global=false)
+    find(:all, :include => 'dashboard', :conditions => ['user_id IS NULL'], :order => 'order_index').select { |a| a.global? == global}
   end
 end
index b9da26594a357672932c6972608e8cbab71b9c95..3039a5a157010a1403be189b44f04be68ddf85da 100644 (file)
@@ -35,7 +35,7 @@
       <div id="sidebar">
         <ul>
           <% if selected_section==Navigation::SECTION_HOME %>
-            <% ActiveDashboard.user_dashboards(current_user).select(&:global?).each do |active_dashboard| %>
+            <% ActiveDashboard.user_dashboards(current_user, true).each do |active_dashboard| %>
               <li class="<%= 'selected' if @dashboard && controller.controller_path=='dashboard' && active_dashboard.dashboard_id==@dashboard.id -%>">
                 <a href="<%= ApplicationController.root_context -%>/dashboard/?did=<%= active_dashboard.dashboard_id -%>"><%= active_dashboard.dashboard.name(true) -%></a>
               </li>
@@ -55,7 +55,7 @@
             <% end %>
 
           <% elsif selected_section==Navigation::SECTION_RESOURCE %>
-            <% ActiveDashboard.user_dashboards(current_user).reject(&:global?).each do |active_dashboard| %>
+            <% ActiveDashboard.user_dashboards(current_user, false).each do |active_dashboard| %>
               <li class="<%= 'selected' if @dashboard && controller.controller_path=='dashboard' && active_dashboard.dashboard_id==@dashboard.id -%>">
                 <a href="<%= ApplicationController.root_context -%>/dashboard/index/<%= @project.id -%>?did=<%= active_dashboard.dashboard_id -%><%= "&"+period_param if period_param -%>"><%= active_dashboard.dashboard.name(true) -%></a>
               </li>
index 8ddccfa8e2afff1d017e8a85188f0c5befd63327..acd85806e6a0b494bd6c3cd09319584f38a08a6b 100644 (file)
@@ -38,22 +38,22 @@ class CreateGlobalDashboardsForFilter < ActiveRecord::Migration
   end
 
   def self.up
-    ActiveFilter.find(:all).each do |activeFilter|
-      filter = ::Filter.find(activeFilter.filter_id)
+    dashboard_per_filter = create_global_dahboards()
+    activate_dashboards(dashboard_per_filter)
+    drop_table('active_filters')
+  end
 
-      dashboard = Dashboard.create(:user_id => activeFilter.user_id,
+  def self.create_global_dahboards
+    dashboards = {}
+
+    ::Filter.find(:all).each do |filter|
+      dashboard = Dashboard.create(:user_id => filter.user_id,
                                    :name => filter.name,
                                    :description => '',
                                    :column_layout => '100%',
                                    :shared => filter.shared,
                                    :is_global => true)
 
-      if !filter.favourites || activeFilter.user_id
-        ActiveDashboard.create(:dashboard_id => dashboard.id,
-                               :user_id => activeFilter.user_id,
-                               :order_index => activeFilter.order_index)
-      end
-
       widget = Widget.create(:dashboard_id => dashboard.id,
                              :widget_key => 'filter',
                              :name => 'Filter',
@@ -64,9 +64,26 @@ class CreateGlobalDashboardsForFilter < ActiveRecord::Migration
       WidgetProperty.create(:widget_id => widget.id,
                             :kee => 'filter',
                             :text_value => filter.id)
+
+      dashboards[filter.id] = dashboard
     end
 
-    drop_table('active_filters')
+    dashboards
   end
+
+  def self.activate_dashboards(dashboard_per_filter)
+    ActiveFilter.find(:all).each do |activeFilter|
+      filter = ::Filter.find(activeFilter.filter_id)
+
+      dashboard = dashboard_per_filter[filter.id]
+
+      if !filter.favourites || activeFilter.user_id
+        ActiveDashboard.create(:dashboard_id => dashboard.id,
+                               :user_id => activeFilter.user_id,
+                               :order_index => activeFilter.order_index)
+      end
+    end
+  end
+
 end