Commit a4dae44e4cd0dbe0388c3f0c324b76a373fcf00b

Authored by Gabriel Silva
1 parent 0cff313e

Reviews ExternalProfile methods

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
app/models/concerns/follower.rb
... ... @@ -5,9 +5,6 @@ module Follower
5 5 circles = [circles] unless circles.is_a?(Array)
6 6 circles.each do |new_circle|
7 7 next if new_circle.owner != self || !profile.kind_of?(new_circle.profile_type.constantize)
8   - if (new_circle.owner != self && !(self.kind_of?(new_circle.owner.class)) || new_circle.profile_type != profile.class.name)
9   - next
10   - end
11 8 ProfileFollower.create(profile: profile, circle: new_circle)
12 9 end
13 10 end
... ... @@ -22,7 +19,7 @@ module Follower
22 19 end
23 20  
24 21 def followed_profiles
25   - external_people = ExternalPerson.joins(:circles).where("circles.owner_id = ?", self.id)
  22 + external_people = ExternalProfile.joins(:circles).where("circles.owner_id = ?", self.id)
26 23 profiles = Profile.joins(:circles).where("circles.owner_id = ?", self.id)
27 24  
28 25 external_people.uniq + profiles.uniq
... ... @@ -43,8 +40,4 @@ module Follower
43 40 ProfileFollower.with_profile(profile).with_circle(circle).destroy_all
44 41 end
45 42  
46   - def in_circle?(circle)
47   - circle.in? self.circles
48   - end
49   -
50 43 end
... ...
app/models/external_person.rb
... ... @@ -5,8 +5,6 @@ class ExternalPerson &lt; ExternalProfile
5 5 include ProfileEntity
6 6 include Follower
7 7  
8   - has_many :profile_followers, :as => :profile
9   - has_many :circles, :through => :profile_followers
10 8 has_many :owned_circles, as: :owner, :class_name => "Circle"
11 9  
12 10 validates_uniqueness_of :identifier, scope: :source
... ... @@ -180,91 +178,19 @@ class ExternalPerson &lt; ExternalProfile
180 178 derivated_methods.merge(methods_and_responses)
181 179 end
182 180  
183   - def profile_instance_methods
184   - methods_and_responses = {
185   - role_assignments: RoleAssignment.none, favorite_enterprises:
186   - Enterprise.none, memberships: Profile.none, friendships: Profile.none,
187   - tasks: Task.none, suggested_profiles: ProfileSuggestion.none,
188   - suggested_people: ProfileSuggestion.none, suggested_communities:
189   - ProfileSuggestion.none, public_profile: true, nickname: nil, custom_footer:
190   - '', custom_header: '', address: '', zip_code: '', contact_phone: '',
191   - image_builder: nil, description: '', closed: false, template_id: nil, lat:
192   - nil, lng: nil, is_template: false, fields_privacy: {}, preferred_domain_id:
193   - nil, category_ids: [], country: '', city: '', state: '',
194   - national_region_code: '', redirect_l10n: false, notification_time: 0,
195   - custom_url_redirection: nil, email_suggestions: false,
196   - allow_members_to_invite: false, invite_friends_only: false, secret: false,
197   - profile_admin_mail_notification: false, redirection_after_login: nil,
198   - profile_activities: ProfileActivity.none, action_tracker_notifications:
199   - ActionTrackerNotification.none, tracked_notifications:
200   - ActionTracker::Record.none, scraps_received: Scrap.none, template:
201   - Profile.none, comments_received: Comment.none, email_templates:
202   - EmailTemplate.none, members: Profile.none, members_like: Profile.none,
203   - members_by: Profile.none, members_by_role: Profile.none, scraps:
204   - Scrap.none, welcome_page_content: nil, settings: {}, find_in_all_tasks:
205   - nil, top_level_categorization: {}, interests: Category.none, geolocation:
206   - '', country_name: '', pending_categorizations: [], add_category: false,
207   - create_pending_categorizations: false, top_level_articles: Article.none,
208   - valid_identifier: true, valid_template: false, create_default_set_of_boxes:
209   - true, copy_blocks_from: nil, default_template: nil,
210   - template_without_default: nil, template_with_default: nil, apply_template:
211   - false, iframe_whitelist: [], recent_documents: Article.none, last_articles:
212   - Article.none, is_validation_entity?: false, hostname: nil, own_hostname:
213   - nil, article_tags: {}, tagged_with: Article.none,
214   - insert_default_article_set: false, copy_articles_from: true,
215   - copy_article_tree: nil, copy_article?: false, add_member: false,
216   - remove_member: false, add_admin: false, remove_admin: false, add_moderator:
217   - false, display_info_to?: true, update_category_from_region: nil,
218   - accept_category?: false, custom_header_expanded: '',
219   - custom_footer_expanded: '', public?: true, themes: [], find_theme: nil,
220   - blogs: Blog.none, blog: nil, has_blog?: false, forums: Forum.none, forum:
221   - nil, has_forum?: false, admins: [], settings_field: {}, setting_changed:
222   - false, public_content: true, enable_contact?: false, folder_types: [],
223   - folders: Article.none, image_galleries: Article.none, image_valid: true,
224   - update_header_and_footer: nil, update_theme: nil, update_layout_template:
225   - nil, recent_actions: ActionTracker::Record.none, recent_notifications:
226   - ActionTracker::Record.none, more_active_label: _('no activity'),
227   - more_popular_label: _('no members'), profile_custom_image: nil,
228   - is_on_homepage?: false, activities: ProfileActivity.none,
229   - may_display_field_to?: true, may_display_location_to?: true, public_fields:
230   - {}, display_private_info_to?: true, can_view_field?:
231   - true, remove_from_suggestion_list: nil, layout_template: 'default',
232   - is_admin?: false, add_friend: false, is_a_friend?: false,
233   - already_request_friendship?: false, tracked_actions: ActionTracker::Record.none
234   - }
235   -
236   - derivated_methods = generate_derivated_methods(methods_and_responses)
237   - derivated_methods.merge(methods_and_responses)
238   - end
239   -
240 181 def method_missing(method, *args, &block)
241 182 if person_instance_methods.keys.include?(method)
242 183 return person_instance_methods[method]
243 184 end
244   - if profile_instance_methods.keys.include? method
245   - return profile_instance_methods[method]
246   - end
247   - super
  185 + super(method, *args, &block)
248 186 end
249 187  
250 188 def respond_to_missing?(method_name, include_private = false)
251 189 person_instance_methods.keys.include?(method_name) ||
252   - profile_instance_methods.keys.include?(method_name) ||
253 190 super
254 191 end
255 192  
256 193 def kind_of?(klass)
257 194 (klass == Person) ? true : super
258 195 end
259   -
260   - private
261   -
262   - def generate_derivated_methods(methods)
263   - derivated_methods = {}
264   - methods.keys.each do |method|
265   - derivated_methods[method.to_s.insert(-1, '?').to_sym] = false
266   - derivated_methods[method.to_s.insert(-1, '=').to_sym] = nil
267   - end
268   - derivated_methods
269   - end
270 196 end
... ...
app/models/external_profile.rb
... ... @@ -2,7 +2,8 @@ class ExternalProfile &lt; ActiveRecord::Base
2 2  
3 3 include Followable
4 4  
5   - has_many :circles, as: :person
  5 + has_many :profile_followers, :as => :profile
  6 + has_many :circles, :through => :profile_followers
6 7  
7 8 def name
8 9 "#{self[:name]}@#{self.source}"
... ... @@ -40,53 +41,8 @@ class ExternalProfile &lt; ActiveRecord::Base
40 41 "http://#{self.source}/profile/#{self.identifier}/icon/"
41 42 end
42 43  
43   - # External Person should respond to all methods in Person and Profile
44   - def person_instance_methods
45   - methods_and_responses = {
46   - enterprises: Enterprise.none, communities: Community.none, friends:
47   - Person.none, memberships: Profile.none, friendships: Person.none,
48   - following_articles: Article.none, article_followers: ArticleFollower.none,
49   - requested_tasks: Task.none, mailings: Mailing.none, scraps_sent:
50   - Scrap.none, favorite_enterprise_people: FavoriteEnterprisePerson.none,
51   - favorite_enterprises: Enterprise.none, acepted_forums: Forum.none,
52   - articles_with_access: Article.none, suggested_profiles:
53   - ProfileSuggestion.none, suggested_people: ProfileSuggestion.none,
54   - suggested_communities: ProfileSuggestion.none, user: nil,
55   - refused_communities: Community.none, has_permission?: false,
56   - has_permission_with_admin?: false, has_permission_without_admin?: false,
57   - has_permission_with_plugins?: false, has_permission_without_plugins?:
58   - false, memberships_by_role: Person.none, can_change_homepage?: false,
59   - can_control_scrap?: false, receives_scrap_notification?: false,
60   - can_control_activity?: false, can_post_content?: false,
61   - suggested_friend_groups: [], friend_groups: [], add_friend: nil,
62   - already_request_friendship?: false, remove_friend: nil,
63   - presence_of_required_fields: nil, active_fields: [], required_fields: [],
64   - signup_fields: [], default_set_of_blocks: [], default_set_of_boxes: [],
65   - default_set_of_articles: [], cell_phone: nil, comercial_phone: nil,
66   - nationality: nil, schooling: nil, contact_information: nil, sex: nil,
67   - birth_date: nil, jabber_id: nil, personal_website: nil, address_reference:
68   - nil, district: nil, schooling_status: nil, formation: nil,
69   - custom_formation: nil, area_of_study: nil, custom_area_of_study: nil,
70   - professional_activity: nil, organization_website: nil, organization: nil,
71   - photo: nil, city: nil, state: nil, country: nil, zip_code: nil,
72   - address_line2: nil, copy_communities_from: nil,
73   - has_organization_pending_tasks?: false, organizations_with_pending_tasks:
74   - Organization.none, pending_tasks_for_organization: Task.none,
75   - build_contact: nil, is_a_friend?: false, ask_to_join?: false, refuse_join:
76   - nil, blocks_to_expire_cache: [], cache_keys: [], communities_cache_key: '',
77   - friends_cache_key: '', manage_friends_cache_key: '',
78   - relationships_cache_key: '', is_member_of?: false,
79   - each_friend: nil, is_last_admin?: false, is_last_admin_leaving?: false,
80   - leave: nil, last_notification: nil, notification_time: 0, notifier: nil,
81   - remove_suggestion: nil, allow_invitation_from?: false, in_social_circle?: false,
82   - allow_followers: false, in_circles: Circle.none, followers: [], in_circle?: false
83   - }
84   -
85   - derivated_methods = generate_derivated_methods(methods_and_responses)
86   - derivated_methods.merge(methods_and_responses)
87   - end
88   -
89   - def profile_instance_methods
  44 + # External Profile should respond to all methods in Profile
  45 +def profile_instance_methods
90 46 methods_and_responses = {
91 47 role_assignments: RoleAssignment.none, favorite_enterprises:
92 48 Enterprise.none, memberships: Profile.none, friendships: Profile.none,
... ... @@ -133,10 +89,10 @@ class ExternalProfile &lt; ActiveRecord::Base
133 89 more_popular_label: _('no members'), profile_custom_image: nil,
134 90 is_on_homepage?: false, activities: ProfileActivity.none,
135 91 may_display_field_to?: true, may_display_location_to?: true, public_fields:
136   - {}, followed_by?: false, display_private_info_to?: true, can_view_field?:
  92 + {}, display_private_info_to?: true, can_view_field?:
137 93 true, remove_from_suggestion_list: nil, layout_template: 'default',
138 94 is_admin?: false, add_friend: false, is_a_friend?: false,
139   - already_request_friendship?: false
  95 + already_request_friendship?: false, tracked_actions: ActionTracker::Record.none
140 96 }
141 97  
142 98 derivated_methods = generate_derivated_methods(methods_and_responses)
... ... @@ -144,18 +100,14 @@ class ExternalProfile &lt; ActiveRecord::Base
144 100 end
145 101  
146 102 def method_missing(method, *args, &block)
147   - if person_instance_methods.keys.include?(method)
148   - return person_instance_methods[method]
149   - end
150 103 if profile_instance_methods.keys.include? method
151 104 return profile_instance_methods[method]
152 105 end
  106 + raise NoMethodError, "undefined method #{method} for #{self}"
153 107 end
154 108  
155 109 def respond_to_missing?(method_name, include_private = false)
156   - person_instance_methods.keys.include?(method_name) ||
157   - profile_instance_methods.keys.include?(method_name) ||
158   - super
  110 + profile_instance_methods.keys.include?(method_name) || super
159 111 end
160 112  
161 113 private
... ...
test/unit/external_person_test.rb
... ... @@ -135,6 +135,12 @@ class ExternalPersonTest &lt; ActiveSupport::TestCase
135 135 end
136 136 end
137 137  
  138 + should 'raise error if method is not in Person or Profile' do
  139 + assert_raise NoMethodError do
  140 + ExternalPerson.new.send(:this_method_does_not_exist_right?)
  141 + end
  142 + end
  143 +
138 144 should 'be able to follow people' do
139 145 person = fast_create(Person, :environment_id => Environment.default.id)
140 146 circle = Circle.create(owner: @external_person, profile_type: "Person", name: "FRIENDSSS")
... ...
test/unit/followable_test.rb 0 → 100644
... ... @@ -0,0 +1,59 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class FollowerTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @person1 = create_user('perso-test-1').person
  7 + @person2 = create_user('person-test-2').person
  8 + @external_person = ExternalPerson.create!(identifier: 'johnlocke',
  9 + name: 'John Locke',
  10 + source: 'anerenvironment.org',
  11 + email: 'locke@island.org',
  12 + created_at: Date.yesterday)
  13 +
  14 + @circle1 = Circle.create!(owner: @person1, name: "Zombies", profile_type: 'Person')
  15 + @circle2 = Circle.create!(owner: @person1, name: "Humans", profile_type: 'Person')
  16 + @circle3 = Circle.create!(owner: @external_person, name: "Crypt", profile_type: 'Person')
  17 +
  18 + @external_profile = ExternalProfile.create
  19 + end
  20 +
  21 + should 'return all unique circles and followers of a profile' do
  22 + @person1.follow(@person2, @circle1)
  23 + @person1.follow(@person2, @circle2)
  24 + @external_person.follow(@person2, @circle3)
  25 +
  26 + assert_equivalent [@circle1, @circle2, @circle3], @person2.circles
  27 + assert_equivalent [@person1, @external_person], @person2.followers
  28 + end
  29 +
  30 + should 'return all unique circles and followers of a external profile' do
  31 + Circle.any_instance.stubs(:profile_type).returns("ExternalProfile")
  32 + @person1.follow(@external_profile, @circle1)
  33 + @person1.follow(@external_profile, @circle2)
  34 + @external_person.follow(@external_profile, @circle3)
  35 +
  36 + assert_equivalent [@circle1, @circle2, @circle3], @external_profile.circles
  37 + assert_equivalent [@person1, @external_person], @external_profile.followers
  38 + end
  39 +
  40 + should 'onlyreturn true if profile is in circle' do
  41 + @person1.follow(@person2, @circle1)
  42 + @external_person.follow(@person2, @circle3)
  43 +
  44 + assert @person2.in_circle? @circle1
  45 + assert @person2.in_circle? @circle3
  46 + refute @person2.in_circle? @circle2
  47 + end
  48 +
  49 + should 'only return true if external profile is in circle' do
  50 + Circle.any_instance.stubs(:profile_type).returns("ExternalProfile")
  51 + @person1.follow(@external_profile, @circle1)
  52 + @external_person.follow(@external_profile, @circle3)
  53 +
  54 + assert @external_profile.in_circle? @circle1
  55 + assert @external_profile.in_circle? @circle3
  56 + refute @external_profile.in_circle? @circle2
  57 + end
  58 +
  59 +end
... ...
test/unit/follower_test.rb
... ... @@ -85,13 +85,13 @@ class FollowerTest &lt; ActiveSupport::TestCase
85 85 end
86 86  
87 87 should 'get the followed profiles for a regular user' do
88   - person3 = create_user('person-test-3').person
89 88 community = fast_create(Community)
90 89  
91 90 @person1.follow(@person2, @circle1)
92   - @person1.follow(person3, @circle1)
  91 + @person1.follow(@external_person, @circle1)
93 92 @person1.follow(community, @circle3)
94   - assert_equivalent [@person2, person3, community], @person1.followed_profiles
  93 + assert_equivalent [@person2, @external_person, community],
  94 + @person1.followed_profiles
95 95 end
96 96  
97 97 should 'get the followed profiles for an external user' do
... ... @@ -103,7 +103,8 @@ class FollowerTest &lt; ActiveSupport::TestCase
103 103 @external_person.follow(@person2, @circle1)
104 104 @external_person.follow(person3, @circle1)
105 105 @external_person.follow(community, @circle3)
106   - assert_equivalent [@person2, person3, community], @external_person.followed_profiles
  106 + assert_equivalent [@person2, person3, community],
  107 + @external_person.followed_profiles
107 108 end
108 109  
109 110 should 'not follow same person twice even with different circles' do
... ... @@ -145,7 +146,8 @@ class FollowerTest &lt; ActiveSupport::TestCase
145 146 @person1.follow(@person2, [@circle1, @circle2])
146 147  
147 148 @person1.remove_profile_from_circle(@person2, @circle2)
148   - assert_equivalent [@circle1], @person2.circles
  149 + assert @person2.in_circle? @circle1
  150 + assert_not @person2.in_circle? @circle2
149 151 end
150 152  
151 153 should 'not remove a person from a circle if the user is not the owner' do
... ... @@ -153,13 +155,30 @@ class FollowerTest &lt; ActiveSupport::TestCase
153 155 @person1.follow(@person2, [@circle1, @circle2])
154 156  
155 157 person3.remove_profile_from_circle(@person2, @circle2)
156   - assert_equivalent [@circle1, @circle2], @person2.circles
  158 + assert @person2.in_circle? @circle1
  159 + assert @person2.in_circle? @circle2
  160 + end
  161 +
  162 + should 'follow external profile' do
  163 + external_profile = ExternalProfile.create
  164 + @circle1.stubs(:profile_type).returns("ExternalProfile")
  165 +
  166 + assert_difference '@person1.followed_profiles.count' do
  167 + @person1.follow(external_profile, @circle1)
  168 + end
  169 + assert @person1.follows? external_profile
  170 + assert_equivalent [external_profile], @person1.followed_profiles
157 171 end
158 172  
159   - should 'follow External Person' do
160   - assert_difference 'ProfileFollower.count' do
161   - @person1.follow(@external_person, @circle1)
  173 + should 'unfollow external profile' do
  174 + external_profile = ExternalProfile.create
  175 + @circle1.stubs(:profile_type).returns("ExternalProfile")
  176 + @person1.follow(external_profile, @circle1)
  177 +
  178 + assert_difference '@person1.followed_profiles.count', -1 do
  179 + @person1.unfollow(external_profile)
162 180 end
163   - assert @person1.follows? @external_person
  181 + assert_not @person1.follows?(@person2)
164 182 end
  183 +
165 184 end
... ...
test/unit/profile_test.rb
... ... @@ -2232,21 +2232,4 @@ class ProfileTest &lt; ActiveSupport::TestCase
2232 2232 c.add_member(p)
2233 2233 end
2234 2234 end
2235   -
2236   - should 'return all circles and followers, and do not repeat' do
2237   - person1 = create_user('testperson-1').person
2238   - person2 = create_user('testperson-2').person
2239   - person3 = create_user('testperson-3').person
2240   - circle1 = Circle.create!(:owner => person1, :name => "Night's Watch", :profile_type => 'Person')
2241   - circle2 = Circle.create!(:owner => person1, :name => "Free Folk", :profile_type => 'Person')
2242   - circle3 = Circle.create!(:owner => person2, :name => "The Unsullied", :profile_type => 'Person')
2243   -
2244   - person1.follow(person3, circle1)
2245   - person1.follow(person3, circle2)
2246   - person2.follow(person3, circle3)
2247   -
2248   - assert_equivalent person3.circles, [circle1, circle2, circle3]
2249   - assert_equivalent person3.followers, [person1, person2]
2250   - end
2251   -
2252 2235 end
... ...