Commit 6e677d68b1a13e42fa0058e476ce97f3ae68c551

Authored by Dylan Guedes
1 parent 9b0cf644

Adds polymorphic relationship between circle and person.

Signed-off-by: DylanGuedes <djmgguedes@gmail.com>
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
app/models/circle.rb
1 class Circle < ApplicationRecord 1 class Circle < ApplicationRecord
  2 + #TODO -> n:m with profile, item in the circle
2 has_many :profile_followers 3 has_many :profile_followers
  4 + #TODO -> owner
3 belongs_to :person, polymorphic: true 5 belongs_to :person, polymorphic: true
4 6
5 attr_accessible :name, :person, :profile_type 7 attr_accessible :name, :person, :profile_type
app/models/external_person.rb
@@ -5,7 +5,6 @@ class ExternalPerson &lt; ActiveRecord::Base @@ -5,7 +5,6 @@ class ExternalPerson &lt; ActiveRecord::Base
5 include ProfileEntity 5 include ProfileEntity
6 6
7 has_many :circles, as: :person 7 has_many :circles, as: :person
8 - has_many :profile_followers, as: :profile  
9 validates_uniqueness_of :identifier, scope: :source 8 validates_uniqueness_of :identifier, scope: :source
10 9
11 validates_presence_of :source, :email, :created_at 10 validates_presence_of :source, :email, :created_at
app/models/friendship.rb
@@ -10,6 +10,9 @@ class Friendship &lt; ApplicationRecord @@ -10,6 +10,9 @@ class Friendship &lt; ApplicationRecord
10 Friendship.update_cache_counter(:friends_count, friendship.person, 1) 10 Friendship.update_cache_counter(:friends_count, friendship.person, 1)
11 Friendship.update_cache_counter(:friends_count, friendship.friend, 1) 11 Friendship.update_cache_counter(:friends_count, friendship.friend, 1)
12 12
  13 + p "*"*60
  14 + p "BEFORE CIRCLES"
  15 + p "*"*60
13 circles = friendship.group.blank? ? ['friendships'] : friendship.group.split(',').map(&:strip) 16 circles = friendship.group.blank? ? ['friendships'] : friendship.group.split(',').map(&:strip)
14 circles.each do |circle| 17 circles.each do |circle|
15 friendship.person.follow(friendship.friend, Circle.find_or_create_by(:person => friendship.person, :name => circle, :profile_type => 'Person')) 18 friendship.person.follow(friendship.friend, Circle.find_or_create_by(:person => friendship.person, :name => circle, :profile_type => 'Person'))
app/models/profile.rb
@@ -248,8 +248,26 @@ class Profile &lt; ApplicationRecord @@ -248,8 +248,26 @@ 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, as: :profile  
252 - has_many :followers, -> { uniq }, :class_name => 'Person', :through => :profile_followers, :source => :person 251 + has_many :profile_followers
  252 + scope :memberships_of, -> person {
  253 + distinct.select('profiles.*').
  254 + joins(:role_assignments).
  255 + where('role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id)
  256 + }
  257 +
  258 + def in_circles
  259 + Circle.joins(:profile_followers).
  260 + where('profiles_circles.profile_id = ?', self.id)
  261 + end
  262 +
  263 + def followers
  264 + person_followers = Person.joins(:circles).merge(in_circles)
  265 + external_person_followers = ExternalPerson.joins(:circles).merge(in_circles)
  266 +
  267 + person_followers+external_person_followers
  268 + end
  269 +
  270 + # has_many :followers, -> { uniq }, :through => :profile_followers, :source => :person
253 271
254 # Although this should be a has_one relation, there are no non-silly names for 272 # Although this should be a has_one relation, there are no non-silly names for
255 # a foreign key on article to reference the template to which it is 273 # a foreign key on article to reference the template to which it is
app/models/profile_follower.rb
@@ -4,12 +4,18 @@ class ProfileFollower &lt; ApplicationRecord @@ -4,12 +4,18 @@ class ProfileFollower &lt; ApplicationRecord
4 4
5 attr_accessible :profile, :circle 5 attr_accessible :profile, :circle
6 6
7 - belongs_to :profile, polymorphic: :true 7 + #TODO -> user being followed
  8 + belongs_to :profile
8 belongs_to :circle 9 belongs_to :circle
9 10
10 - has_one :person, through: :circle, source_type: "ProfileFollower" 11 + #TODO -> circle owner
  12 + # has_one :person, through: :circle, source_type: "ProfileFollower"
11 13
12 - alias follower person 14 + def circle_owner
  15 + self.circle.person
  16 + end
  17 +
  18 + alias follower circle_owner
13 19
14 validates_presence_of :profile_id, :circle_id 20 validates_presence_of :profile_id, :circle_id
15 validates :profile_id, :uniqueness => {:scope => :circle_id, :message => "can't put a profile in the same circle twice"} 21 validates :profile_id, :uniqueness => {:scope => :circle_id, :message => "can't put a profile in the same circle twice"}
app/views/blocks/profile_info_actions/_common.html.erb
1 <li><%= report_abuse(profile, :button) %></li> 1 <li><%= report_abuse(profile, :button) %></li>
2 <% if logged_in? && (user != profile) && profile.allow_followers? %> 2 <% if logged_in? && (user != profile) && profile.allow_followers? %>
3 <li> 3 <li>
4 - <% follow = user.follows?(profile) %> 4 + <% follow = profile.followed_by user %>
5 <%= button(:unfollow, content_tag('span', _('Unfollow')), {:profile => profile.identifier, :controller => 'profile', :action => 'unfollow'}, :method => :post, :id => 'action-unfollow', :title => _("Unfollow"), :style => follow ? "" : "display: none;") %> 5 <%= button(:unfollow, content_tag('span', _('Unfollow')), {:profile => profile.identifier, :controller => 'profile', :action => 'unfollow'}, :method => :post, :id => 'action-unfollow', :title => _("Unfollow"), :style => follow ? "" : "display: none;") %>
6 <%= button(:ok, content_tag('span', _('Follow')), {:profile => profile.identifier, :controller => 'profile', :action => 'find_profile_circles'}, :id => 'action-follow', :title => _("Follow"), :style => follow ? "display: none;" : "") %> 6 <%= button(:ok, content_tag('span', _('Follow')), {:profile => profile.identifier, :controller => 'profile', :action => 'find_profile_circles'}, :id => 'action-follow', :title => _("Follow"), :style => follow ? "display: none;" : "") %>
7 <div id="circles-container" style="display: none;"> 7 <div id="circles-container" style="display: none;">
db/migrate/20160810132802_add_profile_type_to_profile_followers.rb
@@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
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  
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 # 11 #
12 # It's strongly recommended that you check this file into your version control system. 12 # It's strongly recommended that you check this file into your version control system.
13 13
14 -ActiveRecord::Schema.define(version: 20160608123748) do 14 +ActiveRecord::Schema.define(version: 20160810132802) do
15 15
16 # These are extensions that must be enabled in order to support this database 16 # These are extensions that must be enabled in order to support this database
17 enable_extension "plpgsql" 17 enable_extension "plpgsql"
@@ -278,10 +278,12 @@ ActiveRecord::Schema.define(version: 20160608123748) do @@ -278,10 +278,12 @@ ActiveRecord::Schema.define(version: 20160608123748) do
278 create_table "circles", force: :cascade do |t| 278 create_table "circles", force: :cascade do |t|
279 t.string "name" 279 t.string "name"
280 t.integer "person_id" 280 t.integer "person_id"
281 - t.string "profile_type", null: false 281 + t.string "profile_type", null: false
  282 + t.string "person_type", default: "Person"
282 end 283 end
283 284
284 add_index "circles", ["person_id", "name"], name: "circles_composite_key_index", unique: true, using: :btree 285 add_index "circles", ["person_id", "name"], name: "circles_composite_key_index", unique: true, using: :btree
  286 + add_index "circles", ["person_id", "person_type"], name: "index_circles_on_person_id_and_person_type", using: :btree
285 287
286 create_table "comments", force: :cascade do |t| 288 create_table "comments", force: :cascade do |t|
287 t.string "title" 289 t.string "title"
@@ -553,6 +555,11 @@ ActiveRecord::Schema.define(version: 20160608123748) do @@ -553,6 +555,11 @@ ActiveRecord::Schema.define(version: 20160608123748) do
553 t.datetime "updated_at" 555 t.datetime "updated_at"
554 end 556 end
555 557
  558 + create_table "private_scraps", force: :cascade do |t|
  559 + t.integer "person_id"
  560 + t.integer "scrap_id"
  561 + end
  562 +
556 create_table "product_qualifiers", force: :cascade do |t| 563 create_table "product_qualifiers", force: :cascade do |t|
557 t.integer "product_id" 564 t.integer "product_id"
558 t.integer "qualifier_id" 565 t.integer "qualifier_id"
@@ -681,9 +688,11 @@ ActiveRecord::Schema.define(version: 20160608123748) do @@ -681,9 +688,11 @@ ActiveRecord::Schema.define(version: 20160608123748) do
681 t.integer "circle_id" 688 t.integer "circle_id"
682 t.datetime "created_at" 689 t.datetime "created_at"
683 t.datetime "updated_at" 690 t.datetime "updated_at"
  691 + t.string "profile_type", default: "Profile"
684 end 692 end
685 693
686 add_index "profiles_circles", ["profile_id", "circle_id"], name: "profiles_circles_composite_key_index", unique: true, using: :btree 694 add_index "profiles_circles", ["profile_id", "circle_id"], name: "profiles_circles_composite_key_index", unique: true, using: :btree
  695 + add_index "profiles_circles", ["profile_id", "profile_type"], name: "index_profiles_circles_on_profile_id_and_profile_type", using: :btree
687 696
688 create_table "qualifier_certifiers", force: :cascade do |t| 697 create_table "qualifier_certifiers", force: :cascade do |t|
689 t.integer "qualifier_id" 698 t.integer "qualifier_id"
vendor/plugins/action_tracker/lib/action_tracker.rb
@@ -100,12 +100,6 @@ module ActionTracker @@ -100,12 +100,6 @@ module ActionTracker
100 p "&"*40 100 p "&"*40
101 keep_params.each do |param| 101 keep_params.each do |param|
102 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  
109 param.to_s.split('.').each { |m| result = result.send(m) } 103 param.to_s.split('.').each { |m| result = result.send(m) }
110 stored_params[param.to_s.gsub(/\./, '_')] = result 104 stored_params[param.to_s.gsub(/\./, '_')] = result
111 end 105 end