]> source.dussan.org Git - sonarqube.git/commitdiff
Properties
authorDavid Gageot <david@gageot.net>
Thu, 5 Jul 2012 13:07:24 +0000 (15:07 +0200)
committerDavid Gageot <david@gageot.net>
Thu, 5 Jul 2012 13:08:42 +0000 (15:08 +0200)
sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb [deleted file]
sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb

index 1dc049c3569b8032d7c768c54fca08c33f13b309..c8230344b33ec34374e5e726621982d5b2be18fb 100644 (file)
@@ -18,7 +18,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 #
 module DashboardHelper
-  include WidgetPropertiesHelper
+  include PropertiesHelper
   include MetricsHelper
   include FiltersHelper
 
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb
new file mode 100644 (file)
index 0000000..9376b0b
--- /dev/null
@@ -0,0 +1,71 @@
+#
+# Sonar, entreprise quality control tool.
+# Copyright (C) 2008-2012 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# Sonar is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 3 of the License, or (at your option) any later version.
+#
+# Sonar is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Sonar; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+#
+module PropertiesHelper
+
+  def property_value_field(definition, value)
+    val=value || definition.defaultValue()
+    if definition.type.name()==PropertyType::TYPE_INTEGER
+      text_field_tag definition.key(), val, :size => 10
+
+    elsif definition.type.name()==PropertyType::TYPE_FLOAT
+      text_field_tag definition.key(), val, :size => 10
+
+    elsif definition.type.name()==PropertyType::TYPE_BOOLEAN
+      check_box_tag definition.key(), "true", val=='true'
+
+    elsif definition.type.name()==PropertyType::TYPE_METRIC
+      select_tag definition.key(), options_grouped_by_domain(Metric.all.select{|m| m.display?}.sort_by{|m| m.short_name}, val, :include_empty => true)
+
+    elsif definition.type.name()==PropertyType::TYPE_STRING
+      text_field_tag definition.key(), val, :size => 10
+
+    elsif definition.type.name()==PropertyType::TYPE_FILTER
+      user_filters = options_key(value, ::Filter.find(:all, :conditions => ['user_id=?', current_user.id]).sort_by(&:id))
+      shared_filters = options_key(value, ::Filter.find(:all, :conditions => ['(user_id<>? or user_id is null) and shared=?', current_user.id, true]).sort_by(&:id))
+
+      filters_combo = select_tag definition.key(), option_group('My Filters', user_filters) + option_group('Shared Filters', shared_filters)
+      filter_link = link_to message('widget.filter.edit'), {:controller => :filters, :action => :manage}, :class => 'link-action'
+
+      "#{filters_combo} #{filter_link}"
+  else
+      hidden_field_tag definition.key()
+    end
+  end
+
+  def resource_value_field(value)
+    projects = Project.all(:conditions => {:scope => 'PRJ', :qualifier => 'TRK', :enabled => true})
+    sorted_projects = Api::Utils.insensitive_sort(projects, &:name)
+
+    select_tag 'resource_id', options_id(value, sorted_projects)
+  end
+
+  def options_id(value, values)
+    values.collect { |f| "<option value='#{f.id}'" + (value.to_s == f.id.to_s ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
+  end
+
+  def options_key(value, values)
+    values.collect { |f| "<option value='#{h(f.key)}'" + (value.to_s == f.key ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
+  end
+
+  def option_group(name, options)
+    options.empty? ? '' : "<optgroup label=\"#{h(name)}\">" + options + "</optgroup>"
+  end
+
+end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb
deleted file mode 100644 (file)
index 54b23ff..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-#
-# Sonar, entreprise quality control tool.
-# Copyright (C) 2008-2012 SonarSource
-# mailto:contact AT sonarsource DOT com
-#
-# Sonar is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 3 of the License, or (at your option) any later version.
-#
-# Sonar is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with Sonar; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
-#
-module WidgetPropertiesHelper
-
-  def property_value_field(definition, value)
-    val=value || definition.defaultValue()
-    if definition.type.name()==WidgetProperty::TYPE_INTEGER
-      text_field_tag definition.key(), val, :size => 10
-
-    elsif definition.type.name()==WidgetProperty::TYPE_FLOAT
-      text_field_tag definition.key(), val, :size => 10
-
-    elsif definition.type.name()==WidgetProperty::TYPE_BOOLEAN
-      check_box_tag definition.key(), "true", val=='true'
-
-    elsif definition.type.name()==WidgetProperty::TYPE_METRIC
-      select_tag definition.key(), options_grouped_by_domain(Metric.all.select{|m| m.display?}.sort_by{|m| m.short_name}, val, :include_empty => true)
-
-    elsif definition.type.name()==WidgetProperty::TYPE_STRING
-      text_field_tag definition.key(), val, :size => 10
-
-    elsif definition.type.name()==WidgetProperty::TYPE_FILTER
-      user_filters = options_key(value, ::Filter.find(:all, :conditions => ['user_id=?', current_user.id]).sort_by(&:id))
-      shared_filters = options_key(value, ::Filter.find(:all, :conditions => ['(user_id<>? or user_id is null) and shared=?', current_user.id, true]).sort_by(&:id))
-
-      filters_combo = select_tag definition.key(), option_group('My Filters', user_filters) + option_group('Shared Filters', shared_filters)
-      filter_link = link_to message('widget.filter.edit'), {:controller => :filters, :action => :manage}, :class => 'link-action'
-
-      "#{filters_combo} #{filter_link}"
-
-  else
-      hidden_field_tag definition.key()
-    end
-  end
-
-  def resource_value_field(value)
-    projects = Project.all(:conditions => {:scope => 'PRJ', :qualifier => 'TRK', :enabled => true})
-    sorted_projects = Api::Utils.insensitive_sort(projects, &:name)
-
-    select_tag 'resource_id', options_id(value, sorted_projects)
-  end
-
-  def options_id(value, values)
-    values.collect { |f| "<option value='#{f.id}'" + (value.to_s == f.id.to_s ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
-  end
-
-  def options_key(value, values)
-    values.collect { |f| "<option value='#{h(f.key)}'" + (value.to_s == f.key ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
-  end
-
-  def option_group(name, options)
-    options.empty? ? '' : "<optgroup label=\"#{h(name)}\">" + options + "</optgroup>"
-  end
-
-end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb
new file mode 100644 (file)
index 0000000..30780a6
--- /dev/null
@@ -0,0 +1,53 @@
+#
+# Sonar, entreprise quality control tool.
+# Copyright (C) 2008-2012 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# Sonar is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 3 of the License, or (at your option) any later version.
+#
+# Sonar is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Sonar; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+#
+class PropertyType
+  TYPE_INTEGER = 'INTEGER'
+  TYPE_BOOLEAN = 'BOOLEAN'
+  TYPE_FLOAT = 'FLOAT'
+  TYPE_STRING = 'STRING'
+  TYPE_METRIC = 'METRIC'
+  TYPE_FILTER = 'FILTER'
+
+  def self.text_to_value(text, type)
+    case type
+      when TYPE_INTEGER
+        text.to_i
+      when TYPE_FLOAT
+        Float(text)
+      when TYPE_BOOLEAN
+        text=='true'
+      when TYPE_METRIC
+        Metric.by_key(text)
+      else
+        text
+    end
+  end
+
+  def self.validate(key, type, optional, text_value, errors)
+    errors.add_to_base("Unknown type for property #{key}") unless type
+    if text_value.empty?
+      errors.add_to_base("#{key} is empty") unless optional
+    else
+      errors.add_to_base("#{key} is not an integer") if type==PropertyType::TYPE_INTEGER && !Api::Utils.is_integer?(text_value)
+      errors.add_to_base("#{key} is not a decimal number") if type==PropertyType::TYPE_FLOAT && !Api::Utils.is_number?(text_value)
+      errors.add_to_base("#{key} is not a boolean") if type==PropertyType::TYPE_BOOLEAN && !(text_value=="true" || text_value=="false")
+    end
+  end
+end
index fe4d4ba01e0bb0fc4b58d904bee3b8750a2156ce..a6b58412fc64b36ae30e0c145e9a61cea9ce8a29 100644 (file)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 #
 class WidgetProperty < ActiveRecord::Base
-  TYPE_INTEGER = 'INTEGER'
-  TYPE_BOOLEAN = 'BOOLEAN'
-  TYPE_FLOAT = 'FLOAT'
-  TYPE_STRING = 'STRING'
-  TYPE_METRIC = 'METRIC'
-  TYPE_FILTER = 'FILTER'
-
   belongs_to :widget
 
   validates_length_of :kee, :within => 1..100
@@ -73,31 +66,13 @@ class WidgetProperty < ActiveRecord::Base
   end
 
   def self.text_to_value(text, type)
-    case type
-      when TYPE_INTEGER
-        text.to_i
-      when TYPE_FLOAT
-        Float(text)
-      when TYPE_BOOLEAN
-        text=='true'
-      when TYPE_METRIC
-        Metric.by_key(text)
-      else
-        text
-    end
+    PropertyType.text_to_value(text, type);
   end
 
   protected
   def validate
     errors.add_to_base("Unknown property: #{key}") unless java_definition
-    errors.add_to_base("Unknown type for property #{key}") unless type
-    if text_value.empty?
-      errors.add_to_base("#{key} is empty") unless java_definition.optional()
-    else
-      errors.add_to_base("#{key} is not an integer") if type==TYPE_INTEGER && !Api::Utils.is_integer?(text_value)
-      errors.add_to_base("#{key} is not a decimal number") if type==TYPE_FLOAT && !Api::Utils.is_number?(text_value)
-      errors.add_to_base("#{key} is not a boolean") if type==TYPE_BOOLEAN && !(text_value=="true" || text_value=="false")
-    end
+    PropertyType::validate(key, type, java_definition.optional(), text_value, errors);
   end
 
 end