# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
#
module DashboardHelper
- include WidgetPropertiesHelper
+ include PropertiesHelper
include MetricsHelper
include FiltersHelper
--- /dev/null
+#
+# 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| "<option value='#{f.id}'" + (value.to_s == f.id.to_s ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
+ end
+
+ def options_key(value, values)
+ values.collect { |f| "<option value='#{h(f.key)}'" + (value.to_s == f.key ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
+ end
+
+ def option_group(name, options)
+ options.empty? ? '' : "<optgroup label=\"#{h(name)}\">" + options + "</optgroup>"
+ end
+
+end
+++ /dev/null
-#
-# 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| "<option value='#{f.id}'" + (value.to_s == f.id.to_s ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
- end
-
- def options_key(value, values)
- values.collect { |f| "<option value='#{h(f.key)}'" + (value.to_s == f.key ? " selected='selected'" : "") + ">#{h(f.name)}</option>" }.to_s
- end
-
- def option_group(name, options)
- options.empty? ? '' : "<optgroup label=\"#{h(name)}\">" + options + "</optgroup>"
- end
-
-end
--- /dev/null
+#
+# 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
# 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
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