diff --git a/plugins/mezuro/lib/mezuro_plugin.rb b/plugins/mezuro/lib/mezuro_plugin.rb index bff99dc..e985759 100644 --- a/plugins/mezuro/lib/mezuro_plugin.rb +++ b/plugins/mezuro/lib/mezuro_plugin.rb @@ -18,7 +18,8 @@ class MezuroPlugin < Noosfero::Plugin if context.profile.is_a?(Community) MezuroPlugin::ProjectContent else - MezuroPlugin::ConfigurationContent + [MezuroPlugin::ConfigurationContent, + MezuroPlugin::ReadingGroupContent] end end @@ -26,7 +27,8 @@ class MezuroPlugin < Noosfero::Plugin if context.profile.is_a?(Community) {:title => _('Mezuro project'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ProjectContent'}, :icon => 'mezuro' } else - {:title => _('Mezuro configuration'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ConfigurationContent'}, :icon => 'mezuro' } + [{:title => _('Mezuro configuration'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ConfigurationContent'}, :icon => 'mezuro' }, + {:title => _('Mezuro Reading Group'), :url => {:controller => 'cms', :action => 'new', :profile => context.profile.identifier, :type => 'MezuroPlugin::ReadingGroupContent'}, :icon => 'mezuro' }] end end diff --git a/plugins/mezuro/lib/mezuro_plugin/reading_group_content.rb b/plugins/mezuro/lib/mezuro_plugin/reading_group_content.rb new file mode 100644 index 0000000..6785b75 --- /dev/null +++ b/plugins/mezuro/lib/mezuro_plugin/reading_group_content.rb @@ -0,0 +1,83 @@ +class MezuroPlugin::ReadingGroupContent < Article + include ActionView::Helpers::TagHelper + + settings_items :reading_group_id + + def self.short_description + 'Mezuro reading group' + end + + def self.description + 'Set of thresholds to interpret metric results' + end + + def to_html(options = {}) + lambda do + render :file => 'content_viewer/show_reading_group.rhtml' + end + end + + def reading_group + begin + @reading_group ||= Kalibro::ReadingGroup.find(reading_group_id) + rescue Exception => error + errors.add_to_base(error.message) + end + @reading_group + end + + def readings + begin + @readings ||= Kalibro::Reading.readings_of(reading_group_id) + rescue Exception => error + errors.add_to_base(error.message) + @readings = [] + end + @readings + end + + def description=(value) + @description=value + end + + def description + begin + @description ||= reading_group.description + rescue + @description = "" + end + @description + end + + def readings=(value) + @readings = value.kind_of?(Array) ? value : [value] + @readings = @readings.map { |element| to_reading(element) } + end + + before_save :send_reading_group_to_service + after_destroy :destroy_reading_group_from_service + + private + + def self.to_reading value + value.kind_of?(Hash) ? Kalibro::Reading.new(value) : value + end + + def send_reading_group_to_service + created_reading_group = create_kalibro_reading_group + self.reading_group_id = created_reading_group.id + end + + def create_kalibro_reading_group + Kalibro::ReadingGroup.create( + :name => name, + :description => description, + :id => self.reading_group_id + ) + end + + def destroy_reading_group_from_service + reading_group.destroy unless reading_group.nil? + end + +end diff --git a/plugins/mezuro/test/fixtures/reading_group_content_fixtures.rb b/plugins/mezuro/test/fixtures/reading_group_content_fixtures.rb new file mode 100644 index 0000000..2c306fe --- /dev/null +++ b/plugins/mezuro/test/fixtures/reading_group_content_fixtures.rb @@ -0,0 +1,9 @@ +class ReadingGroupContentFixtures + + def self.reading_group_content + content = MezuroPlugin::ReadingGroupContent.new + content.reading_group_id = 42 + content + end + +end diff --git a/plugins/mezuro/test/unit/mezuro_plugin/reading_group_content_test.rb b/plugins/mezuro/test/unit/mezuro_plugin/reading_group_content_test.rb new file mode 100644 index 0000000..8305807 --- /dev/null +++ b/plugins/mezuro/test/unit/mezuro_plugin/reading_group_content_test.rb @@ -0,0 +1,51 @@ +require "test_helper" + +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_group_fixtures" +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_fixtures" +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_group_content_fixtures" + +class ReadingGroupContentTest < ActiveSupport::TestCase + + def setup + @reading_group_content = ReadingGroupContentFixtures.reading_group_content + @reading_group = ReadingGroupFixtures.reading_group + @reading = ReadingFixtures.reading + end + + should 'provide proper short description' do + assert_equal 'Mezuro reading group', MezuroPlugin::ReadingGroupContent.short_description + end + + should 'provide proper description' do + assert_equal 'Set of thresholds to interpret metric results', MezuroPlugin::ReadingGroupContent.description + end + + should 'have an html view' do + assert_not_nil @reading_group_content.to_html + end + + should 'get reading_group from service' do + Kalibro::ReadingGroup.expects(:find).with(@reading_group.id).returns(@reading_group) + assert_equal @reading_group, @reading_group_content.reading_group + end + + should 'add error to base when the reading_group does not exist' do + Kalibro::ReadingGroup.expects(:find).with(@reading_group.id).raises(Kalibro::Errors::RecordNotFound) + assert_nil @reading_group_content.errors[:base] + @reading_group_content.reading_group + assert_not_nil @reading_group_content.errors[:base] + end + + should 'get readings of the reading_group from service' do + Kalibro::Reading.expects(:readings_of).with(@reading_group.id).returns([@reading]) + assert_equal [@reading], @reading_group_content.readings + end + + should 'add error to base when getting the readings of a reading_group that does not exist' do + Kalibro::Reading.expects(:readings_of).with(@reading_group.id).raises(Kalibro::Errors::RecordNotFound) + assert_nil @reading_group_content.errors[:base] + @reading_group_content.readings + assert_not_nil @reading_group_content.errors[:base] + end + +end diff --git a/plugins/mezuro/views/cms/mezuro_plugin/_reading_group_content.html.erb b/plugins/mezuro/views/cms/mezuro_plugin/_reading_group_content.html.erb new file mode 100644 index 0000000..427b03f --- /dev/null +++ b/plugins/mezuro/views/cms/mezuro_plugin/_reading_group_content.html.erb @@ -0,0 +1,20 @@ +
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 %>?
+ <% else %> + <%= @page.errors[:base] %> + <% end %> +<% else %> + +<%= _('Name') %> | +<%= @reading_group.name %> | +
<%= _('Description') %> | +<%= @reading_group.description %> | +
<%= link_to reading.name, :controller => "mezuro_plugin_reading", + :profile => @page.profile.identifier, + :action => "show", + :id => @page.id, + :reading_id => reading.id %> | +<%= link_to _('Edit'), {:controller => "mezuro_plugin_reading", + :profile => @page.profile.identifier, + :action => "edit", + :id => @page.id, + :reading_id => reading.id}, :class=>"button with-text icon-edit" %> | +<%= link_to _('Remove'), {:controller => "mezuro_plugin_reading", + :profile => @page.profile.identifier, + :action => "destroy", + :id => @page.id, + :reading_id => reading.id}, :class=>"button with-text icon-delete" %> | +