diff --git a/app/controllers/my_profile/circles_controller.rb b/app/controllers/my_profile/circles_controller.rb index 7b74b15..dadab58 100644 --- a/app/controllers/my_profile/circles_controller.rb +++ b/app/controllers/my_profile/circles_controller.rb @@ -3,7 +3,7 @@ class CirclesController < MyProfileController before_action :accept_only_post, :only => [:create, :update, :destroy] def index - @circles = profile.circles + @circles = profile.owned_circles end def new @@ -11,7 +11,7 @@ class CirclesController < MyProfileController end def create - @circle = Circle.new(params[:circle].merge({ :person => profile })) + @circle = Circle.new(params[:circle].merge({ :owner => profile })) if @circle.save redirect_to :action => 'index' else @@ -21,7 +21,7 @@ class CirclesController < MyProfileController def xhr_create if request.xhr? - circle = Circle.new(params[:circle].merge({:person => profile })) + circle = Circle.new(params[:circle].merge({:owner => profile })) if circle.save render :partial => "circle_checkbox", :locals => { :circle => circle }, :status => 201 diff --git a/app/controllers/my_profile/followers_controller.rb b/app/controllers/my_profile/followers_controller.rb index 7288583..daaeafe 100644 --- a/app/controllers/my_profile/followers_controller.rb +++ b/app/controllers/my_profile/followers_controller.rb @@ -17,7 +17,7 @@ class FollowersController < MyProfileController def set_category_modal followed_profile = Profile.find(params[:followed_profile_id]) - circles = Circle.where(:person => profile, :profile_type => followed_profile.class.name) + circles = Circle.where(:owner => profile, :profile_type => followed_profile.class.name) render :partial => 'followers/edit_circles_modal', :locals => { :circles => circles, :followed_profile => followed_profile } end diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index bda1dae..f5d0a9e 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -176,7 +176,7 @@ class ProfileController < PublicController end def find_profile_circles - circles = Circle.where(:person => current_person, :profile_type => profile.class.name) + circles = Circle.where(:owner => current_person, :profile_type => profile.class.name) render :partial => 'blocks/profile_info_actions/circles', :locals => { :circles => circles, :profile_types => Circle.profile_types.to_a } end diff --git a/app/models/circle.rb b/app/models/circle.rb index 1349a2b..861ccf3 100644 --- a/app/models/circle.rb +++ b/app/models/circle.rb @@ -1,15 +1,15 @@ class Circle < ApplicationRecord #TODO -> n:m with profile, item in the circle - has_many :profile_followers + has_many :profile, :through => :profile_follower #TODO -> owner - belongs_to :person, polymorphic: true + belongs_to :owner, polymorphic: true - attr_accessible :name, :person, :profile_type + attr_accessible :name, :owner, :profile_type validates :name, presence: true - validates :person_id, presence: true + validates :owner_id, presence: true validates :profile_type, presence: true - validates :person_id, :uniqueness => {:scope => :name, :message => "can't add two circles with the same name"} + validates :owner_id, :uniqueness => {:scope => :name, :message => "can't add two circles with the same name"} validate :profile_type_must_be_in_list diff --git a/app/models/concerns/follower.rb b/app/models/concerns/follower.rb index 9565ebe..81eadee 100644 --- a/app/models/concerns/follower.rb +++ b/app/models/concerns/follower.rb @@ -4,7 +4,7 @@ module Follower def follow(profile, circles) circles = [circles] unless circles.is_a?(Array) circles.each do |new_circle| - next if new_circle.person != self || new_circle.profile_type != profile.class.name + next if new_circle.owner != self || new_circle.profile_type != profile.class.name ProfileFollower.create(profile: profile, circle: new_circle) end end @@ -33,7 +33,7 @@ module Follower end def remove_profile_from_circle(profile, circle) - return if circle.person != self + return if circle.owner != self ProfileFollower.with_profile(profile).with_circle(circle).destroy_all end diff --git a/app/models/external_person.rb b/app/models/external_person.rb index fd67f5d..7b4daf1 100644 --- a/app/models/external_person.rb +++ b/app/models/external_person.rb @@ -5,7 +5,10 @@ class ExternalPerson < ActiveRecord::Base include ProfileEntity include Follower - has_many :circles, as: :person + has_many :profile_followers + has_many :circles, :through => :profile_followers, :as => :profile + has_many :owned_circles, as: :owner, :class_name => "Circle" + validates_uniqueness_of :identifier, scope: :source validates_presence_of :source, :email, :created_at diff --git a/app/models/person.rb b/app/models/person.rb index a11864a..4dff60b 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -94,7 +94,7 @@ class Person < Profile has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article has_many :friendships, :dependent => :destroy has_many :friends, :class_name => 'Person', :through => :friendships - has_many :circles, as: :person + has_many :owned_circles, as: :owner, :class_name => 'Circle' scope :online, -> { joins(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes) diff --git a/app/models/profile.rb b/app/models/profile.rb index 097376f..7cf9cab 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -223,14 +223,7 @@ class Profile < ApplicationRecord distinct.select('profiles.*'). joins('left join profiles_circles ON profiles_circles.profile_id = profiles.id'). joins('left join circles ON circles.id = profiles_circles.circle_id'). - where('circles.person_id = ?', person.id) - } - - scope :in_circle, -> circle{ - distinct.select('profiles.*'). - joins('left join profiles_circles ON profiles_circles.profile_id = profiles.id'). - joins('left join circles ON circles.id = profiles_circles.circle_id'). - where('circles.id = ?', circle.id) + where('circles.owner_id = ?', person.id) } settings_items :allow_followers, :type => :boolean, :default => true @@ -249,15 +242,11 @@ class Profile < ApplicationRecord has_many :email_templates, :foreign_key => :owner_id has_many :profile_followers - - def in_circles - Circle.joins(:profile_followers). - where('profiles_circles.profile_id = ?', self.id) - end + has_many :circles, :through => :profile_followers, :as => :profile def followers - person_followers = Person.joins(:circles).merge(in_circles).uniq - external_person_followers = ExternalPerson.joins(:circles).merge(in_circles).uniq + person_followers = Person.joins(:owned_circles).merge(circles).uniq + external_person_followers = ExternalPerson.joins(:owned_circles).merge(circles).uniq person_followers + external_person_followers end @@ -1068,7 +1057,7 @@ private :generate_url, :url_options person.kind_of?(Profile) && person.has_permission?('destroy_profile', self) end - def in_circle?(circle, follower) - ProfileFollower.with_follower(follower).with_circle(circle).with_profile(self).present? + def in_circle?(circle) + circle.in? self.circles end end diff --git a/app/models/profile_follower.rb b/app/models/profile_follower.rb index f42129f..ccc8636 100644 --- a/app/models/profile_follower.rb +++ b/app/models/profile_follower.rb @@ -5,14 +5,14 @@ class ProfileFollower < ApplicationRecord attr_accessible :profile, :circle #TODO -> user being followed - belongs_to :profile + belongs_to :profile, :polymorphic => true belongs_to :circle #TODO -> circle owner # has_one :person, through: :circle, source_type: "ProfileFollower" def circle_owner - self.circle.person + self.circle.owner end alias follower circle_owner @@ -21,7 +21,7 @@ class ProfileFollower < ApplicationRecord validates :profile_id, :uniqueness => {:scope => :circle_id, :message => "can't put a profile in the same circle twice"} scope :with_follower, -> person{ - joins(:circle).where('circles.person_id = ?', person.id) + joins(:circle).where('circles.owner_id = ?', person.id) } scope :with_profile, -> profile{ diff --git a/app/views/blocks/profile_info_actions/_select_circles.html.erb b/app/views/blocks/profile_info_actions/_select_circles.html.erb index e4524dc..cdd158b 100644 --- a/app/views/blocks/profile_info_actions/_select_circles.html.erb +++ b/app/views/blocks/profile_info_actions/_select_circles.html.erb @@ -3,7 +3,7 @@