From c675a2b4e2cb696390f5ec2c877477ac0551e081 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 5 Jul 2012 15:07:24 +0200 Subject: [PATCH] Properties --- .../WEB-INF/app/helpers/dashboard_helper.rb | 2 +- ...perties_helper.rb => properties_helper.rb} | 15 +++--- .../WEB-INF/app/models/property_type.rb | 53 +++++++++++++++++++ .../WEB-INF/app/models/widget_property.rb | 29 +--------- 4 files changed, 63 insertions(+), 36 deletions(-) rename sonar-server/src/main/webapp/WEB-INF/app/helpers/{widget_properties_helper.rb => properties_helper.rb} (87%) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb 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/widget_properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb similarity index 87% rename from sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb rename to sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb index 54b23ff922f..9376b0b6497 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb @@ -17,26 +17,26 @@ # License along with Sonar; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 # -module WidgetPropertiesHelper +module PropertiesHelper def property_value_field(definition, value) val=value || definition.defaultValue() - if definition.type.name()==WidgetProperty::TYPE_INTEGER + if definition.type.name()==PropertyType::TYPE_INTEGER text_field_tag definition.key(), val, :size => 10 - elsif definition.type.name()==WidgetProperty::TYPE_FLOAT + elsif definition.type.name()==PropertyType::TYPE_FLOAT text_field_tag definition.key(), val, :size => 10 - elsif definition.type.name()==WidgetProperty::TYPE_BOOLEAN + elsif definition.type.name()==PropertyType::TYPE_BOOLEAN check_box_tag definition.key(), "true", val=='true' - elsif definition.type.name()==WidgetProperty::TYPE_METRIC + 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()==WidgetProperty::TYPE_STRING + elsif definition.type.name()==PropertyType::TYPE_STRING text_field_tag definition.key(), val, :size => 10 - elsif definition.type.name()==WidgetProperty::TYPE_FILTER + 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)) @@ -44,7 +44,6 @@ module WidgetPropertiesHelper 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 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 -- 2.39.5