From f37ce05ac9c1316f43c84c2a61125a0166176f71 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 6 Jul 2012 11:55:33 +0200 Subject: [PATCH] SONAR-3432 reuse input field generation code --- .../WEB-INF/app/helpers/properties_helper.rb | 20 +++++------ ...elper.rb => rules_configuration_helper.rb} | 19 ++++++++-- .../WEB-INF/app/models/rules_parameter.rb | 36 +++++++------------ .../rules_configuration/_rule_param.html.erb | 14 ++------ 4 files changed, 40 insertions(+), 49 deletions(-) rename sonar-server/src/main/webapp/WEB-INF/app/helpers/{rules_parameters_helper.rb => rules_configuration_helper.rb} (70%) 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_parameters_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb similarity index 70% rename from sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_parameters_helper.rb rename to sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb index 76f745c2197..cfb5d27d028 100644 --- 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_configuration_helper.rb @@ -17,7 +17,7 @@ # License along with Sonar; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 # -module RulesParametersHelper +module RulesConfigurationHelper include PropertiesHelper PARAM_TYPE_STRING = "s" @@ -27,16 +27,29 @@ module RulesParametersHelper 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 "String" if type == PARAM_TYPE_STRING + 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 "Boolean" if type == PARAM_TYPE_BOOLEAN + 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 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? -%>
-- 2.39.5