From 9b0cf644403e5136e9c7a1927782b43fe2d20970 Mon Sep 17 00:00:00 2001 From: DylanGuedes Date: Wed, 10 Aug 2016 12:20:59 -0300 Subject: [PATCH] Start following for external noosfero users --- app/controllers/concerns/needs_profile.rb | 2 ++ app/models/circle.rb | 2 +- app/models/external_person.rb | 9 +++++++++ app/models/person.rb | 2 +- app/models/profile.rb | 3 ++- app/models/profile_follower.rb | 6 ++++-- db/migrate/20160810131138_adds_person_type_to_circles.rb | 11 +++++++++++ db/migrate/20160810132802_add_profile_type_to_profile_followers.rb | 11 +++++++++++ vendor/plugins/action_tracker/lib/action_tracker.rb | 12 ++++++++++++ 9 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20160810131138_adds_person_type_to_circles.rb create mode 100644 db/migrate/20160810132802_add_profile_type_to_profile_followers.rb diff --git a/app/controllers/concerns/needs_profile.rb b/app/controllers/concerns/needs_profile.rb index 8048f4c..ff15d95 100644 --- a/app/controllers/concerns/needs_profile.rb +++ b/app/controllers/concerns/needs_profile.rb @@ -32,6 +32,8 @@ module NeedsProfile params.delete(:profile) redirect_to(Noosfero.url_options.merge(params).merge(:host => profile_hostname)) end + elsif session[:external] + @profile = ExternalPerson.find(session[:external]) else render_not_found end diff --git a/app/models/circle.rb b/app/models/circle.rb index d5e5478..722db33 100644 --- a/app/models/circle.rb +++ b/app/models/circle.rb @@ -1,6 +1,6 @@ class Circle < ApplicationRecord has_many :profile_followers - belongs_to :person + 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 7e9a11e..bbf1cc3 100644 --- a/app/models/external_person.rb +++ b/app/models/external_person.rb @@ -4,6 +4,8 @@ class ExternalPerson < ActiveRecord::Base include Human 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 @@ -277,6 +279,13 @@ class ExternalPerson < ActiveRecord::Base super end + 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 + private def generate_derivated_methods(methods) diff --git a/app/models/person.rb b/app/models/person.rb index 8c420ed..62a1100 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -93,7 +93,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 + has_many :circles, as: :person 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 9bce4e4..0f18e5c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -248,7 +248,7 @@ class Profile < ApplicationRecord has_many :email_templates, :foreign_key => :owner_id - has_many :profile_followers + has_many :profile_followers, as: :profile has_many :followers, -> { uniq }, :class_name => 'Person', :through => :profile_followers, :source => :person # Although this should be a has_one relation, there are no non-silly names for @@ -1009,6 +1009,7 @@ private :generate_url, :url_options end def followed_by?(person) + p "self: #{self}" (person == self) || (person.in? self.followers) end diff --git a/app/models/profile_follower.rb b/app/models/profile_follower.rb index 4bf1698..2c539cf 100644 --- a/app/models/profile_follower.rb +++ b/app/models/profile_follower.rb @@ -4,10 +4,11 @@ class ProfileFollower < ApplicationRecord attr_accessible :profile, :circle - belongs_to :profile + belongs_to :profile, polymorphic: :true belongs_to :circle - has_one :person, through: :circle + has_one :person, through: :circle, source_type: "ProfileFollower" + alias follower person validates_presence_of :profile_id, :circle_id @@ -25,4 +26,5 @@ class ProfileFollower < ApplicationRecord where(:circle => circle) } + end diff --git a/db/migrate/20160810131138_adds_person_type_to_circles.rb b/db/migrate/20160810131138_adds_person_type_to_circles.rb new file mode 100644 index 0000000..82f68cc --- /dev/null +++ b/db/migrate/20160810131138_adds_person_type_to_circles.rb @@ -0,0 +1,11 @@ +class AddsPersonTypeToCircles < ActiveRecord::Migration + def up + add_column :circles, :person_type, :string, default: "Person" + add_index :circles, [:person_id, :person_type] + end + + def down + remove_column :circles, :person_type + remove_index :circles, [:person_id, :person_type] + end +end diff --git a/db/migrate/20160810132802_add_profile_type_to_profile_followers.rb b/db/migrate/20160810132802_add_profile_type_to_profile_followers.rb new file mode 100644 index 0000000..e5f4110 --- /dev/null +++ b/db/migrate/20160810132802_add_profile_type_to_profile_followers.rb @@ -0,0 +1,11 @@ +class AddProfileTypeToProfileFollowers < ActiveRecord::Migration + def up + add_column :profiles_circles, :profile_type, :string, default: "Profile" + add_index :profiles_circles, [:profile_id, :profile_type] + end + + def down + remove_column :profiles_circles, :profile_type + remove_index :profiles_circles, [:profile_id, :profile_type] + end +end diff --git a/vendor/plugins/action_tracker/lib/action_tracker.rb b/vendor/plugins/action_tracker/lib/action_tracker.rb index ef8e414..4b5ae86 100644 --- a/vendor/plugins/action_tracker/lib/action_tracker.rb +++ b/vendor/plugins/action_tracker/lib/action_tracker.rb @@ -95,8 +95,17 @@ module ActionTracker return nil if user.nil? if keep_params.is_a? Array stored_params = {} + p "&"*40 + p "keep_params: #{keep_params}" + p "&"*40 keep_params.each do |param| result = self + p "*"*40 + p "result: #{result}" + p "result.follower:" + p result.circle.person + p "&"*40 + p "*"*40 param.to_s.split('.').each { |m| result = result.send(m) } stored_params[param.to_s.gsub(/\./, '_')] = result end @@ -114,6 +123,9 @@ module ActionTracker Record.new :verb => verb, :params => stored_params, :user => user end tracked_action.target = target || self + p "*"*40 + p "user: #{user}" + p "*"*40 user.tracked_actions << tracked_action post_proc.call tracked_action end -- libgit2 0.21.2