From 022405f5d688673bb6aa923047289aabc5b6b220 Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Thu, 26 Mar 2015 11:26:39 -0300 Subject: [PATCH] Refactoring to remove the extended classes --- controllers/colab_integration_plugin_controller.rb | 4 +++- lib/article_data_export.rb | 20 ++++++++++++++++++++ lib/community_data_export.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/enterprise_data_export.rb | 8 ++++++++ lib/ext/article.rb | 20 -------------------- lib/ext/community.rb | 51 --------------------------------------------------- lib/ext/person.rb | 60 ------------------------------------------------------------ lib/ext/profile.rb | 41 ----------------------------------------- lib/person_data_export.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/profile_data_export.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/functional/colab_integration_plugin_controller_test.rb | 20 ++++++++++++-------- 11 files changed, 206 insertions(+), 181 deletions(-) create mode 100644 lib/article_data_export.rb create mode 100644 lib/community_data_export.rb create mode 100644 lib/enterprise_data_export.rb delete mode 100644 lib/ext/article.rb delete mode 100644 lib/ext/community.rb delete mode 100644 lib/ext/person.rb delete mode 100644 lib/ext/profile.rb create mode 100644 lib/person_data_export.rb create mode 100644 lib/profile_data_export.rb diff --git a/controllers/colab_integration_plugin_controller.rb b/controllers/colab_integration_plugin_controller.rb index 88812bb..cf1137b 100644 --- a/controllers/colab_integration_plugin_controller.rb +++ b/controllers/colab_integration_plugin_controller.rb @@ -1,4 +1,6 @@ class ColabIntegrationPluginController < ApplicationController + include ProfileDataExport + def index timestamp = get_timestamp params[:timestamp] @@ -15,7 +17,7 @@ class ColabIntegrationPluginController < ApplicationController } profiles.each do |profile| - data["profiles"] << profile.attr_to_hash + data["profiles"] << attr_to_hash(profile) end render json: data.to_json diff --git a/lib/article_data_export.rb b/lib/article_data_export.rb new file mode 100644 index 0000000..2ff08d4 --- /dev/null +++ b/lib/article_data_export.rb @@ -0,0 +1,20 @@ +module ArticleDataExport + + def article_attr_to_hash article + { + "type" => article.type, + "data" => { + "id" => article.id, + "parent_id" => article.parent_id, + "title" => article.title, + "body" => article.body, + "path" => article.path, + "slug" => article.slug, + "published" => article.published, + "show_to_followers" => article.show_to_followers, + "author_id" => article.author_id + } + } + end + +end diff --git a/lib/community_data_export.rb b/lib/community_data_export.rb new file mode 100644 index 0000000..1844982 --- /dev/null +++ b/lib/community_data_export.rb @@ -0,0 +1,50 @@ +module CommunityDataExport + + def community_attr_to_hash community + attrs = {} + + attrs["members-count"] = community.members.count + attrs["members"] = [] + + community.members.each do |member| + attrs_members = { + "is_admin" => community.admins.include?(member), + "id" => member.id, + "identifier" => member.identifier, + "name" => member.name + } + attrs['members'] << attrs_members + end + + if community.respond_to?("software?") && community.software? + attrs['software_data'] = { + "public_software" => community.software_info.public_software, + "acronym" => community.software_info.acronym, + "finality" => community.software_info.finality, + "repository_link" => community.software_info.repository_link, + "license_info" => { + "id" => community.software_info.license_info.id, + "version" => community.software_info.license_info.version, + "link" => community.software_info.license_info.link, + }, + "categories" => [] + } + + community.categories.each do |category| + if Category.last.parent.name == "Software" + category_info = { + "id" => category.id, + "name" => category.name, + "slug" => category.slug, + "path" => category.path + } + + attrs['software_data']["categories"] << category_info + end + end + end + + attrs + end + +end diff --git a/lib/enterprise_data_export.rb b/lib/enterprise_data_export.rb new file mode 100644 index 0000000..8a0b5af --- /dev/null +++ b/lib/enterprise_data_export.rb @@ -0,0 +1,8 @@ +module EnterpriseDataExport + + #TODO - Enterprise need implementation + def enterprise_attr_to_hash enterprise + {} + end + +end diff --git a/lib/ext/article.rb b/lib/ext/article.rb deleted file mode 100644 index ffa2068..0000000 --- a/lib/ext/article.rb +++ /dev/null @@ -1,20 +0,0 @@ -require_dependency "article" - -class Article - def attr_to_hash - { - "type" => self.type, - "data" => { - "id" => self.id, - "parent_id" => self.parent_id, - "title" => self.title, - "body" => self.body, - "path" => self.path, - "slug" => self.slug, - "published" => self.published, - "show_to_followers" => self.show_to_followers, - "author_id" => self.author_id - } - } - end -end diff --git a/lib/ext/community.rb b/lib/ext/community.rb deleted file mode 100644 index b6e3392..0000000 --- a/lib/ext/community.rb +++ /dev/null @@ -1,51 +0,0 @@ -require_dependency "community" - -class Community - - def attr_to_hash - attrs = super - - attrs["members-count"] = self.members.count - attrs["members"] = [] - - self.members.each do |member| - attrs_members = { - "is_admin" => self.admins.include?(member), - "id" => member.id, - "identifier" => member.identifier, - "name" => member.name - } - attrs['members'] << attrs_members - end - - if self.respond_to?("software?") && self.software? - attrs['software_data'] = { - "public_software" => self.software_info.public_software, - "acronym" => self.software_info.acronym, - "finality" => self.software_info.finality, - "repository_link" => self.software_info.repository_link, - "license_info" => { - "id" => self.software_info.license_info.id, - "version" => self.software_info.license_info.version, - "link" => self.software_info.license_info.link, - }, - "categories" => [] - } - - self.categories.each do |category| - if Category.last.parent.name == "Software" - category_info = { - "id" => category.id, - "name" => category.name, - "slug" => category.slug, - "path" => category.path - } - - attrs['software_data']["categories"] << category_info - end - end - end - - attrs - end -end diff --git a/lib/ext/person.rb b/lib/ext/person.rb deleted file mode 100644 index 8756ad5..0000000 --- a/lib/ext/person.rb +++ /dev/null @@ -1,60 +0,0 @@ -require_dependency 'person' - -class Person - - def attr_to_hash - attrs = super - - attrs = friends_attrs(attrs) - - attrs = commuinities_attrs(attrs) - - attrs = softwares_attrs(attrs) - - attrs - end - - def friends_attrs(attrs) - attrs['friends-count'] = self.friends.count - attrs['friends'] = [] - self.friends.each do |friend| - attrs['friends'] << profile_attrs(friend) - end - attrs - end - - def commuinities_attrs(attrs) - attrs['communities-count'] = self.respond_to?("softwares") ? - self.communities.count - self.softwares.count : - self.communities.count - attrs['communities'] = [] - self.communities.each do |community| - if community.respond_to?("software?") - attrs['communities'] << profile_attrs(community) - end - end - attrs - end - - def softwares_attrs(attrs) - attrs['softwares-count'] = self.respond_to?("softwares") ? self.softwares.count : 0 - attrs['softwares'] = [] - if self.respond_to?("softwares") - self.softwares.each do |software| - attrs['softwares'] << profile_attrs(software) - end - end - attrs - end - - private - - def profile_attrs(profile) - profile = { - "id" => profile.id, - "identifier" => profile.identifier, - "name" => profile.name - } - end - -end diff --git a/lib/ext/profile.rb b/lib/ext/profile.rb deleted file mode 100644 index bd44def..0000000 --- a/lib/ext/profile.rb +++ /dev/null @@ -1,41 +0,0 @@ -require_dependency 'profile' - -class Profile - def attr_to_hash - attrs = { - "type" => self.type.to_s, - "data" => { - "id" => self.id, - "identifier" => self.identifier, - "name" => self.name, - "description" => self.description - } - } - - attrs['articles-count'] = self.articles.count - attrs['articles'] = [] - self.articles.each do |article| - attrs['articles'] << article.attr_to_hash - end - - attrs['activities-count'] = self.activities.count - attrs['activities'] = activities_attrs - - attrs - end - - def activities_attrs - attrs = [] - - ids = self.activities.collect { |activity| activity.id } - - ActionTracker::Record.find(ids).collect { |activity| - attrs << { - 'verb' => activity.verb, - 'params' => activity.params - } - } - - attrs - end -end diff --git a/lib/person_data_export.rb b/lib/person_data_export.rb new file mode 100644 index 0000000..d39f3c3 --- /dev/null +++ b/lib/person_data_export.rb @@ -0,0 +1,58 @@ +module PersonDataExport + + def person_attr_to_hash person + attrs = {} + + attrs = person_friends_attrs(attrs, person) + + attrs = person_commuinities_attrs(attrs, person) + + attrs = person_softwares_attrs(attrs, person) + + attrs + end + + def person_friends_attrs(attrs, person) + attrs['friends-count'] = person.friends.count + attrs['friends'] = [] + person.friends.each do |friend| + attrs['friends'] << profile_attrs(friend) + end + attrs + end + + def person_commuinities_attrs(attrs, person) + attrs['communities-count'] = person.respond_to?("softwares") ? + person.communities.count - person.softwares.count : + person.communities.count + attrs['communities'] = [] + person.communities.each do |community| + if community.respond_to?("software?") + attrs['communities'] << profile_attrs(community) + end + end + attrs + end + + def person_softwares_attrs(attrs, person) + attrs['softwares-count'] = person.respond_to?("softwares") ? person.softwares.count : 0 + attrs['softwares'] = [] + if person.respond_to?("softwares") + person.softwares.each do |software| + attrs['softwares'] << profile_attrs(software) + end + end + attrs + end + + private + + def profile_attrs(profile) + profile = { + "id" => profile.id, + "identifier" => profile.identifier, + "name" => profile.name + } + end + +end diff --git a/lib/profile_data_export.rb b/lib/profile_data_export.rb new file mode 100644 index 0000000..bc87808 --- /dev/null +++ b/lib/profile_data_export.rb @@ -0,0 +1,55 @@ +module ProfileDataExport + include PersonDataExport + include CommunityDataExport + include ArticleDataExport + include EnterpriseDataExport + + def attr_to_hash profile + attrs = profile_attr_to_hash profile + + attrs = attrs.merge(self.send("#{profile.type.underscore}_attr_to_hash", profile)) + + attrs + end + + private + + def profile_attr_to_hash profile + attrs = { + "type" => profile.type.to_s, + "data" => { + "id" => profile.id, + "identifier" => profile.identifier, + "name" => profile.name, + "description" => profile.description + } + } + + attrs['articles-count'] = profile.articles.count + attrs['articles'] = [] + profile.articles.each do |article| + attrs['articles'] << article_attr_to_hash(article) + end + + attrs['activities-count'] = profile.activities.count + attrs['activities'] = profile_activities_attrs(profile) + + attrs + end + + def profile_activities_attrs profile + attrs = [] + + ids = profile.activities.collect { |activity| activity.id } + + ActionTracker::Record.find(ids).collect { |activity| + attrs << { + 'verb' => activity.verb, + 'params' => activity.params + } + } + + attrs + end + +end diff --git a/test/functional/colab_integration_plugin_controller_test.rb b/test/functional/colab_integration_plugin_controller_test.rb index e07c2d2..e98ea99 100644 --- a/test/functional/colab_integration_plugin_controller_test.rb +++ b/test/functional/colab_integration_plugin_controller_test.rb @@ -5,6 +5,8 @@ class ColabIntegrationPluginController; def rescue_action(e) raise e end; end class ColabIntegrationPluginControllerTest < ActionController::TestCase + include ProfileDataExport + def setup @controller = ColabIntegrationPluginController.new @john = fast_create(Person, :name => "John Snow") @@ -73,13 +75,15 @@ class ColabIntegrationPluginControllerTest < ActionController::TestCase end should "return a hash with activities of the user" do - user = fast_create(Person, :name => "User") - fast_create(Community, :name => "User Community").add_member user - fast_create(Community, :name => "User Community 2").add_member user - - assert_equal 2, user.activities_attrs.count - assert user.activities_attrs.first.has_key?("verb") - assert user.activities_attrs.first.has_key?("params") - assert user.activities_attrs.first["verb"] == "join_community" + person = fast_create(Person, :name => "User") + fast_create(Community, :name => "User Community").add_member person + fast_create(Community, :name => "User Community 2").add_member person + + attrs = attr_to_hash(person) + + assert_equal 2, attrs['activities-count'] + assert attrs['activities'].first.has_key?("verb") + assert attrs['activities'].first.has_key?("params") + assert attrs['activities'].first["verb"] == "join_community" end end -- libgit2 0.21.2