Add basics of Follower feature
This commit is contained in:
parent
3819f8d9d3
commit
413bcf3540
@ -1,4 +1,8 @@
|
|||||||
class Profile < ActiveRecord::Base
|
class Profile < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
has_many :social_links
|
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
|
end
|
||||||
|
|||||||
6
app/models/relationship.rb
Normal file
6
app/models/relationship.rb
Normal file
@ -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
|
||||||
@ -7,6 +7,11 @@ class User < ActiveRecord::Base
|
|||||||
has_many :doodads
|
has_many :doodads
|
||||||
has_many :projects
|
has_many :projects
|
||||||
has_many :profiles
|
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
|
def icid
|
||||||
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize
|
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize
|
||||||
@ -100,4 +105,20 @@ class User < ActiveRecord::Base
|
|||||||
# :confirmable, :lockable, :timeoutable and :omniauthable
|
# :confirmable, :lockable, :timeoutable and :omniauthable
|
||||||
devise :invitable, :database_authenticatable, :registerable,
|
devise :invitable, :database_authenticatable, :registerable,
|
||||||
:recoverable, :rememberable, :trackable, :validatable
|
: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
|
end
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
- profile.social_links.each do |social_link|
|
- profile.social_links.each do |social_link|
|
||||||
= social_link.to_icon.html_safe
|
= social_link.to_icon.html_safe
|
||||||
%p= profile.about
|
%p= profile.about
|
||||||
|
%span #{profile.followers.count} Followers
|
||||||
- if policy(profile).destroy?
|
- if policy(profile).destroy?
|
||||||
= link_to 'Edit', edit_profile_path(profile)
|
= link_to 'Edit', edit_profile_path(profile)
|
||||||
= link_to 'Delete', profile, method: :delete, data: { confirm: 'Are you sure?' }
|
= link_to 'Delete', profile, method: :delete, data: { confirm: 'Are you sure?' }
|
||||||
|
|||||||
@ -22,3 +22,5 @@
|
|||||||
%h2 About #{@profile.name}
|
%h2 About #{@profile.name}
|
||||||
%p= @profile.about
|
%p= @profile.about
|
||||||
|
|
||||||
|
.stats
|
||||||
|
%p #{@profile.followers.count} Followers
|
||||||
|
|||||||
10
app/views/shared/_stats.html.haml
Normal file
10
app/views/shared/_stats.html.haml
Normal file
@ -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
|
||||||
@ -1,3 +1,7 @@
|
|||||||
<h3>User</h3>
|
<h3>User</h3>
|
||||||
<p>Name: <%= @user.display_name if @user.display_name %></p>
|
<p>Name: <%= @user.display_name if @user.display_name %></p>
|
||||||
<%# <p>Email: <%= @user.email if @user.email %1></p> %>
|
<%# <p>Email: <%= @user.email if @user.email %1></p> %>
|
||||||
|
|
||||||
|
<section class="stats">
|
||||||
|
<%= render 'shared/stats' %>
|
||||||
|
</section>
|
||||||
|
|||||||
@ -54,7 +54,6 @@ Rails.application.routes.draw do
|
|||||||
get '/play-now', to: 'tables#play_now', as: 'play_now'
|
get '/play-now', to: 'tables#play_now', as: 'play_now'
|
||||||
get '/tutorial', to: 'tables#tutorial', as: 'tutorial'
|
get '/tutorial', to: 'tables#tutorial', as: 'tutorial'
|
||||||
devise_for :users, controllers: { registrations: "users/registrations" }
|
devise_for :users, controllers: { registrations: "users/registrations" }
|
||||||
resources :users
|
|
||||||
get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt')
|
get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt')
|
||||||
get '/loaderio-eb4afd672bf5b9af467fbadcba243f26.txt', to: redirect('/assets/loaderio-eb4afd672bf5b9af467fbadcba243f26.txt')
|
get '/loaderio-eb4afd672bf5b9af467fbadcba243f26.txt', to: redirect('/assets/loaderio-eb4afd672bf5b9af467fbadcba243f26.txt')
|
||||||
get '/calls', to: 'calls#root'
|
get '/calls', to: 'calls#root'
|
||||||
@ -62,4 +61,9 @@ Rails.application.routes.draw do
|
|||||||
resources :calls, only: :create
|
resources :calls, only: :create
|
||||||
mount ActionCable.server, at: '/cable'
|
mount ActionCable.server, at: '/cable'
|
||||||
resources :rooms, only: [:new, :create, :show]
|
resources :rooms, only: [:new, :create, :show]
|
||||||
|
resources :users do
|
||||||
|
member do
|
||||||
|
get :following
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
13
db/migrate/20231109015600_create_relationships.rb
Normal file
13
db/migrate/20231109015600_create_relationships.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
class CreateRelationships < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
create_table :relationships do |t|
|
||||||
|
t.integer :follower_id
|
||||||
|
t.integer :followed_id
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
add_index :relationships, :follower_id
|
||||||
|
add_index :relationships, :followed_id
|
||||||
|
add_index :relationships, [:follower_id, :followed_id], unique: true
|
||||||
|
end
|
||||||
|
end
|
||||||
12
db/schema.rb
12
db/schema.rb
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2023_11_07_161251) do
|
ActiveRecord::Schema.define(version: 2023_11_09_015600) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@ -115,6 +115,16 @@ ActiveRecord::Schema.define(version: 2023_11_07_161251) do
|
|||||||
t.index ["user_id"], name: "index_projects_on_user_id"
|
t.index ["user_id"], name: "index_projects_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "relationships", force: :cascade do |t|
|
||||||
|
t.integer "follower_id"
|
||||||
|
t.integer "followed_id"
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.index ["followed_id"], name: "index_relationships_on_followed_id"
|
||||||
|
t.index ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
|
||||||
|
t.index ["follower_id"], name: "index_relationships_on_follower_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "seats", force: :cascade do |t|
|
create_table "seats", force: :cascade do |t|
|
||||||
t.integer "table_id"
|
t.integer "table_id"
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
|
|||||||
6
spec/factories/relationships.rb
Normal file
6
spec/factories/relationships.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FactoryGirl.define do
|
||||||
|
factory :relationship do
|
||||||
|
follower_id 1
|
||||||
|
followed_id 1
|
||||||
|
end
|
||||||
|
end
|
||||||
5
spec/models/relationship_spec.rb
Normal file
5
spec/models/relationship_spec.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# require 'rails_helper'
|
||||||
|
|
||||||
|
# RSpec.describe Relationship, type: :model do
|
||||||
|
# pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
# end
|
||||||
Loading…
x
Reference in New Issue
Block a user