Commit 9b0cf644403e5136e9c7a1927782b43fe2d20970

Authored by Dylan Guedes
1 parent f919a26e

Start following for external noosfero users

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Signed-off-by: DylanGuedes <djmgguedes@gmail.com>
app/controllers/concerns/needs_profile.rb
@@ -32,6 +32,8 @@ module NeedsProfile @@ -32,6 +32,8 @@ module NeedsProfile
32 params.delete(:profile) 32 params.delete(:profile)
33 redirect_to(Noosfero.url_options.merge(params).merge(:host => profile_hostname)) 33 redirect_to(Noosfero.url_options.merge(params).merge(:host => profile_hostname))
34 end 34 end
  35 + elsif session[:external]
  36 + @profile = ExternalPerson.find(session[:external])
35 else 37 else
36 render_not_found 38 render_not_found
37 end 39 end
app/models/circle.rb
1 class Circle < ApplicationRecord 1 class Circle < ApplicationRecord
2 has_many :profile_followers 2 has_many :profile_followers
3 - belongs_to :person 3 + belongs_to :person, polymorphic: true
4 4
5 attr_accessible :name, :person, :profile_type 5 attr_accessible :name, :person, :profile_type
6 6
app/models/external_person.rb
@@ -4,6 +4,8 @@ class ExternalPerson &lt; ActiveRecord::Base @@ -4,6 +4,8 @@ class ExternalPerson &lt; ActiveRecord::Base
4 include Human 4 include Human
5 include ProfileEntity 5 include ProfileEntity
6 6
  7 + has_many :circles, as: :person
  8 + has_many :profile_followers, as: :profile
7 validates_uniqueness_of :identifier, scope: :source 9 validates_uniqueness_of :identifier, scope: :source
8 10
9 validates_presence_of :source, :email, :created_at 11 validates_presence_of :source, :email, :created_at
@@ -277,6 +279,13 @@ class ExternalPerson &lt; ActiveRecord::Base @@ -277,6 +279,13 @@ class ExternalPerson &lt; ActiveRecord::Base
277 super 279 super
278 end 280 end
279 281
  282 + def follow(profile, circles)
  283 + circles = [circles] unless circles.is_a?(Array)
  284 + circles.each do |new_circle|
  285 + ProfileFollower.create(profile: profile, circle: new_circle)
  286 + end
  287 + end
  288 +
280 private 289 private
281 290
282 def generate_derivated_methods(methods) 291 def generate_derivated_methods(methods)
app/models/person.rb
@@ -93,7 +93,7 @@ class Person &lt; Profile @@ -93,7 +93,7 @@ class Person &lt; Profile
93 has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article 93 has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article
94 has_many :friendships, :dependent => :destroy 94 has_many :friendships, :dependent => :destroy
95 has_many :friends, :class_name => 'Person', :through => :friendships 95 has_many :friends, :class_name => 'Person', :through => :friendships
96 - has_many :circles 96 + has_many :circles, as: :person
97 97
98 scope :online, -> { 98 scope :online, -> {
99 joins(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes) 99 joins(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes)
app/models/profile.rb
@@ -248,7 +248,7 @@ class Profile &lt; ApplicationRecord @@ -248,7 +248,7 @@ class Profile &lt; ApplicationRecord
248 248
249 has_many :email_templates, :foreign_key => :owner_id 249 has_many :email_templates, :foreign_key => :owner_id
250 250
251 - has_many :profile_followers 251 + has_many :profile_followers, as: :profile
252 has_many :followers, -> { uniq }, :class_name => 'Person', :through => :profile_followers, :source => :person 252 has_many :followers, -> { uniq }, :class_name => 'Person', :through => :profile_followers, :source => :person
253 253
254 # Although this should be a has_one relation, there are no non-silly names for 254 # Although this should be a has_one relation, there are no non-silly names for
@@ -1009,6 +1009,7 @@ private :generate_url, :url_options @@ -1009,6 +1009,7 @@ private :generate_url, :url_options
1009 end 1009 end
1010 1010
1011 def followed_by?(person) 1011 def followed_by?(person)
  1012 + p "self: #{self}"
1012 (person == self) || (person.in? self.followers) 1013 (person == self) || (person.in? self.followers)
1013 end 1014 end
1014 1015
app/models/profile_follower.rb
@@ -4,10 +4,11 @@ class ProfileFollower &lt; ApplicationRecord @@ -4,10 +4,11 @@ class ProfileFollower &lt; ApplicationRecord
4 4
5 attr_accessible :profile, :circle 5 attr_accessible :profile, :circle
6 6
7 - belongs_to :profile 7 + belongs_to :profile, polymorphic: :true
8 belongs_to :circle 8 belongs_to :circle
9 9
10 - has_one :person, through: :circle 10 + has_one :person, through: :circle, source_type: "ProfileFollower"
  11 +
11 alias follower person 12 alias follower person
12 13
13 validates_presence_of :profile_id, :circle_id 14 validates_presence_of :profile_id, :circle_id
@@ -25,4 +26,5 @@ class ProfileFollower &lt; ApplicationRecord @@ -25,4 +26,5 @@ class ProfileFollower &lt; ApplicationRecord
25 where(:circle => circle) 26 where(:circle => circle)
26 } 27 }
27 28
  29 +
28 end 30 end
db/migrate/20160810131138_adds_person_type_to_circles.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class AddsPersonTypeToCircles < ActiveRecord::Migration
  2 + def up
  3 + add_column :circles, :person_type, :string, default: "Person"
  4 + add_index :circles, [:person_id, :person_type]
  5 + end
  6 +
  7 + def down
  8 + remove_column :circles, :person_type
  9 + remove_index :circles, [:person_id, :person_type]
  10 + end
  11 +end
db/migrate/20160810132802_add_profile_type_to_profile_followers.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class AddProfileTypeToProfileFollowers < ActiveRecord::Migration
  2 + def up
  3 + add_column :profiles_circles, :profile_type, :string, default: "Profile"
  4 + add_index :profiles_circles, [:profile_id, :profile_type]
  5 + end
  6 +
  7 + def down
  8 + remove_column :profiles_circles, :profile_type
  9 + remove_index :profiles_circles, [:profile_id, :profile_type]
  10 + end
  11 +end
vendor/plugins/action_tracker/lib/action_tracker.rb
@@ -95,8 +95,17 @@ module ActionTracker @@ -95,8 +95,17 @@ module ActionTracker
95 return nil if user.nil? 95 return nil if user.nil?
96 if keep_params.is_a? Array 96 if keep_params.is_a? Array
97 stored_params = {} 97 stored_params = {}
  98 + p "&"*40
  99 + p "keep_params: #{keep_params}"
  100 + p "&"*40
98 keep_params.each do |param| 101 keep_params.each do |param|
99 result = self 102 result = self
  103 + p "*"*40
  104 + p "result: #{result}"
  105 + p "result.follower:"
  106 + p result.circle.person
  107 + p "&"*40
  108 + p "*"*40
100 param.to_s.split('.').each { |m| result = result.send(m) } 109 param.to_s.split('.').each { |m| result = result.send(m) }
101 stored_params[param.to_s.gsub(/\./, '_')] = result 110 stored_params[param.to_s.gsub(/\./, '_')] = result
102 end 111 end
@@ -114,6 +123,9 @@ module ActionTracker @@ -114,6 +123,9 @@ module ActionTracker
114 Record.new :verb => verb, :params => stored_params, :user => user 123 Record.new :verb => verb, :params => stored_params, :user => user
115 end 124 end
116 tracked_action.target = target || self 125 tracked_action.target = target || self
  126 + p "*"*40
  127 + p "user: #{user}"
  128 + p "*"*40
117 user.tracked_actions << tracked_action 129 user.tracked_actions << tracked_action
118 post_proc.call tracked_action 130 post_proc.call tracked_action
119 end 131 end