Commit 022405f5d688673bb6aa923047289aabc5b6b220
1 parent
7f6cc03b
Exists in
master
Refactoring to remove the extended classes
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr@gmail.com>
Showing
11 changed files
with
206 additions
and
181 deletions
Show diff stats
controllers/colab_integration_plugin_controller.rb
1 | 1 | class ColabIntegrationPluginController < ApplicationController |
2 | + include ProfileDataExport | |
3 | + | |
2 | 4 | def index |
3 | 5 | timestamp = get_timestamp params[:timestamp] |
4 | 6 | |
... | ... | @@ -15,7 +17,7 @@ class ColabIntegrationPluginController < ApplicationController |
15 | 17 | } |
16 | 18 | |
17 | 19 | profiles.each do |profile| |
18 | - data["profiles"] << profile.attr_to_hash | |
20 | + data["profiles"] << attr_to_hash(profile) | |
19 | 21 | end |
20 | 22 | |
21 | 23 | render json: data.to_json | ... | ... |
... | ... | @@ -0,0 +1,20 @@ |
1 | +module ArticleDataExport | |
2 | + | |
3 | + def article_attr_to_hash article | |
4 | + { | |
5 | + "type" => article.type, | |
6 | + "data" => { | |
7 | + "id" => article.id, | |
8 | + "parent_id" => article.parent_id, | |
9 | + "title" => article.title, | |
10 | + "body" => article.body, | |
11 | + "path" => article.path, | |
12 | + "slug" => article.slug, | |
13 | + "published" => article.published, | |
14 | + "show_to_followers" => article.show_to_followers, | |
15 | + "author_id" => article.author_id | |
16 | + } | |
17 | + } | |
18 | + end | |
19 | + | |
20 | +end | ... | ... |
... | ... | @@ -0,0 +1,50 @@ |
1 | +module CommunityDataExport | |
2 | + | |
3 | + def community_attr_to_hash community | |
4 | + attrs = {} | |
5 | + | |
6 | + attrs["members-count"] = community.members.count | |
7 | + attrs["members"] = [] | |
8 | + | |
9 | + community.members.each do |member| | |
10 | + attrs_members = { | |
11 | + "is_admin" => community.admins.include?(member), | |
12 | + "id" => member.id, | |
13 | + "identifier" => member.identifier, | |
14 | + "name" => member.name | |
15 | + } | |
16 | + attrs['members'] << attrs_members | |
17 | + end | |
18 | + | |
19 | + if community.respond_to?("software?") && community.software? | |
20 | + attrs['software_data'] = { | |
21 | + "public_software" => community.software_info.public_software, | |
22 | + "acronym" => community.software_info.acronym, | |
23 | + "finality" => community.software_info.finality, | |
24 | + "repository_link" => community.software_info.repository_link, | |
25 | + "license_info" => { | |
26 | + "id" => community.software_info.license_info.id, | |
27 | + "version" => community.software_info.license_info.version, | |
28 | + "link" => community.software_info.license_info.link, | |
29 | + }, | |
30 | + "categories" => [] | |
31 | + } | |
32 | + | |
33 | + community.categories.each do |category| | |
34 | + if Category.last.parent.name == "Software" | |
35 | + category_info = { | |
36 | + "id" => category.id, | |
37 | + "name" => category.name, | |
38 | + "slug" => category.slug, | |
39 | + "path" => category.path | |
40 | + } | |
41 | + | |
42 | + attrs['software_data']["categories"] << category_info | |
43 | + end | |
44 | + end | |
45 | + end | |
46 | + | |
47 | + attrs | |
48 | + end | |
49 | + | |
50 | +end | ... | ... |
lib/ext/article.rb
... | ... | @@ -1,20 +0,0 @@ |
1 | -require_dependency "article" | |
2 | - | |
3 | -class Article | |
4 | - def attr_to_hash | |
5 | - { | |
6 | - "type" => self.type, | |
7 | - "data" => { | |
8 | - "id" => self.id, | |
9 | - "parent_id" => self.parent_id, | |
10 | - "title" => self.title, | |
11 | - "body" => self.body, | |
12 | - "path" => self.path, | |
13 | - "slug" => self.slug, | |
14 | - "published" => self.published, | |
15 | - "show_to_followers" => self.show_to_followers, | |
16 | - "author_id" => self.author_id | |
17 | - } | |
18 | - } | |
19 | - end | |
20 | -end |
lib/ext/community.rb
... | ... | @@ -1,51 +0,0 @@ |
1 | -require_dependency "community" | |
2 | - | |
3 | -class Community | |
4 | - | |
5 | - def attr_to_hash | |
6 | - attrs = super | |
7 | - | |
8 | - attrs["members-count"] = self.members.count | |
9 | - attrs["members"] = [] | |
10 | - | |
11 | - self.members.each do |member| | |
12 | - attrs_members = { | |
13 | - "is_admin" => self.admins.include?(member), | |
14 | - "id" => member.id, | |
15 | - "identifier" => member.identifier, | |
16 | - "name" => member.name | |
17 | - } | |
18 | - attrs['members'] << attrs_members | |
19 | - end | |
20 | - | |
21 | - if self.respond_to?("software?") && self.software? | |
22 | - attrs['software_data'] = { | |
23 | - "public_software" => self.software_info.public_software, | |
24 | - "acronym" => self.software_info.acronym, | |
25 | - "finality" => self.software_info.finality, | |
26 | - "repository_link" => self.software_info.repository_link, | |
27 | - "license_info" => { | |
28 | - "id" => self.software_info.license_info.id, | |
29 | - "version" => self.software_info.license_info.version, | |
30 | - "link" => self.software_info.license_info.link, | |
31 | - }, | |
32 | - "categories" => [] | |
33 | - } | |
34 | - | |
35 | - self.categories.each do |category| | |
36 | - if Category.last.parent.name == "Software" | |
37 | - category_info = { | |
38 | - "id" => category.id, | |
39 | - "name" => category.name, | |
40 | - "slug" => category.slug, | |
41 | - "path" => category.path | |
42 | - } | |
43 | - | |
44 | - attrs['software_data']["categories"] << category_info | |
45 | - end | |
46 | - end | |
47 | - end | |
48 | - | |
49 | - attrs | |
50 | - end | |
51 | -end |
lib/ext/person.rb
... | ... | @@ -1,60 +0,0 @@ |
1 | -require_dependency 'person' | |
2 | - | |
3 | -class Person | |
4 | - | |
5 | - def attr_to_hash | |
6 | - attrs = super | |
7 | - | |
8 | - attrs = friends_attrs(attrs) | |
9 | - | |
10 | - attrs = commuinities_attrs(attrs) | |
11 | - | |
12 | - attrs = softwares_attrs(attrs) | |
13 | - | |
14 | - attrs | |
15 | - end | |
16 | - | |
17 | - def friends_attrs(attrs) | |
18 | - attrs['friends-count'] = self.friends.count | |
19 | - attrs['friends'] = [] | |
20 | - self.friends.each do |friend| | |
21 | - attrs['friends'] << profile_attrs(friend) | |
22 | - end | |
23 | - attrs | |
24 | - end | |
25 | - | |
26 | - def commuinities_attrs(attrs) | |
27 | - attrs['communities-count'] = self.respond_to?("softwares") ? | |
28 | - self.communities.count - self.softwares.count : | |
29 | - self.communities.count | |
30 | - attrs['communities'] = [] | |
31 | - self.communities.each do |community| | |
32 | - if community.respond_to?("software?") | |
33 | - attrs['communities'] << profile_attrs(community) | |
34 | - end | |
35 | - end | |
36 | - attrs | |
37 | - end | |
38 | - | |
39 | - def softwares_attrs(attrs) | |
40 | - attrs['softwares-count'] = self.respond_to?("softwares") ? self.softwares.count : 0 | |
41 | - attrs['softwares'] = [] | |
42 | - if self.respond_to?("softwares") | |
43 | - self.softwares.each do |software| | |
44 | - attrs['softwares'] << profile_attrs(software) | |
45 | - end | |
46 | - end | |
47 | - attrs | |
48 | - end | |
49 | - | |
50 | - private | |
51 | - | |
52 | - def profile_attrs(profile) | |
53 | - profile = { | |
54 | - "id" => profile.id, | |
55 | - "identifier" => profile.identifier, | |
56 | - "name" => profile.name | |
57 | - } | |
58 | - end | |
59 | - | |
60 | -end |
lib/ext/profile.rb
... | ... | @@ -1,41 +0,0 @@ |
1 | -require_dependency 'profile' | |
2 | - | |
3 | -class Profile | |
4 | - def attr_to_hash | |
5 | - attrs = { | |
6 | - "type" => self.type.to_s, | |
7 | - "data" => { | |
8 | - "id" => self.id, | |
9 | - "identifier" => self.identifier, | |
10 | - "name" => self.name, | |
11 | - "description" => self.description | |
12 | - } | |
13 | - } | |
14 | - | |
15 | - attrs['articles-count'] = self.articles.count | |
16 | - attrs['articles'] = [] | |
17 | - self.articles.each do |article| | |
18 | - attrs['articles'] << article.attr_to_hash | |
19 | - end | |
20 | - | |
21 | - attrs['activities-count'] = self.activities.count | |
22 | - attrs['activities'] = activities_attrs | |
23 | - | |
24 | - attrs | |
25 | - end | |
26 | - | |
27 | - def activities_attrs | |
28 | - attrs = [] | |
29 | - | |
30 | - ids = self.activities.collect { |activity| activity.id } | |
31 | - | |
32 | - ActionTracker::Record.find(ids).collect { |activity| | |
33 | - attrs << { | |
34 | - 'verb' => activity.verb, | |
35 | - 'params' => activity.params | |
36 | - } | |
37 | - } | |
38 | - | |
39 | - attrs | |
40 | - end | |
41 | -end |
... | ... | @@ -0,0 +1,58 @@ |
1 | +module PersonDataExport | |
2 | + | |
3 | + def person_attr_to_hash person | |
4 | + attrs = {} | |
5 | + | |
6 | + attrs = person_friends_attrs(attrs, person) | |
7 | + | |
8 | + attrs = person_commuinities_attrs(attrs, person) | |
9 | + | |
10 | + attrs = person_softwares_attrs(attrs, person) | |
11 | + | |
12 | + attrs | |
13 | + end | |
14 | + | |
15 | + def person_friends_attrs(attrs, person) | |
16 | + attrs['friends-count'] = person.friends.count | |
17 | + attrs['friends'] = [] | |
18 | + person.friends.each do |friend| | |
19 | + attrs['friends'] << profile_attrs(friend) | |
20 | + end | |
21 | + attrs | |
22 | + end | |
23 | + | |
24 | + def person_commuinities_attrs(attrs, person) | |
25 | + attrs['communities-count'] = person.respond_to?("softwares") ? | |
26 | + person.communities.count - person.softwares.count : | |
27 | + person.communities.count | |
28 | + attrs['communities'] = [] | |
29 | + person.communities.each do |community| | |
30 | + if community.respond_to?("software?") | |
31 | + attrs['communities'] << profile_attrs(community) | |
32 | + end | |
33 | + end | |
34 | + attrs | |
35 | + end | |
36 | + | |
37 | + def person_softwares_attrs(attrs, person) | |
38 | + attrs['softwares-count'] = person.respond_to?("softwares") ? person.softwares.count : 0 | |
39 | + attrs['softwares'] = [] | |
40 | + if person.respond_to?("softwares") | |
41 | + person.softwares.each do |software| | |
42 | + attrs['softwares'] << profile_attrs(software) | |
43 | + end | |
44 | + end | |
45 | + attrs | |
46 | + end | |
47 | + | |
48 | + private | |
49 | + | |
50 | + def profile_attrs(profile) | |
51 | + profile = { | |
52 | + "id" => profile.id, | |
53 | + "identifier" => profile.identifier, | |
54 | + "name" => profile.name | |
55 | + } | |
56 | + end | |
57 | + | |
58 | +end | ... | ... |
... | ... | @@ -0,0 +1,55 @@ |
1 | +module ProfileDataExport | |
2 | + include PersonDataExport | |
3 | + include CommunityDataExport | |
4 | + include ArticleDataExport | |
5 | + include EnterpriseDataExport | |
6 | + | |
7 | + def attr_to_hash profile | |
8 | + attrs = profile_attr_to_hash profile | |
9 | + | |
10 | + attrs = attrs.merge(self.send("#{profile.type.underscore}_attr_to_hash", profile)) | |
11 | + | |
12 | + attrs | |
13 | + end | |
14 | + | |
15 | + private | |
16 | + | |
17 | + def profile_attr_to_hash profile | |
18 | + attrs = { | |
19 | + "type" => profile.type.to_s, | |
20 | + "data" => { | |
21 | + "id" => profile.id, | |
22 | + "identifier" => profile.identifier, | |
23 | + "name" => profile.name, | |
24 | + "description" => profile.description | |
25 | + } | |
26 | + } | |
27 | + | |
28 | + attrs['articles-count'] = profile.articles.count | |
29 | + attrs['articles'] = [] | |
30 | + profile.articles.each do |article| | |
31 | + attrs['articles'] << article_attr_to_hash(article) | |
32 | + end | |
33 | + | |
34 | + attrs['activities-count'] = profile.activities.count | |
35 | + attrs['activities'] = profile_activities_attrs(profile) | |
36 | + | |
37 | + attrs | |
38 | + end | |
39 | + | |
40 | + def profile_activities_attrs profile | |
41 | + attrs = [] | |
42 | + | |
43 | + ids = profile.activities.collect { |activity| activity.id } | |
44 | + | |
45 | + ActionTracker::Record.find(ids).collect { |activity| | |
46 | + attrs << { | |
47 | + 'verb' => activity.verb, | |
48 | + 'params' => activity.params | |
49 | + } | |
50 | + } | |
51 | + | |
52 | + attrs | |
53 | + end | |
54 | + | |
55 | +end | ... | ... |
test/functional/colab_integration_plugin_controller_test.rb
... | ... | @@ -5,6 +5,8 @@ class ColabIntegrationPluginController; def rescue_action(e) raise e end; end |
5 | 5 | |
6 | 6 | class ColabIntegrationPluginControllerTest < ActionController::TestCase |
7 | 7 | |
8 | + include ProfileDataExport | |
9 | + | |
8 | 10 | def setup |
9 | 11 | @controller = ColabIntegrationPluginController.new |
10 | 12 | @john = fast_create(Person, :name => "John Snow") |
... | ... | @@ -73,13 +75,15 @@ class ColabIntegrationPluginControllerTest < ActionController::TestCase |
73 | 75 | end |
74 | 76 | |
75 | 77 | should "return a hash with activities of the user" do |
76 | - user = fast_create(Person, :name => "User") | |
77 | - fast_create(Community, :name => "User Community").add_member user | |
78 | - fast_create(Community, :name => "User Community 2").add_member user | |
79 | - | |
80 | - assert_equal 2, user.activities_attrs.count | |
81 | - assert user.activities_attrs.first.has_key?("verb") | |
82 | - assert user.activities_attrs.first.has_key?("params") | |
83 | - assert user.activities_attrs.first["verb"] == "join_community" | |
78 | + person = fast_create(Person, :name => "User") | |
79 | + fast_create(Community, :name => "User Community").add_member person | |
80 | + fast_create(Community, :name => "User Community 2").add_member person | |
81 | + | |
82 | + attrs = attr_to_hash(person) | |
83 | + | |
84 | + assert_equal 2, attrs['activities-count'] | |
85 | + assert attrs['activities'].first.has_key?("verb") | |
86 | + assert attrs['activities'].first.has_key?("params") | |
87 | + assert attrs['activities'].first["verb"] == "join_community" | |
84 | 88 | end |
85 | 89 | end | ... | ... |