Commit 5f61fb44edaf090ab47b24f2cf3dcf9a245423f5

Authored by Gabriel Silva
1 parent f8e47f31

Introduces the Followable concert

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Signed-off-by: DylanGuedes <djmgguedes@gmail.com>
app/models/concerns/followable.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +module Followable
  2 + extend ActiveSupport::Concern
  3 +
  4 + def followers
  5 + person_followers = Person.joins(:owned_circles).merge(circles).uniq
  6 + external_person_followers = ExternalPerson.joins(:owned_circles).merge(circles).uniq
  7 +
  8 + person_followers + external_person_followers
  9 + end
  10 +
  11 + def followed_by?(person)
  12 + (person == self) || (person.in? self.followers)
  13 + end
  14 +
  15 + def in_circle?(circle)
  16 + circle.in? self.circles
  17 + end
  18 +end
... ...
app/models/concerns/follower.rb
... ... @@ -4,14 +4,14 @@ module Follower
4 4 def follow(profile, circles)
5 5 circles = [circles] unless circles.is_a?(Array)
6 6 circles.each do |new_circle|
7   - next if new_circle.owner != self || new_circle.profile_type != profile.class.name
  7 + next if new_circle.owner != self || !profile.kind_of?(new_circle.profile_type.constantize)
8 8 ProfileFollower.create(profile: profile, circle: new_circle)
9 9 end
10 10 end
11 11  
12 12 def follows?(profile)
13 13 return false if profile.nil?
14   - p profile.followed_by?(self)
  14 + profile.followed_by?(self)
15 15 end
16 16  
17 17 def unfollow(profile)
... ... @@ -19,7 +19,10 @@ module Follower
19 19 end
20 20  
21 21 def followed_profiles
22   - Profile.followed_by self
  22 + external_people = ExternalPerson.joins(:circles).where("circles.owner_id = ?", self.id)
  23 + profiles = Profile.joins(:circles).where("circles.owner_id = ?", self.id)
  24 +
  25 + external_people.uniq + profiles.uniq
23 26 end
24 27  
25 28 def update_profile_circles(profile, new_circles)
... ... @@ -37,4 +40,8 @@ module Follower
37 40 ProfileFollower.with_profile(profile).with_circle(circle).destroy_all
38 41 end
39 42  
  43 + def in_circle?(circle)
  44 + circle.in? self.circles
  45 + end
  46 +
40 47 end
... ...
app/models/external_person.rb
... ... @@ -4,9 +4,10 @@ class ExternalPerson &lt; ActiveRecord::Base
4 4 include Human
5 5 include ProfileEntity
6 6 include Follower
  7 + include Followable
7 8  
8   - has_many :profile_followers
9   - has_many :circles, :through => :profile_followers, :as => :profile
  9 + has_many :profile_followers, :as => :profile
  10 + has_many :circles, :through => :profile_followers
10 11 has_many :owned_circles, as: :owner, :class_name => "Circle"
11 12  
12 13 validates_uniqueness_of :identifier, scope: :source
... ... @@ -204,7 +205,7 @@ class ExternalPerson &lt; ActiveRecord::Base
204 205 each_friend: nil, is_last_admin?: false, is_last_admin_leaving?: false,
205 206 leave: nil, last_notification: nil, notification_time: 0, notifier: nil,
206 207 remove_suggestion: nil, allow_invitation_from?: false, in_social_circle?: false,
207   - allow_followers: false, in_circles: Circle.none, followers: [], in_circle?: false
  208 + allow_followers: false
208 209 }
209 210  
210 211 derivated_methods = generate_derivated_methods(methods_and_responses)
... ... @@ -258,7 +259,7 @@ class ExternalPerson &lt; ActiveRecord::Base
258 259 more_popular_label: _('no members'), profile_custom_image: nil,
259 260 is_on_homepage?: false, activities: ProfileActivity.none,
260 261 may_display_field_to?: true, may_display_location_to?: true, public_fields:
261   - {}, followed_by?: false, display_private_info_to?: true, can_view_field?:
  262 + {}, display_private_info_to?: true, can_view_field?:
262 263 true, remove_from_suggestion_list: nil, layout_template: 'default',
263 264 is_admin?: false, add_friend: false, is_a_friend?: false,
264 265 already_request_friendship?: false, tracked_actions: ActionTracker::Record.none
... ... @@ -275,6 +276,7 @@ class ExternalPerson &lt; ActiveRecord::Base
275 276 if profile_instance_methods.keys.include? method
276 277 return profile_instance_methods[method]
277 278 end
  279 + super
278 280 end
279 281  
280 282 def respond_to_missing?(method_name, include_private = false)
... ... @@ -283,6 +285,10 @@ class ExternalPerson &lt; ActiveRecord::Base
283 285 super
284 286 end
285 287  
  288 + def kind_of?(klass)
  289 + (klass == Person) ? true : super
  290 + end
  291 +
286 292 private
287 293  
288 294 def generate_derivated_methods(methods)
... ...
app/models/person.rb
... ... @@ -570,10 +570,6 @@ class Person &lt; Profile
570 570 }
571 571 end
572 572  
573   - def followed_profiles
574   - Profile.followed_by self
575   - end
576   -
577 573 def in_social_circle?(person)
578 574 self.is_a_friend?(person) || super
579 575 end
... ...
app/models/profile.rb
... ... @@ -4,6 +4,7 @@
4 4 class Profile < ApplicationRecord
5 5  
6 6 include ProfileEntity
  7 + include Followable
7 8  
8 9 attr_accessible :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time,
9 10 :custom_url_redirection, :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification, :redirection_after_login, :allow_followers
... ... @@ -219,13 +220,6 @@ class Profile &lt; ApplicationRecord
219 220 scope :more_active, -> { order 'activities_count DESC' }
220 221 scope :more_recent, -> { order "created_at DESC" }
221 222  
222   - scope :followed_by, -> person{
223   - distinct.select('profiles.*').
224   - joins('left join profiles_circles ON profiles_circles.profile_id = profiles.id').
225   - joins('left join circles ON circles.id = profiles_circles.circle_id').
226   - where('circles.owner_id = ?', person.id)
227   - }
228   -
229 223 settings_items :allow_followers, :type => :boolean, :default => true
230 224 alias_method :allow_followers?, :allow_followers
231 225  
... ... @@ -241,15 +235,8 @@ class Profile &lt; ApplicationRecord
241 235  
242 236 has_many :email_templates, :foreign_key => :owner_id
243 237  
244   - has_many :profile_followers
245   - has_many :circles, :through => :profile_followers, :as => :profile
246   -
247   - def followers
248   - person_followers = Person.joins(:owned_circles).merge(circles).uniq
249   - external_person_followers = ExternalPerson.joins(:owned_circles).merge(circles).uniq
250   -
251   - person_followers + external_person_followers
252   - end
  238 + has_many :profile_followers, :as => :profile
  239 + has_many :circles, :through => :profile_followers
253 240  
254 241 # has_many :followers, -> { uniq }, :through => :profile_followers, :source => :person
255 242  
... ... @@ -1010,10 +997,6 @@ private :generate_url, :url_options
1010 997 self.active_fields
1011 998 end
1012 999  
1013   - def followed_by?(person)
1014   - (person == self) || (person.in? self.followers)
1015   - end
1016   -
1017 1000 def in_social_circle?(person)
1018 1001 (person == self) || (person.is_member_of?(self))
1019 1002 end
... ... @@ -1057,7 +1040,4 @@ private :generate_url, :url_options
1057 1040 person.kind_of?(Profile) && person.has_permission?('destroy_profile', self)
1058 1041 end
1059 1042  
1060   - def in_circle?(circle)
1061   - circle.in? self.circles
1062   - end
1063 1043 end
... ...
test/unit/follower_test.rb
... ... @@ -150,14 +150,10 @@ class FollowerTest &lt; ActiveSupport::TestCase
150 150 assert_equivalent [@circle1, @circle2], @person2.circles
151 151 end
152 152  
153   - should 'external person be followable' do
154   - person = fast_create(Person, :environment_id => Environment.default.id)
155   - circle = Circle.create(owner: @external_person, profile_type: "Person", name: "FRIENDSSS")
156   - pf = ProfileFollower.create(profile: @external_person, circle: circle)
157   - assert person.follows? @external_person
158   -
159   - # assert_difference 'ProfileFollower.all.count' do
160   - # person.follow(@external_person, circle)
161   - # end
  153 + should 'follow External Person' do
  154 + assert_difference 'ProfileFollower.count' do
  155 + @person1.follow(@external_person, @circle1)
  156 + end
  157 + assert @person1.follows? @external_person
162 158 end
163 159 end
... ...