]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3432 Prepare Rules Parameters to use shared code
authorDavid Gageot <david@gageot.net>
Thu, 5 Jul 2012 14:43:54 +0000 (16:43 +0200)
committerDavid Gageot <david@gageot.net>
Thu, 5 Jul 2012 14:44:01 +0000 (16:44 +0200)
sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_parameters_helper.rb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/models/rules_parameter.rb

diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_parameters_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_parameters_helper.rb
new file mode 100644 (file)
index 0000000..76f745c
--- /dev/null
@@ -0,0 +1,58 @@
+#
+# 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 RulesParametersHelper
+  include PropertiesHelper
+
+  PARAM_TYPE_STRING = "s"
+  PARAM_TYPE_STRING_LIST = "s{}"
+  PARAM_TYPE_INTEGER = "i"
+  PARAM_TYPE_INTEGER_LIST = "i{}"
+  PARAM_TYPE_BOOLEAN = "b"
+  PARAM_TYPE_REGEXP = "r"
+
+  def readable_type(type)
+    return "String" if type == PARAM_TYPE_STRING
+    return "Set of string (, as delimiter)" if type == PARAM_TYPE_STRING_LIST
+    return "Number" if type == PARAM_TYPE_INTEGER
+    return "Set of number (, as delimiter)" if type == PARAM_TYPE_INTEGER_LIST
+    return "Boolean" if type == PARAM_TYPE_BOOLEAN
+    return "Regular expression" if type == PARAM_TYPE_REGEXP
+    return "Set of values (, as delimiter)" if is_set(type)
+  end
+
+  def input_size(type)
+    return 15 if type == PARAM_TYPE_STRING
+    return 15 if type == PARAM_TYPE_STRING_LIST
+    return  8 if type == PARAM_TYPE_INTEGER
+    return  8 if type == PARAM_TYPE_INTEGER_LIST
+    return  4 if type == PARAM_TYPE_BOOLEAN
+    return 15 if type == PARAM_TYPE_REGEXP
+    if is_set(type)
+      size = (type.length / 2).to_i
+      size = 64 if size > 64
+      size
+    end
+  end
+
+  def is_set(type)
+    type.at(1) == "[" && type.ends_with?("]")
+  end
+end
+
index 184f49ec782ec6c3089384bd9a7468b89c345897..c6d96e7073ab37827460257c6effbc325b0e87d6 100644 (file)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 #
 class RulesParameter < ActiveRecord::Base
-
-  validates_presence_of :name, :param_type
+  include RulesParametersHelper
 
   PARAM_MAX_NUMBER = 4
 
-  PARAM_TYPE_STRING = "s"
-  PARAM_TYPE_STRING_LIST = "s{}"
-  PARAM_TYPE_INTEGER = "i"
-  PARAM_TYPE_INTEGER_LIST = "i{}"
-  PARAM_TYPE_BOOLEAN = "b"
-  PARAM_TYPE_REGEXP = "r"
-
+  validates_presence_of :name, :param_type
   belongs_to :rule
 
   def is_set_type
@@ -42,11 +35,11 @@ class RulesParameter < ActiveRecord::Base
 
   def description
     @l10n_description ||=
-        begin
-          result = Java::OrgSonarServerUi::JRubyFacade.getInstance().getRuleParamDescription(I18n.locale, rule.repository_key, rule.plugin_rule_key, name())
-          result = read_attribute(:description) unless result
-          result
-        end
+      begin
+        result = Java::OrgSonarServerUi::JRubyFacade.getInstance().getRuleParamDescription(I18n.locale, rule.repository_key, rule.plugin_rule_key, name())
+        result = read_attribute(:description) unless result
+        result
+      end
   end
 
   def description=(value)
@@ -54,24 +47,11 @@ class RulesParameter < ActiveRecord::Base
   end
 
   def readable_param_type
-    return "String" if param_type == PARAM_TYPE_STRING
-    return "Set of string (, as delimiter)" if param_type == PARAM_TYPE_STRING_LIST
-    return "Number" if param_type == PARAM_TYPE_INTEGER
-    return "Set of number (, as delimiter)" if param_type == PARAM_TYPE_INTEGER_LIST
-    return "Boolean" if param_type == PARAM_TYPE_BOOLEAN
-    return "Regular expression" if param_type == PARAM_TYPE_REGEXP
-    return "Set of values (, as delimiter)" if is_set_type
+    readable_type(param_type)
   end
 
   def input_box_size
-    return 15 if param_type == PARAM_TYPE_STRING or param_type == PARAM_TYPE_STRING_LIST or param_type == PARAM_TYPE_REGEXP
-    return 8 if param_type == PARAM_TYPE_INTEGER or param_type == PARAM_TYPE_INTEGER_LIST
-    return 4 if param_type == PARAM_TYPE_BOOLEAN
-    if is_set_type
-      size = (param_type.length / 2).to_i
-      size = 64 if size > 64
-      return size
-    end
+    input_size(param_type)
   end
 
   def validate_value(attribute, errors, value)
@@ -84,13 +64,13 @@ class RulesParameter < ActiveRecord::Base
           errors.add("#{value}", "Invalid value '" + provided_token + "'. Must be one of : " + allowed_tokens.join(", "))
         end
       end
-    elsif param_type == RulesParameter::PARAM_TYPE_INTEGER
+    elsif param_type == RulesParametersHelper::PARAM_TYPE_INTEGER
       begin
         Kernel.Integer(attribute)
       rescue
         errors.add("#{value}", "Invalid value '" + attribute + "'. Must be an integer.")
       end
-    elsif param_type == RulesParameter::PARAM_TYPE_INTEGER_LIST
+    elsif param_type == RulesParametersHelper::PARAM_TYPE_INTEGER_LIST
       provided_numbers = attribute.split(",")
       provided_numbers.each do |provided_number|
         begin
@@ -100,11 +80,11 @@ class RulesParameter < ActiveRecord::Base
           return
         end
       end
-    elsif param_type == RulesParameter::PARAM_TYPE_BOOLEAN
+    elsif param_type == RulesParametersHelper::PARAM_TYPE_BOOLEAN
       if attribute != "true" && attribute != "false"
         errors.add("#{value}", "Invalid value '" + attribute + "'. Must be one of : true,false")
       end
-    elsif param_type == RulesParameter::PARAM_TYPE_REGEXP
+    elsif param_type == RulesParametersHelper::PARAM_TYPE_REGEXP
       begin
         Regexp.new(attribute)
       rescue