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 32 params.delete(:profile)
33 33 redirect_to(Noosfero.url_options.merge(params).merge(:host => profile_hostname))
34 34 end
  35 + elsif session[:external]
  36 + @profile = ExternalPerson.find(session[:external])
35 37 else
36 38 render_not_found
37 39 end
... ...
app/models/circle.rb
1 1 class Circle < ApplicationRecord
2 2 has_many :profile_followers
3   - belongs_to :person
  3 + belongs_to :person, polymorphic: true
4 4  
5 5 attr_accessible :name, :person, :profile_type
6 6  
... ...
app/models/external_person.rb
... ... @@ -4,6 +4,8 @@ class ExternalPerson &lt; ActiveRecord::Base
4 4 include Human
5 5 include ProfileEntity
6 6  
  7 + has_many :circles, as: :person
  8 + has_many :profile_followers, as: :profile
7 9 validates_uniqueness_of :identifier, scope: :source
8 10  
9 11 validates_presence_of :source, :email, :created_at
... ... @@ -277,6 +279,13 @@ class ExternalPerson &lt; ActiveRecord::Base
277 279 super
278 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 289 private
281 290  
282 291 def generate_derivated_methods(methods)
... ...
app/models/person.rb
... ... @@ -93,7 +93,7 @@ class Person &lt; Profile
93 93 has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article
94 94 has_many :friendships, :dependent => :destroy
95 95 has_many :friends, :class_name => 'Person', :through => :friendships
96   - has_many :circles
  96 + has_many :circles, as: :person
97 97  
98 98 scope :online, -> {
99 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 248  
249 249 has_many :email_templates, :foreign_key => :owner_id
250 250  
251   - has_many :profile_followers
  251 + has_many :profile_followers, as: :profile
252 252 has_many :followers, -> { uniq }, :class_name => 'Person', :through => :profile_followers, :source => :person
253 253  
254 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 1009 end
1010 1010  
1011 1011 def followed_by?(person)
  1012 + p "self: #{self}"
1012 1013 (person == self) || (person.in? self.followers)
1013 1014 end
1014 1015  
... ...
app/models/profile_follower.rb
... ... @@ -4,10 +4,11 @@ class ProfileFollower &lt; ApplicationRecord
4 4  
5 5 attr_accessible :profile, :circle
6 6  
7   - belongs_to :profile
  7 + belongs_to :profile, polymorphic: :true
8 8 belongs_to :circle
9 9  
10   - has_one :person, through: :circle
  10 + has_one :person, through: :circle, source_type: "ProfileFollower"
  11 +
11 12 alias follower person
12 13  
13 14 validates_presence_of :profile_id, :circle_id
... ... @@ -25,4 +26,5 @@ class ProfileFollower &lt; ApplicationRecord
25 26 where(:circle => circle)
26 27 }
27 28  
  29 +
28 30 end
... ...
db/migrate/20160810131138_adds_person_type_to_circles.rb 0 → 100644
... ... @@ -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 @@
  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 95 return nil if user.nil?
96 96 if keep_params.is_a? Array
97 97 stored_params = {}
  98 + p "&"*40
  99 + p "keep_params: #{keep_params}"
  100 + p "&"*40
98 101 keep_params.each do |param|
99 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 109 param.to_s.split('.').each { |m| result = result.send(m) }
101 110 stored_params[param.to_s.gsub(/\./, '_')] = result
102 111 end
... ... @@ -114,6 +123,9 @@ module ActionTracker
114 123 Record.new :verb => verb, :params => stored_params, :user => user
115 124 end
116 125 tracked_action.target = target || self
  126 + p "*"*40
  127 + p "user: #{user}"
  128 + p "*"*40
117 129 user.tracked_actions << tracked_action
118 130 post_proc.call tracked_action
119 131 end
... ...