From: David Gageot Date: Thu, 5 Jul 2012 13:07:24 +0000 (+0200) Subject: Properties X-Git-Tag: 3.2~230 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c675a2b4e2cb696390f5ec2c877477ac0551e081;p=sonarqube.git Properties --- diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb index 1dc049c3569..c8230344b33 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb @@ -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 index 00000000000..9376b0b6497 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_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 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| "" }.to_s + end + + def options_key(value, values) + values.collect { |f| "" }.to_s + end + + def option_group(name, options) + options.empty? ? '' : "" + options + "" + 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 index 54b23ff922f..00000000000 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb +++ /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| "" }.to_s - end - - def options_key(value, values) - values.collect { |f| "" }.to_s - end - - def option_group(name, options) - options.empty? ? '' : "" + options + "" - 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 index 00000000000..30780a6c2f9 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb @@ -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 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb index fe4d4ba01e0..a6b58412fc6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb @@ -18,13 +18,6 @@ # 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