From 624abf3454d67c41734c8797670bfe672a914c35 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 21 Oct 2013 17:57:52 -0300 Subject: [PATCH] stoa_plugin: add tags to person api --- plugins/stoa/lib/stoa_plugin/person_api.rb | 7 +++++++ plugins/stoa/lib/stoa_plugin/person_fields.rb | 3 ++- plugins/stoa/test/unit/person_api_test.rb | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 plugins/stoa/test/unit/person_api_test.rb diff --git a/plugins/stoa/lib/stoa_plugin/person_api.rb b/plugins/stoa/lib/stoa_plugin/person_api.rb index 65760a3..bb0186f 100644 --- a/plugins/stoa/lib/stoa_plugin/person_api.rb +++ b/plugins/stoa/lib/stoa_plugin/person_api.rb @@ -22,4 +22,11 @@ class StoaPlugin::PersonApi < Noosfero::FieldsDecorator def image_base64 Base64.encode64(image.current_data) if image && image.current_data end + + def tags + articles.tag_counts({:order => 'tags.count desc', :limit => 10}).inject({}) do |memo,tag| + memo[tag.name] = tag.count + memo + end + end end diff --git a/plugins/stoa/lib/stoa_plugin/person_fields.rb b/plugins/stoa/lib/stoa_plugin/person_fields.rb index 7e19e8e..b25c8af 100644 --- a/plugins/stoa/lib/stoa_plugin/person_fields.rb +++ b/plugins/stoa/lib/stoa_plugin/person_fields.rb @@ -1,10 +1,11 @@ module StoaPlugin::PersonFields HEAVY = %w[image_base64] FILTER = %w[image] + EXTRA = %w[tags] ESSENTIAL = %w[username email nusp] AVERAGE = ESSENTIAL + %w[name first_name surname address homepage] - FULL = (AVERAGE + Person.fields + HEAVY - FILTER).uniq + FULL = (AVERAGE + Person.fields + HEAVY + EXTRA - FILTER).uniq COMPLETE = FULL - HEAVY FIELDS = { diff --git a/plugins/stoa/test/unit/person_api_test.rb b/plugins/stoa/test/unit/person_api_test.rb new file mode 100644 index 0000000..4879c55 --- /dev/null +++ b/plugins/stoa/test/unit/person_api_test.rb @@ -0,0 +1,103 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class StoaPlugin::PersonApiTest < ActiveSupport::TestCase + + def setup + @person = create_user('sample-user').person + end + + attr_accessor :person + + should 'provide nusp' do + person.usp_id = '99999999' + api = StoaPlugin::PersonApi.new(person) + assert_equal person.usp_id, api.nusp + end + + should 'provide username' do + api = StoaPlugin::PersonApi.new(person) + assert_equal person.user.login, api.username + end + + should 'provide first_name' do + person.name = "Jean-Luc Picard" + api = StoaPlugin::PersonApi.new(person) + assert_equal 'Jean-Luc', api.first_name + end + + should 'provide surname' do + person.name = "Jean-Luc Picard" + api = StoaPlugin::PersonApi.new(person) + assert_equal 'Picard', api.surname + end + + should 'provide homepage' do + api = StoaPlugin::PersonApi.new(person, self) + homepage = 'picard.me' + self.stubs(:url_for).with(person.url).returns(homepage) + assert_equal homepage, api.homepage + end + + should 'provide image on base64' do + person.image_builder = {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} + person.save! + api = StoaPlugin::PersonApi.new(person) + assert_equal Base64.encode64(person.image.current_data), api.image_base64 + end + + should 'not crash on image_base64 if profile has no image' do + api = StoaPlugin::PersonApi.new(person) + assert_equal nil, api.image_base64 + end + + should 'provide tags' do + create_article_with_tags(person.id, 'free_software, noosfero, linux') + create_article_with_tags(person.id, 'free_software, linux') + create_article_with_tags(person.id, 'free_software') + + api = StoaPlugin::PersonApi.new(person) + assert_equal person.article_tags, api.tags + end + + should 'provide tags limited by 10 most relevant' do + 13.times {create_article_with_tags(person.id, 'a')} + 12.times {create_article_with_tags(person.id, 'b')} + 11.times {create_article_with_tags(person.id, 'c')} + 10.times {create_article_with_tags(person.id, 'd')} + 9.times {create_article_with_tags(person.id, 'e')} + 8.times {create_article_with_tags(person.id, 'f')} + 7.times {create_article_with_tags(person.id, 'g')} + 6.times {create_article_with_tags(person.id, 'h')} + 5.times {create_article_with_tags(person.id, 'i')} + 4.times {create_article_with_tags(person.id, 'j')} + 3.times {create_article_with_tags(person.id, 'l')} + 2.times {create_article_with_tags(person.id, 'm')} + 1.times {create_article_with_tags(person.id, 'n')} + + api = StoaPlugin::PersonApi.new(person) + tags = api.tags + assert_equal 10, tags.size + assert tags['a'] + assert tags['b'] + assert tags['c'] + assert tags['d'] + assert tags['e'] + assert tags['f'] + assert tags['g'] + assert tags['h'] + assert tags['i'] + assert tags['j'] + assert_nil tags['l'] + assert_nil tags['m'] + assert_nil tags['n'] + end + + private + + def create_article_with_tags(profile_id, tags = '') + article = fast_create(Article, :profile_id => profile_id) + article.tag_list = tags + article.save! + article + end +end -- libgit2 0.21.2