Commit 7ad294ee9f70b846847a4f763ba7b2e82c24e410
1 parent
4cbb0d67
Exists in
master
and in
23 other branches
[Mezuro] Feature of create a configuration based on a old one
Showing
5 changed files
with
58 additions
and
28 deletions
Show diff stats
plugins/mezuro/lib/kalibro/configuration.rb
| @@ -22,10 +22,11 @@ class Kalibro::Configuration < Kalibro::Model | @@ -22,10 +22,11 @@ class Kalibro::Configuration < Kalibro::Model | ||
| 22 | new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] | 22 | new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] |
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | - def self.create(content) | ||
| 26 | - attributes = { | ||
| 27 | - :name => content.name, | ||
| 28 | - :description => content.description | 25 | + def self.create(content, configuration) |
| 26 | + attributes = { | ||
| 27 | + :name => content.name, | ||
| 28 | + :description => content.description, | ||
| 29 | + :metric_configuration => configuration.to_hash[:metric_configuration] | ||
| 29 | } | 30 | } |
| 30 | super attributes | 31 | super attributes |
| 31 | end | 32 | end |
| @@ -34,7 +35,13 @@ class Kalibro::Configuration < Kalibro::Model | @@ -34,7 +35,13 @@ class Kalibro::Configuration < Kalibro::Model | ||
| 34 | request("Configuration", :get_configuration_names)[:configuration_name] | 35 | request("Configuration", :get_configuration_names)[:configuration_name] |
| 35 | end | 36 | end |
| 36 | 37 | ||
| 37 | - def destroy | 38 | + def destroy |
| 38 | self.class.request("Configuration", :remove_configuration, {:configuration_name => name}) | 39 | self.class.request("Configuration", :remove_configuration, {:configuration_name => name}) |
| 39 | end | 40 | end |
| 41 | + | ||
| 42 | + def update_attributes(attributes={}) | ||
| 43 | + attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } | ||
| 44 | + save | ||
| 45 | + end | ||
| 46 | + | ||
| 40 | end | 47 | end |
plugins/mezuro/lib/mezuro_plugin/configuration_content.rb
| @@ -9,7 +9,7 @@ class MezuroPlugin::ConfigurationContent < Article | @@ -9,7 +9,7 @@ class MezuroPlugin::ConfigurationContent < Article | ||
| 9 | 'Sets of thresholds to interpret metrics' | 9 | 'Sets of thresholds to interpret metrics' |
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | - settings_items :description | 12 | + settings_items :description, :clone_configuration_name |
| 13 | 13 | ||
| 14 | include ActionView::Helpers::TagHelper | 14 | include ActionView::Helpers::TagHelper |
| 15 | def to_html(options = {}) | 15 | def to_html(options = {}) |
| @@ -20,21 +20,20 @@ class MezuroPlugin::ConfigurationContent < Article | @@ -20,21 +20,20 @@ class MezuroPlugin::ConfigurationContent < Article | ||
| 20 | 20 | ||
| 21 | def configuration | 21 | def configuration |
| 22 | begin | 22 | begin |
| 23 | - configuration = Kalibro::Configuration.find_by_name(self.name) | ||
| 24 | - configuration.description = self.description | ||
| 25 | - configuration | ||
| 26 | - rescue Exception | ||
| 27 | - Kalibro::Configuration.new({ | ||
| 28 | - :name => self.name, | ||
| 29 | - :description => self.description | ||
| 30 | - }) | 23 | + @configuration ||= Kalibro::Configuration.find_by_name(self.name) |
| 24 | + rescue Exception => error | ||
| 25 | + errors.add_to_base(error.message) | ||
| 26 | + nil | ||
| 31 | end | 27 | end |
| 32 | end | 28 | end |
| 33 | - | 29 | + |
| 34 | def metric_configurations | 30 | def metric_configurations |
| 35 | configuration.metric_configurations | 31 | configuration.metric_configurations |
| 36 | end | 32 | end |
| 37 | - | 33 | + |
| 34 | + def configuration_names | ||
| 35 | + ["None"] + Kalibro::Configuration.all_names.sort | ||
| 36 | + end | ||
| 38 | 37 | ||
| 39 | after_save :send_configuration_to_service | 38 | after_save :send_configuration_to_service |
| 40 | after_destroy :remove_configuration_from_service | 39 | after_destroy :remove_configuration_from_service |
| @@ -42,8 +41,7 @@ class MezuroPlugin::ConfigurationContent < Article | @@ -42,8 +41,7 @@ class MezuroPlugin::ConfigurationContent < Article | ||
| 42 | private | 41 | private |
| 43 | 42 | ||
| 44 | def validate_kalibro_configuration_name | 43 | def validate_kalibro_configuration_name |
| 45 | - existing = Kalibro::Configuration.all_names | ||
| 46 | - existing.each { |a| a.downcase!} | 44 | + existing = configuration_names.map { |a| a.downcase} |
| 47 | 45 | ||
| 48 | if existing.include?(name.downcase) | 46 | if existing.include?(name.downcase) |
| 49 | errors.add_to_base("Configuration name already exists in Kalibro") | 47 | errors.add_to_base("Configuration name already exists in Kalibro") |
| @@ -51,11 +49,24 @@ class MezuroPlugin::ConfigurationContent < Article | @@ -51,11 +49,24 @@ class MezuroPlugin::ConfigurationContent < Article | ||
| 51 | end | 49 | end |
| 52 | 50 | ||
| 53 | def send_configuration_to_service | 51 | def send_configuration_to_service |
| 54 | - configuration.save | 52 | + if configuration.nil? |
| 53 | + begin | ||
| 54 | + clone_configuration = Kalibro::Configuration.find_by_name(self.clone_configuration_name) | ||
| 55 | + rescue Exception => error | ||
| 56 | + clone_configuration = nil | ||
| 57 | + end | ||
| 58 | + Kalibro::Configuration.create(self, clone_configuration) | ||
| 59 | + else | ||
| 60 | + configuration.update_attributes({:description => description}) | ||
| 61 | + end | ||
| 55 | end | 62 | end |
| 56 | 63 | ||
| 57 | def remove_configuration_from_service | 64 | def remove_configuration_from_service |
| 58 | - configuration.destroy | 65 | + begin |
| 66 | + configuration.destroy | ||
| 67 | + rescue Exception => error | ||
| 68 | + errors.add_to_base(error.message) | ||
| 69 | + end | ||
| 59 | end | 70 | end |
| 60 | 71 | ||
| 61 | end | 72 | end |
plugins/mezuro/test/unit/mezuro_plugin/configuration_content_test.rb
| @@ -61,5 +61,5 @@ class ConfigurationContentTest < ActiveSupport::TestCase | @@ -61,5 +61,5 @@ class ConfigurationContentTest < ActiveSupport::TestCase | ||
| 61 | @configuration.expects(:destroy) | 61 | @configuration.expects(:destroy) |
| 62 | @content.send :remove_configuration_from_service | 62 | @content.send :remove_configuration_from_service |
| 63 | end | 63 | end |
| 64 | - | 64 | + |
| 65 | end | 65 | end |
plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb
| 1 | <h1> <%= _(MezuroPlugin::ConfigurationContent.short_description) %> </h1> | 1 | <h1> <%= _(MezuroPlugin::ConfigurationContent.short_description) %> </h1> |
| 2 | 2 | ||
| 3 | <% | 3 | <% |
| 4 | -begin | ||
| 5 | - @configuration = @article.title.nil? ? nil : Kalibro::Configuration.find_by_name(@article.title) | ||
| 6 | -rescue | ||
| 7 | - @configuration = nil | ||
| 8 | -end | 4 | + begin |
| 5 | + configuration = @article.title.nil? ? nil : @article.configuration | ||
| 6 | + rescue | ||
| 7 | + configuration = nil | ||
| 8 | + end | ||
| 9 | %> | 9 | %> |
| 10 | 10 | ||
| 11 | <%= error_messages_for 'kalibro_configuration' %> | 11 | <%= error_messages_for 'kalibro_configuration' %> |
| @@ -13,8 +13,20 @@ end | @@ -13,8 +13,20 @@ end | ||
| 13 | <%= hidden_field_tag 'kalibro_configuration[profile_id]', profile.id %> | 13 | <%= hidden_field_tag 'kalibro_configuration[profile_id]', profile.id %> |
| 14 | <%= hidden_field_tag 'id', @article.id %> | 14 | <%= hidden_field_tag 'id', @article.id %> |
| 15 | 15 | ||
| 16 | +<% configuration_names = @article.configuration_names %> | ||
| 17 | + | ||
| 18 | +<% selected = (configuration.nil? ? configuration_names[0] : @article.clone_configuration_name) %> | ||
| 19 | + | ||
| 16 | <%= required_fields_message %> | 20 | <%= required_fields_message %> |
| 17 | 21 | ||
| 18 | -<%= required f.text_field(:name, :disabled => !(@configuration.nil? || @article.id.nil?)) %> | 22 | +<%= required labelled_form_field _('Clone Configuration'), |
| 23 | +if !configuration.nil? && !@article.id.nil? | ||
| 24 | + f.select(:clone_configuration_name, configuration_names, {:selected => selected}, :disabled => 'true') | ||
| 25 | +else | ||
| 26 | + f.select(:clone_configuration_name, configuration_names, {:selected => selected}) | ||
| 27 | +end %> | ||
| 28 | +<br/> | ||
| 29 | + | ||
| 30 | +<%= required f.text_field(:name, :disabled => !(configuration.nil? || @article.id.nil?)) %> | ||
| 19 | 31 | ||
| 20 | <%= f.text_field :description %><br/> | 32 | <%= f.text_field :description %><br/> |
plugins/mezuro/views/content_viewer/show_configuration.rhtml