Commit 9aeb6dc29cde1e2c8e9cccf9787929a2d75c2d9a
Committed by
Paulo Meireles
1 parent
56378671
Exists in
master
and in
29 other branches
[Mezuro] Creating, editing and removing Configuration.
Showing
9 changed files
with
145 additions
and
101 deletions
Show diff stats
plugins/mezuro/lib/kalibro/configuration.rb
1 | 1 | class Kalibro::Configuration < Kalibro::Model |
2 | 2 | |
3 | 3 | attr_accessor :id, :name, :description |
4 | - | |
4 | + | |
5 | + def id=(value) | |
6 | + @id = value.to_i | |
7 | + end | |
8 | + | |
5 | 9 | def self.configuration_of(repository_id) |
6 | 10 | new request(:configuration_of, {:repository_id => repository_id})[:configuration] |
7 | 11 | end |
... | ... | @@ -13,10 +17,4 @@ class Kalibro::Configuration < Kalibro::Model |
13 | 17 | response.map {|configuration| new configuration} |
14 | 18 | end |
15 | 19 | |
16 | - | |
17 | - def update_attributes(attributes={}) | |
18 | - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } | |
19 | - save | |
20 | - end | |
21 | - | |
22 | 20 | end | ... | ... |
plugins/mezuro/lib/mezuro_plugin/configuration_content.rb
1 | 1 | class MezuroPlugin::ConfigurationContent < Article |
2 | - validate_on_create :validate_kalibro_configuration_name | |
2 | + validate_on_create :validate_configuration_name | |
3 | 3 | |
4 | - settings_items :kalibro_id, :description, :configuration_to_clone_name | |
4 | + settings_items :configuration_id | |
5 | 5 | |
6 | - after_save :send_kalibro_configuration_to_service | |
7 | - after_destroy :remove_kalibro_configuration_from_service | |
6 | + before_save :send_configuration_to_service | |
7 | + after_destroy :remove_configuration_from_service | |
8 | 8 | |
9 | 9 | def self.short_description |
10 | 10 | 'Mezuro configuration' |
11 | 11 | end |
12 | 12 | |
13 | 13 | def self.description |
14 | - 'Sets of thresholds to interpret metrics' | |
14 | + 'Set of metric configurations to interpret a Kalibro project' | |
15 | 15 | end |
16 | 16 | |
17 | 17 | include ActionView::Helpers::TagHelper |
... | ... | @@ -21,25 +21,21 @@ class MezuroPlugin::ConfigurationContent < Article |
21 | 21 | end |
22 | 22 | end |
23 | 23 | |
24 | - def kalibro_configuration | |
24 | + def kalibro_configuration #Can't be just "configuration", method name exists somewhere in noosfero | |
25 | 25 | begin |
26 | - @kalibro_configuration ||= Kalibro::Configuration.find(self.kalibro_id) | |
26 | + @configuration ||= Kalibro::Configuration.find(self.configuration_id) | |
27 | 27 | rescue Exception => exception |
28 | 28 | errors.add_to_base(exception.message) |
29 | 29 | end |
30 | - @kalibro_configuration | |
30 | + @configuration | |
31 | 31 | end |
32 | 32 | |
33 | -# def metric_configurations | |
34 | -# kalibro_configuration.metric_configurations | |
35 | -# end | |
36 | - | |
37 | - def kalibro_configuration_names_and_ids | |
33 | + def configuration_names_and_ids | |
38 | 34 | all_names_and_ids = {} |
39 | 35 | begin |
40 | 36 | all_configurations = Kalibro::Configuration.all |
41 | - if(!all_configurations.nil?) | |
42 | - all_configuration.each do |configuration| | |
37 | + if(!all_configurations.empty?) | |
38 | + all_configurations.each do |configuration| | |
43 | 39 | all_names_and_ids[configuration.id] = configuration.name |
44 | 40 | end |
45 | 41 | end |
... | ... | @@ -50,40 +46,63 @@ class MezuroPlugin::ConfigurationContent < Article |
50 | 46 | all_names_and_ids |
51 | 47 | end |
52 | 48 | |
49 | + def description=(value) | |
50 | + @description=value | |
51 | + end | |
52 | + | |
53 | + def description | |
54 | + begin | |
55 | + @description ||= kalibro_configuration.description | |
56 | + rescue | |
57 | + @description = "" | |
58 | + end | |
59 | + @description | |
60 | + end | |
61 | + | |
62 | + def metric_configurations | |
63 | + begin | |
64 | + @metric_configurations ||= Kalibro::MetricConfiguration.metric_configurations_of(configuration_id) | |
65 | + rescue Exception => error | |
66 | + errors.add_to_base(error.message) | |
67 | + @metric_configurations = [] | |
68 | + end | |
69 | + @metric_configurations | |
70 | + end | |
71 | + | |
72 | + def metric_configurations=(value) | |
73 | + @metric_configurations = value.kind_of?(Array) ? value : [value] | |
74 | + @metric_configurations = @metric_configurations.map { |element| to_metric_configuration(element) } | |
75 | + end | |
76 | + | |
53 | 77 | private |
54 | 78 | |
55 | - def validate_kalibro_configuration_name | |
56 | - existing = kalibro_configuration_names.map { |a| a.downcase} | |
79 | + def self.to_metric_configuration value | |
80 | + value.kind_of?(Hash) ? Kalibro::MetricConfiguration.new(value) : value | |
81 | + end | |
82 | + | |
83 | + def validate_configuration_name | |
84 | + existing = configuration_names_and_ids.values.map { |a| a.downcase} | |
57 | 85 | |
58 | 86 | if existing.include?(name.downcase) |
59 | 87 | errors.add_to_base("Configuration name already exists in Kalibro") |
60 | 88 | end |
61 | 89 | end |
62 | 90 | |
63 | - def send_kalibro_configuration_to_service | |
64 | - if editing_kalibro_configuration? | |
65 | - kalibro_configuration.update_attributes({:description => description}) | |
66 | - else | |
67 | - create_kalibro_configuration | |
68 | - end | |
91 | + def send_configuration_to_service | |
92 | + attributes = {:id => configuration_id, :name => name, :description => description} | |
93 | +# if cloning_configuration? | |
94 | +# attributes[:metric_configuration] = configuration_to_clone.metric_configurations_hash | |
95 | +# end | |
96 | + created_configuration = Kalibro::Configuration.create attributes | |
97 | + self.configuration_id = created_configuration.id | |
69 | 98 | end |
70 | 99 | |
71 | - def remove_kalibro_configuration_from_service | |
100 | + def remove_configuration_from_service | |
101 | + puts "aqui tem #{@configuration.inspect}" | |
72 | 102 | kalibro_configuration.destroy unless kalibro_configuration.nil? |
73 | 103 | end |
74 | 104 | |
75 | - def create_kalibro_configuration | |
76 | - attributes = {:name => name, :description => description} | |
77 | - if cloning_kalibro_configuration? | |
78 | - attributes[:metric_configuration] = configuration_to_clone.metric_configurations_hash | |
79 | - end | |
80 | - Kalibro::Configuration.create attributes | |
81 | - end | |
82 | - | |
83 | - def editing_kalibro_configuration? | |
84 | - kalibro_configuration.present? | |
85 | - end | |
86 | - | |
105 | +=begin | |
87 | 106 | def configuration_to_clone |
88 | 107 | @configuration_to_clone ||= find_configuration_to_clone |
89 | 108 | end |
... | ... | @@ -92,8 +111,9 @@ class MezuroPlugin::ConfigurationContent < Article |
92 | 111 | (configuration_to_clone_name == "None") ? nil : Kalibro::Configuration.find_by_name(configuration_to_clone_name) |
93 | 112 | end |
94 | 113 | |
95 | - def cloning_kalibro_configuration? | |
114 | + def cloning_configuration? | |
96 | 115 | configuration_to_clone.present? |
97 | 116 | end |
117 | +=end | |
98 | 118 | |
99 | 119 | end | ... | ... |
plugins/mezuro/lib/mezuro_plugin/project_content.rb
... | ... | @@ -3,6 +3,9 @@ class MezuroPlugin::ProjectContent < Article |
3 | 3 | |
4 | 4 | settings_items :project_id |
5 | 5 | |
6 | + before_save :send_project_to_service | |
7 | + after_destroy :destroy_project_from_service | |
8 | + | |
6 | 9 | def self.short_description |
7 | 10 | 'Mezuro project' |
8 | 11 | end |
... | ... | @@ -54,9 +57,6 @@ class MezuroPlugin::ProjectContent < Article |
54 | 57 | @repositories = @repositories.map { |element| to_repository(element) } |
55 | 58 | end |
56 | 59 | |
57 | - before_save :send_project_to_service | |
58 | - after_destroy :destroy_project_from_service | |
59 | - | |
60 | 60 | private |
61 | 61 | |
62 | 62 | def self.to_repository value | ... | ... |
plugins/mezuro/lib/mezuro_plugin/reading_group_content.rb
... | ... | @@ -3,6 +3,9 @@ class MezuroPlugin::ReadingGroupContent < Article |
3 | 3 | |
4 | 4 | settings_items :reading_group_id |
5 | 5 | |
6 | + before_save :send_reading_group_to_service | |
7 | + after_destroy :destroy_reading_group_from_service | |
8 | + | |
6 | 9 | def self.short_description |
7 | 10 | 'Mezuro reading group' |
8 | 11 | end |
... | ... | @@ -54,9 +57,6 @@ class MezuroPlugin::ReadingGroupContent < Article |
54 | 57 | @readings = @readings.map { |element| to_reading(element) } |
55 | 58 | end |
56 | 59 | |
57 | - before_save :send_reading_group_to_service | |
58 | - after_destroy :destroy_reading_group_from_service | |
59 | - | |
60 | 60 | private |
61 | 61 | |
62 | 62 | def self.to_reading value | ... | ... |
plugins/mezuro/test/fixtures/configuration_content_fixtures.rb
0 → 100644
... | ... | @@ -0,0 +1,23 @@ |
1 | +class ConfigurationContentFixtures | |
2 | + | |
3 | + def self.configuration_content | |
4 | + MezuroPlugin::ConfigurationContent.new configuration_content_hash | |
5 | + end | |
6 | + | |
7 | + def self.created_configuration_content | |
8 | + MezuroPlugin::ConfigurationContent.new( { | |
9 | + :name => 'Sample Configuration', | |
10 | + :description => 'Kalibro configuration for Java projects.', | |
11 | + :configuration_id => nil | |
12 | + } ) | |
13 | + end | |
14 | + | |
15 | + def self.configuration_content_hash | |
16 | + { | |
17 | + :name => 'Sample Configuration', | |
18 | + :description => 'Kalibro configuration for Java projects.', | |
19 | + :configuration_id => "42" | |
20 | + } | |
21 | + end | |
22 | + | |
23 | +end | ... | ... |
plugins/mezuro/test/fixtures/configuration_fixtures.rb
... | ... | @@ -21,16 +21,8 @@ class ConfigurationFixtures |
21 | 21 | } |
22 | 22 | end |
23 | 23 | |
24 | - def self.configuration_content(clone_configuration) | |
25 | - MezuroPlugin::ConfigurationContent.new({ | |
26 | - :name => 'Sample Configuration', | |
27 | - :description => 'Kalibro configuration for Java projects.', | |
28 | - :configuration_to_clone_name => clone_configuration | |
29 | - }) | |
24 | + def self.all | |
25 | + [configuration] | |
30 | 26 | end |
31 | 27 | |
32 | -def self.all | |
33 | - [configuration] | |
34 | -end | |
35 | - | |
36 | 28 | end | ... | ... |
plugins/mezuro/test/unit/mezuro_plugin/configuration_content_test.rb
1 | 1 | require "test_helper" |
2 | 2 | |
3 | 3 | require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/configuration_fixtures" |
4 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/configuration_content_fixtures" | |
4 | 5 | |
5 | 6 | class ConfigurationContentTest < ActiveSupport::TestCase |
6 | 7 | |
7 | 8 | def setup |
8 | 9 | @configuration = ConfigurationFixtures.configuration |
9 | - @content = ConfigurationFixtures.configuration_content("None") | |
10 | + @content = ConfigurationContentFixtures.configuration_content | |
11 | + @created_configuration = ConfigurationFixtures.created_configuration | |
12 | + @content_hash = ConfigurationContentFixtures.configuration_content_hash | |
13 | + @configuration_hash = {:name => @content_hash[:name], :description => @content_hash[:description], :id => @content_hash[:configuration_id]} | |
14 | + @created_content = ConfigurationContentFixtures.created_configuration_content | |
10 | 15 | end |
11 | 16 | |
12 | 17 | should 'be an article' do |
... | ... | @@ -18,7 +23,7 @@ class ConfigurationContentTest < ActiveSupport::TestCase |
18 | 23 | end |
19 | 24 | |
20 | 25 | should 'provide proper description' do |
21 | - assert_equal 'Sets of thresholds to interpret metrics', MezuroPlugin::ConfigurationContent.description | |
26 | + assert_equal 'Set of metric configurations to interpret a Kalibro project', MezuroPlugin::ConfigurationContent.description | |
22 | 27 | end |
23 | 28 | |
24 | 29 | should 'have an html view' do |
... | ... | @@ -26,52 +31,55 @@ class ConfigurationContentTest < ActiveSupport::TestCase |
26 | 31 | end |
27 | 32 | |
28 | 33 | should 'not save a configuration with an existing cofiguration name in kalibro' do |
29 | - Kalibro::Configuration.expects(:all_names).returns([@content.name.upcase]) | |
30 | - @content.send :validate_kalibro_configuration_name | |
34 | + Kalibro::Configuration.expects(:all).returns([@configuration]) | |
35 | + @content.send :validate_configuration_name | |
31 | 36 | assert_equal "Configuration name already exists in Kalibro", @content.errors.on_base |
32 | 37 | end |
33 | 38 | |
34 | 39 | should 'get configuration from service' do |
35 | - Kalibro::Configuration.expects(:find_by_name).with(@content.name).returns(@configuration) | |
36 | - assert_equal @configuration, @content.kalibro_configuration | |
40 | + Kalibro::Configuration.expects(:find).with(@content.configuration_id).returns(@configuration) | |
41 | + assert_equal @configuration, @content.configuration | |
37 | 42 | end |
38 | 43 | |
39 | 44 | should 'send configuration to service after saving' do |
40 | - @content.expects :send_kalibro_configuration_to_service | |
45 | + @content.expects :send_configuration_to_service | |
41 | 46 | @content.stubs(:solr_save) |
42 | 47 | @content.run_callbacks :after_save |
43 | 48 | end |
44 | 49 | |
45 | 50 | should 'create new configuration' do |
46 | - Kalibro::Configuration.expects(:create).with(:name => @content.name, :description => @content.description) | |
47 | - Kalibro::Configuration.expects(:find_by_name).with(@content.name) | |
48 | - @content.send :send_kalibro_configuration_to_service | |
51 | + Kalibro::Configuration.expects(:create).with(:name => @created_content.name, :description => @created_content.description, :id => nil).returns(@configuration) | |
52 | + @created_content.send :send_configuration_to_service | |
53 | + assert_equal @configuration.id, @created_content.configuration_id | |
49 | 54 | end |
50 | - | |
55 | + | |
56 | +=begin | |
51 | 57 | should 'clone configuration' do |
52 | 58 | @content.configuration_to_clone_name = 'clone name' |
53 | - Kalibro::Configuration.expects(:create).with(:name => @content.name, :description => @content.description, :metric_configuration => @configuration.metric_configurations_hash) | |
54 | - Kalibro::Configuration.expects(:find_by_name).with(@content.name).returns(nil) | |
55 | - Kalibro::Configuration.expects(:find_by_name).with('clone name').returns(@configuration) | |
56 | - @content.send :send_kalibro_configuration_to_service | |
59 | + Kalibro::Configuration.expects(:create).with(:name => @content.configuration_id, :description => @content.description, :metric_configuration => @configuration.metric_configurations_hash) | |
60 | + Kalibro::Configuration.expects(:find).with(@content.configuration_id).returns(nil) | |
61 | + Kalibro::Configuration.expects(:find).with('clone name').returns(@configuration) | |
62 | + @content.send :send_configuration_to_service | |
57 | 63 | end |
64 | +=end | |
58 | 65 | |
59 | 66 | should 'edit configuration' do |
60 | - Kalibro::Configuration.expects(:find_by_name).with(@content.name).returns(@configuration) | |
61 | - @configuration.expects(:update_attributes).with(:description => @content.description) | |
62 | - @content.send :send_kalibro_configuration_to_service | |
67 | + Kalibro::Configuration.expects(:new).with(@configuration_hash).returns(@configuration) | |
68 | + @configuration.expects(:save).returns(true) | |
69 | + @content.send :send_configuration_to_service | |
70 | + assert_equal @configuration.id, @content.configuration_id | |
63 | 71 | end |
64 | 72 | |
65 | 73 | should 'send correct configuration to service but comunication fails' do |
66 | - Kalibro::Configuration.expects(:find_by_name).with(@content.name).returns(@configuration) | |
67 | - @configuration.expects(:save).returns(false) | |
68 | - @content.send :send_kalibro_configuration_to_service | |
74 | + Kalibro::Configuration.expects(:new).with(@configuration_hash).returns(@created_configuration) | |
75 | + @created_configuration.expects(:save).returns(false) | |
76 | + @content.send :send_configuration_to_service | |
69 | 77 | end |
70 | 78 | |
71 | 79 | should 'remove configuration from service' do |
72 | - Kalibro::Configuration.expects(:find_by_name).with(@content.name).returns(@configuration) | |
80 | + Kalibro::Configuration.expects(:find).with(@content.configuration_id).returns(@configuration) | |
73 | 81 | @configuration.expects(:destroy) |
74 | - @content.send :remove_kalibro_configuration_from_service | |
82 | + @content.send :remove_configuration_from_service | |
75 | 83 | end |
76 | 84 | |
77 | 85 | end | ... | ... |
plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | |
3 | 3 | <% |
4 | 4 | kalibro_configuration = @article.title.nil? ? nil : @article.kalibro_configuration |
5 | - kalibro_configuration_names = @article.kalibro_configuration_names | |
5 | + kalibro_configuration_names = @article.configuration_names_and_ids.values | |
6 | 6 | %> |
7 | 7 | |
8 | 8 | <%= error_messages_for 'kalibro_configuration' %> |
... | ... | @@ -10,19 +10,23 @@ |
10 | 10 | <%= hidden_field_tag 'kalibro_configuration[profile_id]', profile.id %> |
11 | 11 | <%= hidden_field_tag 'id', @article.id %> |
12 | 12 | |
13 | - | |
14 | -<% selected = (kalibro_configuration.nil? ? "None" : @article.configuration_to_clone_name) %> | |
13 | +<% # selected = (kalibro_configuration.nil? ? "None" : @article.configuration_to_clone_name) | |
14 | + %> | |
15 | 15 | |
16 | 16 | <%= required_fields_message %> |
17 | 17 | |
18 | -<%= required labelled_form_field _('Clone Configuration'), | |
18 | +<% | |
19 | +=begin | |
20 | +required labelled_form_field _('Clone Configuration'), | |
19 | 21 | if !kalibro_configuration.nil? && !@article.id.nil? |
20 | 22 | f.select(:configuration_to_clone_name, kalibro_configuration_names, {:selected => selected}, :disabled => 'true') |
21 | 23 | else |
22 | 24 | f.select(:configuration_to_clone_name, kalibro_configuration_names, {:selected => selected}) |
23 | -end %> | |
25 | +end | |
26 | +=end | |
27 | +%> | |
24 | 28 | <br/> |
25 | 29 | |
26 | -<%= required f.text_field(:name, :disabled => !(kalibro_configuration.nil? || @article.id.nil?)) %> | |
30 | +<%= required f.text_field(:name) %> | |
27 | 31 | |
28 | 32 | <%= f.text_field :description %><br/> | ... | ... |
plugins/mezuro/views/content_viewer/show_configuration.rhtml
1 | 1 | <% @configuration_content = @page |
2 | 2 | @kalibro_configuration = @page.kalibro_configuration %> |
3 | 3 | <% owner = (not user.nil?) && user.id == @profile.id %> |
4 | - | |
5 | 4 | <% unless @page.errors[:base].nil? %> |
6 | - <% if @page.errors[:base] =~ /There is no configuration named/ %> | |
5 | + <% if @page.errors[:base] =~ /Kalibro::Errors::RecordNotFound/ %> | |
7 | 6 | <h3>Warning:</h3> |
8 | 7 | <p>This Configuration doesn't exist on the Web Service.</p> |
9 | 8 | <% if owner %> |
... | ... | @@ -39,27 +38,27 @@ |
39 | 38 | <td><h5>Metric Name</h5></td> |
40 | 39 | <td><h5>Collector Name</h5></td> |
41 | 40 | <td><h5>Metric Code</h5></td> |
41 | + <td><h5>Weight</h5></td> | |
42 | + <td><h5>Aggregation Form</h5></td> | |
42 | 43 | <td/> |
43 | 44 | </tr> |
44 | - <% @kalibro_configuration.metric_configurations.each do |metric_configuration| %> | |
45 | + <% @configuration_content.metric_configurations.each do |metric_configuration| %> | |
45 | 46 | <tr class="metric"> |
46 | - <% if metric_configuration.metric.instance_of? Kalibro::NativeMetric %> | |
47 | 47 | <td><%= link_to metric_configuration.metric.name, :controller => "mezuro_plugin_metric_configuration", :action => "edit_metric_configuration", |
48 | - :metric_name => metric_configuration.metric.name, :id => @configuration_content.id, | |
48 | + :metric_configuration_id => metric_configuration.id, :id => @configuration_content.id, | |
49 | 49 | :profile => @page.profile.identifier %></td> |
50 | + <% if metric_configuration.metric.compound %> | |
50 | 51 | <td> |
51 | - <%= metric_configuration.metric.origin %> | |
52 | + Compound Metric | |
52 | 53 | </td> |
53 | - <td><%= metric_configuration.code %></td> | |
54 | 54 | <% else %> |
55 | - <td><%= link_to metric_configuration.metric.name, :controller => "mezuro_plugin_metric_configuration", | |
56 | - :action => "edit_compound_metric_configuration", :metric_name => metric_configuration.metric.name, | |
57 | - :id => @configuration_content.id, :profile => @page.profile.identifier %></td> | |
58 | 55 | <td> |
59 | - Compound Metric | |
56 | + <%= metric_configuration.metric.origin %> | |
60 | 57 | </td> |
61 | - <td><%= metric_configuration.code %></td> | |
62 | 58 | <% end %> |
59 | + <td><%= metric_configuration.code %></td> | |
60 | + <td><%= metric_configuration.weight %></td> | |
61 | + <td><%= metric_configuration.aggreafation_form %></td> | |
63 | 62 | <% if owner %> |
64 | 63 | <td><%= link_to "Remove", :controller => "mezuro_plugin_metric_configuration", :action => "remove_metric_configuration", |
65 | 64 | :metric_name => metric_configuration.metric.name, :id => @configuration_content.id, | ... | ... |