From: David Gageot Date: Fri, 6 Jul 2012 09:55:33 +0000 (+0200) Subject: SONAR-3432 reuse input field generation code X-Git-Tag: 3.2~211 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f37ce05ac9c1316f43c84c2a61125a0166176f71;p=sonarqube.git SONAR-3432 reuse input field generation code --- 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 index 1f94cbf7c87..3d0e468e462 100644 --- 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 @@ -19,39 +19,39 @@ # module PropertiesHelper - def property_value(key, type, value) + def property_value(key, type, value, options = {}) if type==PropertyType::TYPE_INTEGER - text_field_tag key, value, :size => 10 + text_field_tag key, value, {:size => 10}.update(options) elsif type==PropertyType::TYPE_BOOLEAN - check_box_tag key, "true", value=='true' + check_box_tag key, "true", value=='true', options elsif type==PropertyType::TYPE_FLOAT - text_field_tag key, value, :size => 10 + text_field_tag key, value, {:size => 10}.update(options) elsif type==PropertyType::TYPE_STRING - text_field_tag key, value, :size => 10 + text_field_tag key, value, {:size => 10}.update(options) elsif type==PropertyType::TYPE_METRIC - select_tag key, options_grouped_by_domain(Metric.all.select{|m| m.display?}.sort_by(&:short_name), value, :include_empty => true) + select_tag key, options_grouped_by_domain(Metric.all.select{|m| m.display?}.sort_by(&:short_name), value, :include_empty => true), options elsif type==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 key, option_group('My Filters', user_filters) + option_group('Shared Filters', shared_filters) + filters_combo = select_tag key, option_group('My Filters', user_filters) + option_group('Shared Filters', shared_filters), options filter_link = link_to message('widget.filter.edit'), {:controller => :filters, :action => :manage}, :class => 'link-action' "#{filters_combo} #{filter_link}" elsif type==PropertyType::TYPE_TEXT - text_area_tag key, value, :size => '40x6' + text_area_tag key, value, {:size => '40x6'}.update(options) elsif type==PropertyType::TYPE_PASSWORD - password_field_tag key, value, :size => 10 + password_field_tag key, value, {:size => 10}.update(options) else - hidden_field_tag key + hidden_field_tag key, options end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb new file mode 100644 index 00000000000..cfb5d27d028 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb @@ -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 RulesConfigurationHelper + 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 property_type_for_param_type(type) + return PropertyType::TYPE_STRING if type == PARAM_TYPE_STRING + return PropertyType::TYPE_STRING if type == PARAM_TYPE_STRING_LIST + return PropertyType::TYPE_INTEGER if type == PARAM_TYPE_INTEGER + return PropertyType::TYPE_INTEGER if type == PARAM_TYPE_INTEGER_LIST + return PropertyType::TYPE_BOOLEAN if type == PARAM_TYPE_BOOLEAN + return PropertyType::TYPE_STRING if type == PARAM_TYPE_REGEXP + end + + def readable_type(type) + return "" 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 "" 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 param_value_input(parameter, value, options = {}) + property_value 'value', property_type_for_param_type(parameter.param_type), value, {:id => parameter.id}.update(options) + 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 + 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 deleted file mode 100644 index 76f745c2197..00000000000 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_parameters_helper.rb +++ /dev/null @@ -1,58 +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 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 - diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/rules_parameter.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/rules_parameter.rb index c6d96e7073a..1ed08f89e00 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/rules_parameter.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/rules_parameter.rb @@ -18,7 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 # class RulesParameter < ActiveRecord::Base - include RulesParametersHelper + include RulesConfigurationHelper PARAM_MAX_NUMBER = 4 @@ -46,14 +46,6 @@ class RulesParameter < ActiveRecord::Base write_attribute(:description, value) end - def readable_param_type - readable_type(param_type) - end - - def input_box_size - input_size(param_type) - end - def validate_value(attribute, errors, value) return if attribute.nil? or attribute.length == 0 if is_set_type @@ -61,34 +53,30 @@ class RulesParameter < ActiveRecord::Base allowed_tokens = get_allowed_tokens provided_tokens.each do |provided_token| if !allowed_tokens.include?(provided_token) - errors.add("#{value}", "Invalid value '" + provided_token + "'. Must be one of : " + allowed_tokens.join(", ")) + errors.add("#{value}", "'#{provided_token}' kust be one of : " + allowed_tokens.join(", ")) end end - elsif param_type == RulesParametersHelper::PARAM_TYPE_INTEGER - begin - Kernel.Integer(attribute) - rescue - errors.add("#{value}", "Invalid value '" + attribute + "'. Must be an integer.") + elsif param_type == RulesConfigurationHelper::PARAM_TYPE_INTEGER + if !Api::Utils.is_integer?(attribute) + errors.add("#{value}", "'#{attribute}' must be an integer.") end - elsif param_type == RulesParametersHelper::PARAM_TYPE_INTEGER_LIST + elsif param_type == RulesConfigurationHelper::PARAM_TYPE_INTEGER_LIST provided_numbers = attribute.split(",") provided_numbers.each do |provided_number| - begin - Kernel.Integer(provided_number) - rescue - errors.add("#{value}", "Invalid value '" + provided_number + "'. Must be an integer.") + if !Api::Utils.is_integer?(provided_number) + errors.add("#{value}", "'#{provided_number}' must be an integer.") return end end - elsif param_type == RulesParametersHelper::PARAM_TYPE_BOOLEAN + elsif param_type == RulesConfigurationHelper::PARAM_TYPE_BOOLEAN if attribute != "true" && attribute != "false" - errors.add("#{value}", "Invalid value '" + attribute + "'. Must be one of : true,false") + errors.add("#{value}", "'#{attribute}' must be one of : true,false") end - elsif param_type == RulesParametersHelper::PARAM_TYPE_REGEXP + elsif param_type == RulesConfigurationHelper::PARAM_TYPE_REGEXP begin Regexp.new(attribute) rescue - errors.add("#{value}", "Invalid regular expression '" + attribute + "'.") + errors.add("#{value}", "'#{attribute}' must be a regular expression") end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb index b7410b1ecd8..2530ab33a1b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb @@ -23,17 +23,9 @@ <% span_id = "text_#{parameter.id}" read_only = !active_rule || !enable_modification - - if param_value.length < 50 - textfield = text_field_tag parameter.id, param_value, :size => parameter.input_box_size, :name => "value", :disabled => read_only - textfield += link_to_function(image_tag("zoom.png"), "replaceTextField('#{span_id}', '#{parameter.id}')", :id => "toggle_text", :class => 'nolink') unless read_only - else - textfield = text_area_tag parameter.id, param_value, :size => "100x10", :name => "value", :disabled => read_only - textfield += "
" - end %> - <%= textfield -%> + <%= param_value_input(parameter, param_value, :disabled => read_only) -%> <% if !read_only %> <%= submit_tag message('update_verb') %> @@ -60,7 +52,5 @@ %> - <% if parameter.description && !parameter.description.blank? %> -
<%= h parameter.description -%>
- <% end %> +
<%= h(parameter.description || "") -%> <%= ('(' + readable_type(parameter.param_type) + ')') if !readable_type(parameter.param_type).empty? -%>