From 6e677d68b1a13e42fa0058e476ce97f3ae68c551 Mon Sep 17 00:00:00 2001 From: DylanGuedes Date: Thu, 11 Aug 2016 13:43:00 -0300 Subject: [PATCH] Adds polymorphic relationship between circle and person. --- app/models/circle.rb | 2 ++ app/models/external_person.rb | 1 - app/models/friendship.rb | 3 +++ app/models/profile.rb | 22 ++++++++++++++++++++-- app/models/profile_follower.rb | 12 +++++++++--- app/views/blocks/profile_info_actions/_common.html.erb | 2 +- db/migrate/20160810132802_add_profile_type_to_profile_followers.rb | 11 ----------- db/schema.rb | 13 +++++++++++-- vendor/plugins/action_tracker/lib/action_tracker.rb | 6 ------ 9 files changed, 46 insertions(+), 26 deletions(-) delete mode 100644 db/migrate/20160810132802_add_profile_type_to_profile_followers.rb diff --git a/app/models/circle.rb b/app/models/circle.rb index 722db33..1349a2b 100644 --- a/app/models/circle.rb +++ b/app/models/circle.rb @@ -1,5 +1,7 @@ class Circle < ApplicationRecord + #TODO -> n:m with profile, item in the circle has_many :profile_followers + #TODO -> owner belongs_to :person, polymorphic: true attr_accessible :name, :person, :profile_type diff --git a/app/models/external_person.rb b/app/models/external_person.rb index bbf1cc3..abab490 100644 --- a/app/models/external_person.rb +++ b/app/models/external_person.rb @@ -5,7 +5,6 @@ class ExternalPerson < ActiveRecord::Base include ProfileEntity has_many :circles, as: :person - has_many :profile_followers, as: :profile validates_uniqueness_of :identifier, scope: :source validates_presence_of :source, :email, :created_at diff --git a/app/models/friendship.rb b/app/models/friendship.rb index 7a6927b..7cf1395 100644 --- a/app/models/friendship.rb +++ b/app/models/friendship.rb @@ -10,6 +10,9 @@ class Friendship < ApplicationRecord Friendship.update_cache_counter(:friends_count, friendship.person, 1) Friendship.update_cache_counter(:friends_count, friendship.friend, 1) + p "*"*60 + p "BEFORE CIRCLES" + p "*"*60 circles = friendship.group.blank? ? ['friendships'] : friendship.group.split(',').map(&:strip) circles.each do |circle| friendship.person.follow(friendship.friend, Circle.find_or_create_by(:person => friendship.person, :name => circle, :profile_type => 'Person')) diff --git a/app/models/profile.rb b/app/models/profile.rb index 0f18e5c..e7f5837 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -248,8 +248,26 @@ class Profile < ApplicationRecord has_many :email_templates, :foreign_key => :owner_id - has_many :profile_followers, as: :profile - has_many :followers, -> { uniq }, :class_name => 'Person', :through => :profile_followers, :source => :person + has_many :profile_followers + scope :memberships_of, -> person { + distinct.select('profiles.*'). + joins(:role_assignments). + where('role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id) + } + + def in_circles + Circle.joins(:profile_followers). + where('profiles_circles.profile_id = ?', self.id) + end + + def followers + person_followers = Person.joins(:circles).merge(in_circles) + external_person_followers = ExternalPerson.joins(:circles).merge(in_circles) + + person_followers+external_person_followers + end + + # has_many :followers, -> { uniq }, :through => :profile_followers, :source => :person # Although this should be a has_one relation, there are no non-silly names for # a foreign key on article to reference the template to which it is diff --git a/app/models/profile_follower.rb b/app/models/profile_follower.rb index 2c539cf..f42129f 100644 --- a/app/models/profile_follower.rb +++ b/app/models/profile_follower.rb @@ -4,12 +4,18 @@ class ProfileFollower < ApplicationRecord attr_accessible :profile, :circle - belongs_to :profile, polymorphic: :true + #TODO -> user being followed + belongs_to :profile belongs_to :circle - has_one :person, through: :circle, source_type: "ProfileFollower" + #TODO -> circle owner + # has_one :person, through: :circle, source_type: "ProfileFollower" - alias follower person + def circle_owner + self.circle.person + end + + alias follower circle_owner validates_presence_of :profile_id, :circle_id validates :profile_id, :uniqueness => {:scope => :circle_id, :message => "can't put a profile in the same circle twice"} diff --git a/app/views/blocks/profile_info_actions/_common.html.erb b/app/views/blocks/profile_info_actions/_common.html.erb index a59ef79..ed83824 100644 --- a/app/views/blocks/profile_info_actions/_common.html.erb +++ b/app/views/blocks/profile_info_actions/_common.html.erb @@ -1,7 +1,7 @@
  • <%= report_abuse(profile, :button) %>
  • <% if logged_in? && (user != profile) && profile.allow_followers? %>
  • - <% follow = user.follows?(profile) %> + <% follow = profile.followed_by user %> <%= button(:unfollow, content_tag('span', _('Unfollow')), {:profile => profile.identifier, :controller => 'profile', :action => 'unfollow'}, :method => :post, :id => 'action-unfollow', :title => _("Unfollow"), :style => follow ? "" : "display: none;") %> <%= button(:ok, content_tag('span', _('Follow')), {:profile => profile.identifier, :controller => 'profile', :action => 'find_profile_circles'}, :id => 'action-follow', :title => _("Follow"), :style => follow ? "display: none;" : "") %>