diff --git a/app/models/profile.rb b/app/models/profile.rb index e23c028f..4c84113a 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -1,4 +1,8 @@ class Profile < ActiveRecord::Base belongs_to :user has_many :social_links + has_many :passive_relationships, class_name: "Relationship", + foreign_key: "followed_id", + dependent: :destroy + has_many :followers, through: :passive_relationships, source: :follower end diff --git a/app/models/relationship.rb b/app/models/relationship.rb new file mode 100644 index 00000000..4f159a84 --- /dev/null +++ b/app/models/relationship.rb @@ -0,0 +1,6 @@ +class Relationship < ActiveRecord::Base + belongs_to :follower, class_name: "User" + belongs_to :followed, class_name: "Profile" + validates :follower_id, presence: true + validates :followed_id, presence: true +end diff --git a/app/models/user.rb b/app/models/user.rb index bd949a7c..ad8f5709 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,11 @@ class User < ActiveRecord::Base has_many :doodads has_many :projects has_many :profiles + has_many :active_relationships, class_name: "Relationship", + foreign_key: "follower_id", + dependent: :destroy + has_many :following, through: :active_relationships, source: :followed + def icid stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize @@ -100,4 +105,20 @@ class User < ActiveRecord::Base # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + + # Follows a user. + def follow(leader) + active_relationships.create(followed_id: leader.id) + end + + # Unfollows a user. + def unfollow(leader) + active_relationships.find_by(followed_id: leader.id).destroy + end + + # Returns true if the current user is following the leader. + def following?(leader) + following.include?(leader) + end + end diff --git a/app/views/profiles/index.html.haml b/app/views/profiles/index.html.haml index 7cbd574c..18dedfc0 100644 --- a/app/views/profiles/index.html.haml +++ b/app/views/profiles/index.html.haml @@ -17,6 +17,7 @@ - profile.social_links.each do |social_link| = social_link.to_icon.html_safe %p= profile.about + %span #{profile.followers.count} Followers - if policy(profile).destroy? = link_to 'Edit', edit_profile_path(profile) = link_to 'Delete', profile, method: :delete, data: { confirm: 'Are you sure?' } diff --git a/app/views/profiles/show.html.haml b/app/views/profiles/show.html.haml index 014ae509..ae936275 100644 --- a/app/views/profiles/show.html.haml +++ b/app/views/profiles/show.html.haml @@ -22,3 +22,5 @@ %h2 About #{@profile.name} %p= @profile.about +.stats + %p #{@profile.followers.count} Followers diff --git a/app/views/shared/_stats.html.haml b/app/views/shared/_stats.html.haml new file mode 100644 index 00000000..7f73a4dc --- /dev/null +++ b/app/views/shared/_stats.html.haml @@ -0,0 +1,10 @@ +- @user ||= current_user +.stats + %a{:href => "#{following_user_path(@user)}"} + %strong#following.stat + = @user.following.count + following + -# %a{:href => "#{followers_user_path(@user)}"} + %strong#followers.stat + = @user.followers.count + followers diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 5626e376..a6cdec07 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,3 +1,7 @@
Name: <%= @user.display_name if @user.display_name %>
<%#Email: <%= @user.email if @user.email %1>
%> + +