Commit fab2d51bb06180a7d1467ba1f7c6888ae154d156
1 parent
34b8a866
Refactos ProfileFollower relation
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Showing
12 changed files
with
49 additions
and
35 deletions
Show diff stats
app/controllers/my_profile/circles_controller.rb
@@ -3,7 +3,7 @@ class CirclesController < MyProfileController | @@ -3,7 +3,7 @@ class CirclesController < MyProfileController | ||
3 | before_action :accept_only_post, :only => [:create, :update, :destroy] | 3 | before_action :accept_only_post, :only => [:create, :update, :destroy] |
4 | 4 | ||
5 | def index | 5 | def index |
6 | - @circles = profile.circles | 6 | + @circles = profile.owned_circles |
7 | end | 7 | end |
8 | 8 | ||
9 | def new | 9 | def new |
@@ -11,7 +11,7 @@ class CirclesController < MyProfileController | @@ -11,7 +11,7 @@ class CirclesController < MyProfileController | ||
11 | end | 11 | end |
12 | 12 | ||
13 | def create | 13 | def create |
14 | - @circle = Circle.new(params[:circle].merge({ :person => profile })) | 14 | + @circle = Circle.new(params[:circle].merge({ :owner => profile })) |
15 | if @circle.save | 15 | if @circle.save |
16 | redirect_to :action => 'index' | 16 | redirect_to :action => 'index' |
17 | else | 17 | else |
@@ -21,7 +21,7 @@ class CirclesController < MyProfileController | @@ -21,7 +21,7 @@ class CirclesController < MyProfileController | ||
21 | 21 | ||
22 | def xhr_create | 22 | def xhr_create |
23 | if request.xhr? | 23 | if request.xhr? |
24 | - circle = Circle.new(params[:circle].merge({:person => profile })) | 24 | + circle = Circle.new(params[:circle].merge({:owner => profile })) |
25 | if circle.save | 25 | if circle.save |
26 | render :partial => "circle_checkbox", :locals => { :circle => circle }, | 26 | render :partial => "circle_checkbox", :locals => { :circle => circle }, |
27 | :status => 201 | 27 | :status => 201 |
app/controllers/my_profile/followers_controller.rb
@@ -17,7 +17,7 @@ class FollowersController < MyProfileController | @@ -17,7 +17,7 @@ class FollowersController < MyProfileController | ||
17 | 17 | ||
18 | def set_category_modal | 18 | def set_category_modal |
19 | followed_profile = Profile.find(params[:followed_profile_id]) | 19 | followed_profile = Profile.find(params[:followed_profile_id]) |
20 | - circles = Circle.where(:person => profile, :profile_type => followed_profile.class.name) | 20 | + circles = Circle.where(:owner => profile, :profile_type => followed_profile.class.name) |
21 | render :partial => 'followers/edit_circles_modal', :locals => { :circles => circles, :followed_profile => followed_profile } | 21 | render :partial => 'followers/edit_circles_modal', :locals => { :circles => circles, :followed_profile => followed_profile } |
22 | end | 22 | end |
23 | 23 |
app/controllers/public/profile_controller.rb
@@ -176,7 +176,7 @@ class ProfileController < PublicController | @@ -176,7 +176,7 @@ class ProfileController < PublicController | ||
176 | end | 176 | end |
177 | 177 | ||
178 | def find_profile_circles | 178 | def find_profile_circles |
179 | - circles = Circle.where(:person => current_person, :profile_type => profile.class.name) | 179 | + circles = Circle.where(:owner => current_person, :profile_type => profile.class.name) |
180 | render :partial => 'blocks/profile_info_actions/circles', :locals => { :circles => circles, :profile_types => Circle.profile_types.to_a } | 180 | render :partial => 'blocks/profile_info_actions/circles', :locals => { :circles => circles, :profile_types => Circle.profile_types.to_a } |
181 | end | 181 | end |
182 | 182 |
app/models/circle.rb
1 | class Circle < ApplicationRecord | 1 | class Circle < ApplicationRecord |
2 | #TODO -> n:m with profile, item in the circle | 2 | #TODO -> n:m with profile, item in the circle |
3 | - has_many :profile_followers | 3 | + has_many :profile, :through => :profile_follower |
4 | #TODO -> owner | 4 | #TODO -> owner |
5 | - belongs_to :person, polymorphic: true | 5 | + belongs_to :owner, polymorphic: true |
6 | 6 | ||
7 | - attr_accessible :name, :person, :profile_type | 7 | + attr_accessible :name, :owner, :profile_type |
8 | 8 | ||
9 | validates :name, presence: true | 9 | validates :name, presence: true |
10 | - validates :person_id, presence: true | 10 | + validates :owner_id, presence: true |
11 | validates :profile_type, presence: true | 11 | validates :profile_type, presence: true |
12 | - validates :person_id, :uniqueness => {:scope => :name, :message => "can't add two circles with the same name"} | 12 | + validates :owner_id, :uniqueness => {:scope => :name, :message => "can't add two circles with the same name"} |
13 | 13 | ||
14 | validate :profile_type_must_be_in_list | 14 | validate :profile_type_must_be_in_list |
15 | 15 |
app/models/concerns/follower.rb
@@ -4,7 +4,7 @@ module Follower | @@ -4,7 +4,7 @@ module Follower | ||
4 | def follow(profile, circles) | 4 | def follow(profile, circles) |
5 | circles = [circles] unless circles.is_a?(Array) | 5 | circles = [circles] unless circles.is_a?(Array) |
6 | circles.each do |new_circle| | 6 | circles.each do |new_circle| |
7 | - next if new_circle.person != self || new_circle.profile_type != profile.class.name | 7 | + next if new_circle.owner != self || new_circle.profile_type != profile.class.name |
8 | ProfileFollower.create(profile: profile, circle: new_circle) | 8 | ProfileFollower.create(profile: profile, circle: new_circle) |
9 | end | 9 | end |
10 | end | 10 | end |
@@ -33,7 +33,7 @@ module Follower | @@ -33,7 +33,7 @@ module Follower | ||
33 | end | 33 | end |
34 | 34 | ||
35 | def remove_profile_from_circle(profile, circle) | 35 | def remove_profile_from_circle(profile, circle) |
36 | - return if circle.person != self | 36 | + return if circle.owner != self |
37 | ProfileFollower.with_profile(profile).with_circle(circle).destroy_all | 37 | ProfileFollower.with_profile(profile).with_circle(circle).destroy_all |
38 | end | 38 | end |
39 | 39 |
app/models/external_person.rb
@@ -5,7 +5,10 @@ class ExternalPerson < ActiveRecord::Base | @@ -5,7 +5,10 @@ class ExternalPerson < ActiveRecord::Base | ||
5 | include ProfileEntity | 5 | include ProfileEntity |
6 | include Follower | 6 | include Follower |
7 | 7 | ||
8 | - has_many :circles, as: :person | 8 | + has_many :profile_followers |
9 | + has_many :circles, :through => :profile_followers, :as => :profile | ||
10 | + has_many :owned_circles, as: :owner, :class_name => "Circle" | ||
11 | + | ||
9 | validates_uniqueness_of :identifier, scope: :source | 12 | validates_uniqueness_of :identifier, scope: :source |
10 | 13 | ||
11 | validates_presence_of :source, :email, :created_at | 14 | validates_presence_of :source, :email, :created_at |
app/models/person.rb
@@ -94,7 +94,7 @@ class Person < Profile | @@ -94,7 +94,7 @@ class Person < Profile | ||
94 | has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article | 94 | has_many :following_articles, :class_name => 'Article', :through => :article_followers, :source => :article |
95 | has_many :friendships, :dependent => :destroy | 95 | has_many :friendships, :dependent => :destroy |
96 | has_many :friends, :class_name => 'Person', :through => :friendships | 96 | has_many :friends, :class_name => 'Person', :through => :friendships |
97 | - has_many :circles, as: :person | 97 | + has_many :owned_circles, as: :owner, :class_name => 'Circle' |
98 | 98 | ||
99 | scope :online, -> { | 99 | scope :online, -> { |
100 | joins(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes) | 100 | joins(:user).where("users.chat_status != '' AND users.chat_status_at >= ?", DateTime.now - User.expires_chat_status_every.minutes) |
app/models/profile.rb
@@ -223,14 +223,7 @@ class Profile < ApplicationRecord | @@ -223,14 +223,7 @@ class Profile < ApplicationRecord | ||
223 | distinct.select('profiles.*'). | 223 | distinct.select('profiles.*'). |
224 | joins('left join profiles_circles ON profiles_circles.profile_id = profiles.id'). | 224 | joins('left join profiles_circles ON profiles_circles.profile_id = profiles.id'). |
225 | joins('left join circles ON circles.id = profiles_circles.circle_id'). | 225 | joins('left join circles ON circles.id = profiles_circles.circle_id'). |
226 | - where('circles.person_id = ?', person.id) | ||
227 | - } | ||
228 | - | ||
229 | - scope :in_circle, -> circle{ | ||
230 | - distinct.select('profiles.*'). | ||
231 | - joins('left join profiles_circles ON profiles_circles.profile_id = profiles.id'). | ||
232 | - joins('left join circles ON circles.id = profiles_circles.circle_id'). | ||
233 | - where('circles.id = ?', circle.id) | 226 | + where('circles.owner_id = ?', person.id) |
234 | } | 227 | } |
235 | 228 | ||
236 | settings_items :allow_followers, :type => :boolean, :default => true | 229 | settings_items :allow_followers, :type => :boolean, :default => true |
@@ -249,15 +242,11 @@ class Profile < ApplicationRecord | @@ -249,15 +242,11 @@ class Profile < ApplicationRecord | ||
249 | has_many :email_templates, :foreign_key => :owner_id | 242 | has_many :email_templates, :foreign_key => :owner_id |
250 | 243 | ||
251 | has_many :profile_followers | 244 | has_many :profile_followers |
252 | - | ||
253 | - def in_circles | ||
254 | - Circle.joins(:profile_followers). | ||
255 | - where('profiles_circles.profile_id = ?', self.id) | ||
256 | - end | 245 | + has_many :circles, :through => :profile_followers, :as => :profile |
257 | 246 | ||
258 | def followers | 247 | def followers |
259 | - person_followers = Person.joins(:circles).merge(in_circles).uniq | ||
260 | - external_person_followers = ExternalPerson.joins(:circles).merge(in_circles).uniq | 248 | + person_followers = Person.joins(:owned_circles).merge(circles).uniq |
249 | + external_person_followers = ExternalPerson.joins(:owned_circles).merge(circles).uniq | ||
261 | 250 | ||
262 | person_followers + external_person_followers | 251 | person_followers + external_person_followers |
263 | end | 252 | end |
@@ -1068,7 +1057,7 @@ private :generate_url, :url_options | @@ -1068,7 +1057,7 @@ private :generate_url, :url_options | ||
1068 | person.kind_of?(Profile) && person.has_permission?('destroy_profile', self) | 1057 | person.kind_of?(Profile) && person.has_permission?('destroy_profile', self) |
1069 | end | 1058 | end |
1070 | 1059 | ||
1071 | - def in_circle?(circle, follower) | ||
1072 | - ProfileFollower.with_follower(follower).with_circle(circle).with_profile(self).present? | 1060 | + def in_circle?(circle) |
1061 | + circle.in? self.circles | ||
1073 | end | 1062 | end |
1074 | end | 1063 | end |
app/models/profile_follower.rb
@@ -5,14 +5,14 @@ class ProfileFollower < ApplicationRecord | @@ -5,14 +5,14 @@ class ProfileFollower < ApplicationRecord | ||
5 | attr_accessible :profile, :circle | 5 | attr_accessible :profile, :circle |
6 | 6 | ||
7 | #TODO -> user being followed | 7 | #TODO -> user being followed |
8 | - belongs_to :profile | 8 | + belongs_to :profile, :polymorphic => true |
9 | belongs_to :circle | 9 | belongs_to :circle |
10 | 10 | ||
11 | #TODO -> circle owner | 11 | #TODO -> circle owner |
12 | # has_one :person, through: :circle, source_type: "ProfileFollower" | 12 | # has_one :person, through: :circle, source_type: "ProfileFollower" |
13 | 13 | ||
14 | def circle_owner | 14 | def circle_owner |
15 | - self.circle.person | 15 | + self.circle.owner |
16 | end | 16 | end |
17 | 17 | ||
18 | alias follower circle_owner | 18 | alias follower circle_owner |
@@ -21,7 +21,7 @@ class ProfileFollower < ApplicationRecord | @@ -21,7 +21,7 @@ class ProfileFollower < ApplicationRecord | ||
21 | 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"} |
22 | 22 | ||
23 | scope :with_follower, -> person{ | 23 | scope :with_follower, -> person{ |
24 | - joins(:circle).where('circles.person_id = ?', person.id) | 24 | + joins(:circle).where('circles.owner_id = ?', person.id) |
25 | } | 25 | } |
26 | 26 | ||
27 | scope :with_profile, -> profile{ | 27 | scope :with_profile, -> profile{ |
app/views/blocks/profile_info_actions/_select_circles.html.erb
@@ -3,7 +3,7 @@ | @@ -3,7 +3,7 @@ | ||
3 | <div id="circles-checkboxes"> | 3 | <div id="circles-checkboxes"> |
4 | <% circles.each do |circle| %> | 4 | <% circles.each do |circle| %> |
5 | <div class="circle"> | 5 | <div class="circle"> |
6 | - <%= labelled_check_box circle.name, "circles[#{circle.name}]", circle.id, followed_profile.in_circle?(circle, follower) %> | 6 | + <%= labelled_check_box circle.name, "circles[#{circle.name}]", circle.id, followed_profile.in_circle?(circle) %> |
7 | </div> | 7 | </div> |
8 | <% end %> | 8 | <% end %> |
9 | </div> | 9 | </div> |
db/migrate/20160822174619_adds_profile_type_to_profile_followers.rb
0 → 100644
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +class AddsProfileTypeToProfileFollowers < ActiveRecord::Migration | ||
2 | + def up | ||
3 | + add_column :profiles_circles, :profile_type, :string, default: "Person" | ||
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 |
db/migrate/20160822184703_rename_person_id_from_circles.rb
0 → 100644
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +class RenamePersonIdFromCircles < ActiveRecord::Migration | ||
2 | + def up | ||
3 | + rename_column :circles, :person_id, :owner_id | ||
4 | + rename_column :circles, :person_type, :owner_type | ||
5 | + end | ||
6 | + | ||
7 | + def down | ||
8 | + rename_column :circles, :owner_id, :person_id | ||
9 | + rename_column :circles, :owner_type, :person_type | ||
10 | + end | ||
11 | +end |