Skip to content
This repository was archived by the owner on Sep 27, 2022. It is now read-only.

Commit 7a2cb1e

Browse files
committed
Add followers and feed
1 parent 11ae5ef commit 7a2cb1e

File tree

8 files changed

+83
-2
lines changed

8 files changed

+83
-2
lines changed

app/controllers/articles_controller.rb

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ def index
1313
@articles = @articles.order(created_at: :desc).offset(params[:offset] || 0).limit(params[:limit] || 20)
1414
end
1515

16+
def feed
17+
@articles = Article.includes(:user).where(user: current_user.following_users)
18+
19+
@articles_count = @articles.count
20+
21+
@articles = @articles.order(created_at: :desc).offset(params[:offset] || 0).limit(params[:limit] || 20)
22+
23+
render :index
24+
end
25+
1626
def create
1727
@article = Article.new(article_params)
1828
@article.user = current_user

app/controllers/follows_controller.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class FollowsController < ApplicationController
2+
before_action :authenticate_user!
3+
4+
def create
5+
@user = User.find_by_username!(params[:profile_username])
6+
7+
current_user.follow(@user) if current_user.id != @user.id
8+
9+
render 'profiles/show'
10+
end
11+
12+
def destroy
13+
@user = User.find_by_username!(params[:profile_username])
14+
15+
current_user.stop_following(@user) if current_user.id != @user.id
16+
17+
render 'profiles/show'
18+
end
19+
end

app/models/follow.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Follow < ActiveRecord::Base
2+
3+
extend ActsAsFollower::FollowerLib
4+
extend ActsAsFollower::FollowScopes
5+
6+
# NOTE: Follows belong to the "followable" interface, and also to followers
7+
belongs_to :followable, :polymorphic => true
8+
belongs_to :follower, :polymorphic => true
9+
10+
def block!
11+
self.update_attribute(:blocked, true)
12+
end
13+
14+
end

app/models/user.rb

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class User < ActiveRecord::Base
88
has_many :favorites, dependent: :destroy
99
has_many :comments, dependent: :destroy
1010

11+
acts_as_follower
12+
acts_as_followable
13+
14+
1115
validates :username, uniqueness: { case_sensitive: true },
1216
format: { with: /\A[a-zA-Z0-9]+\z/ },
1317
presence: true,
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
json.(user, :username, :bio)
22
json.image user.image || 'https://static.productionready.io/images/smiley-cyrus.jpg'
3+
json.following signed_in? ? current_user.following?(user) : false

config/routes.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
resource :user, only: [:show, :update]
88

9-
resources :profiles, param: :username, only: [:show]
9+
resources :profiles, param: :username, only: [:show] do
10+
resource :follow, only: [:create, :destroy]
11+
end
1012

1113
resources :articles, param: :slug, except: [:edit, :new] do
1214
resource :favorite, only: [:create, :destroy]
1315
resources :comments, only: [:create, :index, :destroy]
16+
get :feed, on: :collection
1417
end
1518

1619
resources :tags, only: [:index]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class ActsAsFollowerMigration < ActiveRecord::Migration
2+
def self.up
3+
create_table :follows, :force => true do |t|
4+
t.references :followable, :polymorphic => true, :null => false
5+
t.references :follower, :polymorphic => true, :null => false
6+
t.boolean :blocked, :default => false, :null => false
7+
t.timestamps
8+
end
9+
10+
add_index :follows, ["follower_id", "follower_type"], :name => "fk_follows"
11+
add_index :follows, ["followable_id", "followable_type"], :name => "fk_followables"
12+
end
13+
14+
def self.down
15+
drop_table :follows
16+
end
17+
end

db/schema.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20160712061113) do
14+
ActiveRecord::Schema.define(version: 20160712061614) do
1515

1616
create_table "articles", force: :cascade do |t|
1717
t.string "title"
@@ -48,6 +48,19 @@
4848
add_index "favorites", ["article_id"], name: "index_favorites_on_article_id"
4949
add_index "favorites", ["user_id"], name: "index_favorites_on_user_id"
5050

51+
create_table "follows", force: :cascade do |t|
52+
t.integer "followable_id", null: false
53+
t.string "followable_type", null: false
54+
t.integer "follower_id", null: false
55+
t.string "follower_type", null: false
56+
t.boolean "blocked", default: false, null: false
57+
t.datetime "created_at"
58+
t.datetime "updated_at"
59+
end
60+
61+
add_index "follows", ["followable_id", "followable_type"], name: "fk_followables"
62+
add_index "follows", ["follower_id", "follower_type"], name: "fk_follows"
63+
5164
create_table "taggings", force: :cascade do |t|
5265
t.integer "tag_id"
5366
t.integer "taggable_id"

0 commit comments

Comments
 (0)