Commit 7bed0f9568b2f84919fd46a2d7099cb429add489
Committed by
Paulo Meireles
1 parent
9462c19e
Exists in
master
and in
29 other branches
[Mezuro] Reading group content.
Showing
6 changed files
with
218 additions
and
2 deletions
Show diff stats
plugins/mezuro/lib/mezuro_plugin.rb
@@ -18,7 +18,8 @@ class MezuroPlugin < Noosfero::Plugin | @@ -18,7 +18,8 @@ class MezuroPlugin < Noosfero::Plugin | ||
18 | if context.profile.is_a?(Community) | 18 | if context.profile.is_a?(Community) |
19 | MezuroPlugin::ProjectContent | 19 | MezuroPlugin::ProjectContent |
20 | else | 20 | else |
21 | - MezuroPlugin::ConfigurationContent | 21 | + [MezuroPlugin::ConfigurationContent, |
22 | + MezuroPlugin::ReadingGroupContent] | ||
22 | end | 23 | end |
23 | end | 24 | end |
24 | 25 | ||
@@ -26,7 +27,8 @@ class MezuroPlugin < Noosfero::Plugin | @@ -26,7 +27,8 @@ class MezuroPlugin < Noosfero::Plugin | ||
26 | if context.profile.is_a?(Community) | 27 | if context.profile.is_a?(Community) |
27 | {:title => _('Mezuro project'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ProjectContent'}, :icon => 'mezuro' } | 28 | {:title => _('Mezuro project'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ProjectContent'}, :icon => 'mezuro' } |
28 | else | 29 | else |
29 | - {:title => _('Mezuro configuration'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ConfigurationContent'}, :icon => 'mezuro' } | 30 | + [{:title => _('Mezuro configuration'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ConfigurationContent'}, :icon => 'mezuro' }, |
31 | + {:title => _('Mezuro Reading Group'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ReadingGroupContent'}, :icon => 'mezuro' }] | ||
30 | end | 32 | end |
31 | end | 33 | end |
32 | 34 |
plugins/mezuro/lib/mezuro_plugin/reading_group_content.rb
0 → 100644
@@ -0,0 +1,83 @@ | @@ -0,0 +1,83 @@ | ||
1 | +class MezuroPlugin::ReadingGroupContent < Article | ||
2 | + include ActionView::Helpers::TagHelper | ||
3 | + | ||
4 | + settings_items :reading_group_id | ||
5 | + | ||
6 | + def self.short_description | ||
7 | + 'Mezuro reading group' | ||
8 | + end | ||
9 | + | ||
10 | + def self.description | ||
11 | + 'Set of thresholds to interpret metric results' | ||
12 | + end | ||
13 | + | ||
14 | + def to_html(options = {}) | ||
15 | + lambda do | ||
16 | + render :file => 'content_viewer/show_reading_group.rhtml' | ||
17 | + end | ||
18 | + end | ||
19 | + | ||
20 | + def reading_group | ||
21 | + begin | ||
22 | + @reading_group ||= Kalibro::ReadingGroup.find(reading_group_id) | ||
23 | + rescue Exception => error | ||
24 | + errors.add_to_base(error.message) | ||
25 | + end | ||
26 | + @reading_group | ||
27 | + end | ||
28 | + | ||
29 | + def readings | ||
30 | + begin | ||
31 | + @readings ||= Kalibro::Reading.readings_of(reading_group_id) | ||
32 | + rescue Exception => error | ||
33 | + errors.add_to_base(error.message) | ||
34 | + @readings = [] | ||
35 | + end | ||
36 | + @readings | ||
37 | + end | ||
38 | + | ||
39 | + def description=(value) | ||
40 | + @description=value | ||
41 | + end | ||
42 | + | ||
43 | + def description | ||
44 | + begin | ||
45 | + @description ||= reading_group.description | ||
46 | + rescue | ||
47 | + @description = "" | ||
48 | + end | ||
49 | + @description | ||
50 | + end | ||
51 | + | ||
52 | + def readings=(value) | ||
53 | + @readings = value.kind_of?(Array) ? value : [value] | ||
54 | + @readings = @readings.map { |element| to_reading(element) } | ||
55 | + end | ||
56 | + | ||
57 | + before_save :send_reading_group_to_service | ||
58 | + after_destroy :destroy_reading_group_from_service | ||
59 | + | ||
60 | + private | ||
61 | + | ||
62 | + def self.to_reading value | ||
63 | + value.kind_of?(Hash) ? Kalibro::Reading.new(value) : value | ||
64 | + end | ||
65 | + | ||
66 | + def send_reading_group_to_service | ||
67 | + created_reading_group = create_kalibro_reading_group | ||
68 | + self.reading_group_id = created_reading_group.id | ||
69 | + end | ||
70 | + | ||
71 | + def create_kalibro_reading_group | ||
72 | + Kalibro::ReadingGroup.create( | ||
73 | + :name => name, | ||
74 | + :description => description, | ||
75 | + :id => self.reading_group_id | ||
76 | + ) | ||
77 | + end | ||
78 | + | ||
79 | + def destroy_reading_group_from_service | ||
80 | + reading_group.destroy unless reading_group.nil? | ||
81 | + end | ||
82 | + | ||
83 | +end |
plugins/mezuro/test/fixtures/reading_group_content_fixtures.rb
0 → 100644
plugins/mezuro/test/unit/mezuro_plugin/reading_group_content_test.rb
0 → 100644
@@ -0,0 +1,51 @@ | @@ -0,0 +1,51 @@ | ||
1 | +require "test_helper" | ||
2 | + | ||
3 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_group_fixtures" | ||
4 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_fixtures" | ||
5 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_group_content_fixtures" | ||
6 | + | ||
7 | +class ReadingGroupContentTest < ActiveSupport::TestCase | ||
8 | + | ||
9 | + def setup | ||
10 | + @reading_group_content = ReadingGroupContentFixtures.reading_group_content | ||
11 | + @reading_group = ReadingGroupFixtures.reading_group | ||
12 | + @reading = ReadingFixtures.reading | ||
13 | + end | ||
14 | + | ||
15 | + should 'provide proper short description' do | ||
16 | + assert_equal 'Mezuro reading group', MezuroPlugin::ReadingGroupContent.short_description | ||
17 | + end | ||
18 | + | ||
19 | + should 'provide proper description' do | ||
20 | + assert_equal 'Set of thresholds to interpret metric results', MezuroPlugin::ReadingGroupContent.description | ||
21 | + end | ||
22 | + | ||
23 | + should 'have an html view' do | ||
24 | + assert_not_nil @reading_group_content.to_html | ||
25 | + end | ||
26 | + | ||
27 | + should 'get reading_group from service' do | ||
28 | + Kalibro::ReadingGroup.expects(:find).with(@reading_group.id).returns(@reading_group) | ||
29 | + assert_equal @reading_group, @reading_group_content.reading_group | ||
30 | + end | ||
31 | + | ||
32 | + should 'add error to base when the reading_group does not exist' do | ||
33 | + Kalibro::ReadingGroup.expects(:find).with(@reading_group.id).raises(Kalibro::Errors::RecordNotFound) | ||
34 | + assert_nil @reading_group_content.errors[:base] | ||
35 | + @reading_group_content.reading_group | ||
36 | + assert_not_nil @reading_group_content.errors[:base] | ||
37 | + end | ||
38 | + | ||
39 | + should 'get readings of the reading_group from service' do | ||
40 | + Kalibro::Reading.expects(:readings_of).with(@reading_group.id).returns([@reading]) | ||
41 | + assert_equal [@reading], @reading_group_content.readings | ||
42 | + end | ||
43 | + | ||
44 | + should 'add error to base when getting the readings of a reading_group that does not exist' do | ||
45 | + Kalibro::Reading.expects(:readings_of).with(@reading_group.id).raises(Kalibro::Errors::RecordNotFound) | ||
46 | + assert_nil @reading_group_content.errors[:base] | ||
47 | + @reading_group_content.readings | ||
48 | + assert_not_nil @reading_group_content.errors[:base] | ||
49 | + end | ||
50 | + | ||
51 | +end |
plugins/mezuro/views/cms/mezuro_plugin/_reading_group_content.html.erb
0 → 100644
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +<h1> <%= _(MezuroPlugin::ReadingGroupContent.short_description) %> </h1> | ||
2 | + | ||
3 | +<% | ||
4 | + reading_group = @article.title.nil? ? nil : @article.reading_group | ||
5 | +%> | ||
6 | + | ||
7 | +<%= error_messages_for 'reading_group_content' %> | ||
8 | + | ||
9 | +<%= hidden_field_tag 'reading_group_content[profile_id]', profile.id %> | ||
10 | +<%= hidden_field_tag 'id', @article.id %> | ||
11 | +<%= hidden_field_tag 'reading_group_id', reading_group.id %> | ||
12 | + | ||
13 | +<%= required_fields_message %> | ||
14 | + | ||
15 | +<%= required f.text_field(:name) %> | ||
16 | + | ||
17 | +<%= f.text_field :description %><br/> | ||
18 | + | ||
19 | +<!-- partial rendered in app/views/cms/edit.rhtml --> | ||
20 | +<!-- After submit, the action cms/new is called --> |
plugins/mezuro/views/content_viewer/show_reading_group.rhtml
0 → 100644
@@ -0,0 +1,51 @@ | @@ -0,0 +1,51 @@ | ||
1 | +<% @reading_group = @page.reading_group %> | ||
2 | +<% unless @page.errors[:base].nil? %> | ||
3 | + <% if @page.errors[:base] == "Kalibro::Errors::RecordNotFound" %> | ||
4 | + <h3>Warning:</h3> | ||
5 | + <p>This reading group doesn't exist on the Web Service. Do you want to <%= link_to 'delete', :action => 'destroy', :controller => 'cms', :profile => @page.profile.identifier, :id => @page.id %> or <%= link_to 'save it again', :action => 'edit', :controller => 'cms', :profile => @page.profile.identifier, :id => @page.id %>?</p> | ||
6 | + <% else %> | ||
7 | + <%= @page.errors[:base] %> | ||
8 | + <% end %> | ||
9 | +<% else %> | ||
10 | + | ||
11 | + <table> | ||
12 | + <tr> | ||
13 | + <td><%= _('Name') %></td> | ||
14 | + <td><%= @reading_group.name %></td> | ||
15 | + </tr> | ||
16 | + <tr> | ||
17 | + <td><%= _('Description') %></td> | ||
18 | + <td><%= @reading_group.description %></td> | ||
19 | + </tr> | ||
20 | + </table> | ||
21 | + <br/> | ||
22 | + <h5><%= _('Readings') %></h5> | ||
23 | + <table> | ||
24 | + <% @page.readings.each do |reading| %> | ||
25 | + <tr> | ||
26 | + <td><%= link_to reading.name, :controller => "mezuro_plugin_reading", | ||
27 | + :profile => @page.profile.identifier, | ||
28 | + :action => "show", | ||
29 | + :id => @page.id, | ||
30 | + :reading_id => reading.id %></td> | ||
31 | + <td ><%= link_to _('Edit'), {:controller => "mezuro_plugin_reading", | ||
32 | + :profile => @page.profile.identifier, | ||
33 | + :action => "edit", | ||
34 | + :id => @page.id, | ||
35 | + :reading_id => reading.id}, :class=>"button with-text icon-edit" %></td> | ||
36 | + <td ><%= link_to _('Remove'), {:controller => "mezuro_plugin_reading", | ||
37 | + :profile => @page.profile.identifier, | ||
38 | + :action => "destroy", | ||
39 | + :id => @page.id, | ||
40 | + :reading_id => reading.id}, :class=>"button with-text icon-delete" %></td> | ||
41 | + </tr> | ||
42 | + <% end %> | ||
43 | + </table> | ||
44 | + | ||
45 | + <br/> | ||
46 | + <%= link_to "#{image_tag ('/plugins/mezuro/images/plus.png')}Add Reading", :controller => "mezuro_plugin_reading", | ||
47 | + :profile => @page.profile.identifier, | ||
48 | + :action => "new", | ||
49 | + :id => @page.id %><br/> | ||
50 | + | ||
51 | +<% end %> |