diff --git a/app/controllers/my_profile/follow_categories_controller.rb b/app/controllers/my_profile/follow_categories_controller.rb index a27eb5c..468859a 100644 --- a/app/controllers/my_profile/follow_categories_controller.rb +++ b/app/controllers/my_profile/follow_categories_controller.rb @@ -5,11 +5,11 @@ class FollowCategoriesController < MyProfileController end def new - @follow_category = FollowCategory.new + @follow_category = Circle.new end def create - @follow_category = FollowCategory.new(:name => params[:follow_category][:name], + @follow_category = Circle.new(:name => params[:follow_category][:name], :person => current_person) if @follow_category.save redirect_to :action => 'index' @@ -19,12 +19,13 @@ class FollowCategoriesController < MyProfileController end def edit - @follow_category = FollowCategory.find_by_id(params[:id]) + @follow_category = Circle.find_by_id(params[:id]) render_not_found if @follow_category.nil? end + #TODO FIX METHOD def update - @follow_category = FollowCategory.find_by_id(params[:id]) + @follow_category = Circle.find_by_id(params[:id]) return render_not_found if @follow_category.nil? if @follow_category.update(params[:follow_category]) @@ -35,7 +36,7 @@ class FollowCategoriesController < MyProfileController end def destroy - @follow_category = FollowCategory.find_by_id(params[:id]) + @follow_category = Circle.find_by_id(params[:id]) return render_not_found if @follow_category.nil? if !@follow_category.destroy diff --git a/app/controllers/my_profile/followers_controller.rb b/app/controllers/my_profile/followers_controller.rb index 75d13fa..4c695a9 100644 --- a/app/controllers/my_profile/followers_controller.rb +++ b/app/controllers/my_profile/followers_controller.rb @@ -3,27 +3,21 @@ class FollowersController < MyProfileController before_filter :only_for_person, :only => :index def index - @followed_people = current_person.following_profiles.order(:type).paginate(:per_page => 15, :page => params[:npage]) + @followed_people = current_person.followed_profiles.order(:type).paginate(:per_page => 15, :page => params[:npage]) end def set_category_modal - categories = FollowCategory.where(:person => current_person).map(&:name) + categories = Circle.where(:person => current_person).map(&:name) profile = Profile.find(params[:followed_profile_id]) render :partial => 'blocks/profile_info_actions/follow_categories', :locals => { :categories => categories, :profile => profile } end + #TODO fix def update_category params["followed_profile_id"] ||= profile.id - follower = ProfileFollower.find_by(follower_id: current_person.id, profile_id: params[:followed_profile_id]) if params[:category_name] - category = FollowCategory.find_or_create_by(:name => params[:category_name], :person => current_person) - else - category = nil - end - - if follower - follower.follow_category = category - follower.save + category = Circle.find_or_create_by(:name => params[:category_name], :person => current_person) + follower = ProfileFollower.find_or_create_by(circle: category, profile: Profile.find_by(:id => params[:followed_profile_id])) end redirect_url = params["redirect_to"] ? params["redirect_to"] : url_for(:controller => "followers", :action => "index", :profile => current_person.identifier) diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 92291c8..ab1730f 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -4,7 +4,7 @@ class ProfileController < PublicController before_filter :check_access_to_profile, :except => [:join, :join_not_logged, :index, :add] before_filter :store_location, :only => [:join, :join_not_logged, :report_abuse, :send_mail] before_filter :login_required, :only => [:add, :join, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity, :send_mail, :follow, :unfollow] - before_filter :allow_following?, :only => [:follow, :unfollow] + before_filter :allow_followers?, :only => [:follow, :unfollow] helper TagsHelper helper ActionTrackerHelper @@ -158,10 +158,10 @@ class ProfileController < PublicController def follow if !current_person.follows?(profile) - group = params['follow'] ? params['follow']['category'] : '' - current_person.follow(profile, group) + #group = params['follow'] ? params['follow']['category'] : '' + #current_person.follow(profile, group) - categories = FollowCategory.where(:person => current_person).map(&:name) + categories = Circle.where(:person => current_person).map(&:name) render :partial => 'blocks/profile_info_actions/follow_categories', :locals => { :categories => categories } else render :text => _("It was not possible to follow %s") % profile.name, :status => 400 @@ -462,8 +462,8 @@ class ProfileController < PublicController [:image, :domains, :preferred_domain, :environment] end - def allow_following? - render_not_found unless profile.allow_following? + def allow_followers? + render_not_found unless profile.allow_followers? end end diff --git a/app/models/add_member.rb b/app/models/add_member.rb index ffa2374..cbb5f35 100644 --- a/app/models/add_member.rb +++ b/app/models/add_member.rb @@ -22,7 +22,7 @@ class AddMember < Task self.roles = [Profile::Roles.member(organization.environment.id).id] end target.affiliate(requestor, self.roles.select{|r| !r.to_i.zero? }.map{|i| Role.find(i)}) - person.follow(organization, _('memberships')) + person.follow(organization, Circle.find_or_create_by(:person => person, :name =>_('memberships'))) end def title diff --git a/app/models/circle.rb b/app/models/circle.rb new file mode 100644 index 0000000..73106d7 --- /dev/null +++ b/app/models/circle.rb @@ -0,0 +1,20 @@ +class Circle < ApplicationRecord + has_many :profile_followers + belongs_to :person + + attr_accessible :name, :person, :profile_type + + validates :name, presence: true + validates :person_id, presence: true + + validates :person_id, :uniqueness => {:scope => :name, :message => "can't add two circles with the same name"} + + scope :by_owner, -> person{ + where(:person => person) + } + + scope :with_name, -> name{ + where(:name => name) + } + +end diff --git a/app/models/favorite_enterprise_person.rb b/app/models/favorite_enterprise_person.rb index deb6562..b432d87 100644 --- a/app/models/favorite_enterprise_person.rb +++ b/app/models/favorite_enterprise_person.rb @@ -8,7 +8,7 @@ class FavoriteEnterprisePerson < ApplicationRecord belongs_to :person after_create do |favorite| - favorite.person.follow(favorite.enterprise, _('favorites')) + favorite.person.follow(favotire.enterprise, Circle.find_or_create_by(:person => favorite.person, :name =>_('favorites'))) end protected diff --git a/app/models/follow_category.rb b/app/models/follow_category.rb deleted file mode 100644 index b4dca5d..0000000 --- a/app/models/follow_category.rb +++ /dev/null @@ -1,8 +0,0 @@ -class FollowCategory < ApplicationRecord - belongs_to :environment - has_many :profile_followers - belongs_to :person - - attr_accessible :name, :person - validates :name, presence: true -end diff --git a/app/models/friendship.rb b/app/models/friendship.rb index d3ff315..76ded0a 100644 --- a/app/models/friendship.rb +++ b/app/models/friendship.rb @@ -9,13 +9,15 @@ class Friendship < ApplicationRecord after_create do |friendship| Friendship.update_cache_counter(:friends_count, friendship.person, 1) Friendship.update_cache_counter(:friends_count, friendship.friend, 1) - friendship.person.follow(friendship.friend, friendship.group) + friendship.person.follow(friendship.friend, Circle.find_or_create_by(:person => friendship.person, :name => friendship.group)) end after_destroy do |friendship| Friendship.update_cache_counter(:friends_count, friendship.person, -1) Friendship.update_cache_counter(:friends_count, friendship.friend, -1) - friendship.person.unfollow(friendship.friend) + + circle=Circle.find_by(:person => friendship.person, :name => friendship.group) + friendship.person.remove_profile_from_circle(friendship.friend, circle) if circle end def self.remove_friendship(person1, person2) diff --git a/app/models/person.rb b/app/models/person.rb index 3c83e4c..f5ff215 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -92,7 +92,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 :follow_categories + has_many :circles scope :online, -> { joins(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes) @@ -200,17 +200,33 @@ class Person < Profile end end - def follow(profile, category_name = "") - unless self.following_profiles.include?(profile) - profile_follower = ProfileFollower.new - profile_follower.profile = profile - profile_follower.follower = self - category = FollowCategory.find_or_create_by(:name => category_name, :person => self) - profile_follower.follow_category = category - profile_follower.save + def follow (profile, circles) + circles = [circles] unless circles.is_a?(Array) + circles.each do |new_circle| + ProfileFollower.create(profile: profile, circle: new_circle) end end + def update_profile_circles (profile, new_circles) + circles_to_add = new_circles - self.circles + circles_to_remove = self.circles - new_circles + + circles_to_add.each do |new_circle| + ProfileFollower.create(profile: profile, circle: new_circle) + end + + ProfileFollower.where('circle_id IN (?) AND profile_id = ?', + circles_to_remove.map(&:id), profile.id).destroy_all + end + + def unfollow(profile) + ProfileFollower.with_follower(self).with_profile(profile).destroy_all + end + + def remove_profile_from_circle(profile, circle) + ProfileFollower.with_profile(profile).with_circle(circle).destroy_all + end + def already_request_friendship?(person) person.tasks.where(requestor_id: self.id, type: 'AddFriend', status: Task::Status::ACTIVE).first end @@ -219,11 +235,6 @@ class Person < Profile Friendship.where(friend_id: friend, person_id: id).first.destroy end - def unfollow(profile) - follower = ProfileFollower.where(follower_id: id, profile_id: profile.id) if profile - follower.first.destroy if follower.present? - end - FIELDS = %w[ description image @@ -596,8 +607,8 @@ class Person < Profile person.has_permission?(:manage_friends, self) end - def following_profiles - Profile.following_profiles self + def followed_profiles + Profile.followed_by self end def in_social_circle?(person) diff --git a/app/models/profile.rb b/app/models/profile.rb index 2da0d77..33e13cf 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -5,7 +5,7 @@ class Profile < ApplicationRecord attr_accessible :name, :identifier, :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :environment, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, :redirection_after_login, :custom_url_redirection, - :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification, :allow_following + :email_suggestions, :allow_members_to_invite, :invite_friends_only, :secret, :profile_admin_mail_notification, :allow_followers # use for internationalizable human type names in search facets # reimplement on subclasses @@ -203,15 +203,22 @@ class Profile < ApplicationRecord scope :more_active, -> { order 'activities_count DESC' } scope :more_recent, -> { order "created_at DESC" } - scope :following_profiles, -> person { - distinct.select('profiles.*, follow_categories.name AS category'). - joins('left join profile_followers ON profile_followers.profile_id = profiles.id'). - joins('left join follow_categories ON follow_categories.id = profile_followers.follow_category_id'). - where('profile_followers.follower_id = ?', person.id) + scope :followed_by, -> person{ + distinct.select('profiles.*, circles.name as category'). #TODO remove category from here + 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) } - settings_items :allow_following, :type => :boolean, :default => true - alias_method :allow_following?, :allow_following + 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) + } + + settings_items :allow_followers, :type => :boolean, :default => true + alias_method :allow_followers?, :allow_followers acts_as_trackable :dependent => :destroy @@ -226,7 +233,7 @@ class Profile < ApplicationRecord has_many :email_templates, :foreign_key => :owner_id has_many :profile_followers - has_many :followers, :class_name => 'Person', :through => :profile_followers + has_many :followers, :class_name => 'Person', :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 @@ -777,7 +784,7 @@ private :generate_url, :url_options else self.affiliate(person, Profile::Roles.admin(environment.id), attributes) if members.count == 0 self.affiliate(person, Profile::Roles.member(environment.id), attributes) - person.follow(self, _('memberships')) + person.follow(self, Circle.find_or_create_by(:person => person, :name =>_('memberships'))) end person.tasks.pending.of("InviteMember").select { |t| t.data[:community_id] == self.id }.each { |invite| invite.cancel } remove_from_suggestion_list person diff --git a/app/models/profile_follower.rb b/app/models/profile_follower.rb index c35b199..4bf1698 100644 --- a/app/models/profile_follower.rb +++ b/app/models/profile_follower.rb @@ -1,13 +1,28 @@ class ProfileFollower < ApplicationRecord + self.table_name = :profiles_circles track_actions :new_follower, :after_create, :keep_params => ["follower.name", "follower.url", "follower.profile_custom_icon"], :custom_user => :profile - attr_accessible :profile, :follower, :follow_category + attr_accessible :profile, :circle - belongs_to :profile, :foreign_key => :profile_id - belongs_to :follower, :class_name => 'Person', :foreign_key => :follower_id - belongs_to :follow_category, :foreign_key => :follow_category_id + belongs_to :profile + belongs_to :circle - validates_presence_of :profile_id, :follower_id - validates :profile_id, :uniqueness => {:scope => :follower_id, :message => "can't follow the same profile twice"} + has_one :person, through: :circle + alias follower person + + 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"} + + scope :with_follower, -> person{ + joins(:circle).where('circles.person_id = ?', person.id) + } + + scope :with_profile, -> profile{ + where(:profile => profile) + } + + scope :with_circle, -> circle{ + where(:circle => circle) + } end diff --git a/app/views/blocks/profile_info_actions/_common.html.erb b/app/views/blocks/profile_info_actions/_common.html.erb index 49baca6..d080c98 100644 --- a/app/views/blocks/profile_info_actions/_common.html.erb +++ b/app/views/blocks/profile_info_actions/_common.html.erb @@ -1,5 +1,5 @@
- <%= profile.category %> + <%= profile.category #TODO get categories through scope%>