Skip to content

Commit

Permalink
Fixed and added some functionality with the FollowRelationship Model
Browse files Browse the repository at this point in the history
- Added a follows and unfollow button
- Added a counter for follows
- Fixed countless spelling errors and mistakes
  • Loading branch information
a2k11 committed Oct 21, 2014
1 parent ef738e4 commit 166ec25
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 19 deletions.
16 changes: 16 additions & 0 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class FollowsController < ApplicationController
def create
user = User.find_by(username: params[:id])
current_user.follow(user)

redirect_to user
end

def destroy
user = User.find_by(username: params[:id])
current_user.unfollow(user)

redirect_to user
end

end
28 changes: 14 additions & 14 deletions app/controllers/shouts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class ShoutsController < ApplicationController
before_action :require_login

def new
@shout = Shout.new
end

def create
@shout = current_user.shouts.new(shout_params)
def new
@shout = Shout.new
end

def create
@shout = current_user.shouts.new(shout_params)

if @shout.save
redirect_to dashboard_path
else
render :new
if @shout.save
redirect_to dashboard_path
else
render :new
end
end
end


private
def shout_params
params.require(:shout).permit(:content)
end
def shout_params
params.require(:shout).permit(:content, :username)
end
end
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def create
end
end

def show
@user = User.find_by(username: params[:id])
end

private

def user_params
Expand Down
4 changes: 4 additions & 0 deletions app/models/follow_relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class FollowRelationship < ActiveRecord::Base
belongs_to :followed_user, class_name: "User"
belongs_to :follower, class_name: "User"
end
4 changes: 3 additions & 1 deletion app/models/shout.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Shout < ActiveRecord::Base

delegate :username, to: :user
belongs_to :user

validates :content, presence: true
end
22 changes: 22 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,26 @@ class User < ActiveRecord::Base
validates :password_digest, presence: true

has_many :shouts
has_many :followed_user_relationships,
class_name: "FollowRelationship", foreign_key: :follower_id

has_many :followed_users, through: :followed_user_relationships

has_many :follower_relationships,
class_name: "FollowRelationship", foreign_key: :followed_user_id

has_many :followers, through: :follower_relationships

def follow(user)
followed_users << user
end

def unfollow(user)
user.followers.destroy(self)
end

def to_param
username
end

end
4 changes: 2 additions & 2 deletions app/views/dashboards/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>FUCKER</h1>
<h1><%= link_to "#{current_user.username.capitalize}", user_path(current_user.username) %></h1>

<h2>FUCKING STEELERS FANS</h2>
<h2>Shouts Page</h2>

<%= link_to "Make a Shout", new_shout_path %>

Expand Down
15 changes: 15 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>This is <%= @user.username.capitalize %>'s page.</h1>
<br>
<% unless @user.followers.include? current_user %>
<%= button_to "follow" , follows_user_path(@user.username) %>
<% else %>
<%= button_to "unfollow" , follows_user_path(@user.username), method: :delete %>
<% end %>
<br>
<%= @user.followers.count %>
<br>
<% @user.shouts.each do |shout| %>
<div>
<%= shout.content %>
</div>
<% end %>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

resources :shouts, only: [:new, :create]
resource :session, only: [:new, :create, :destroy]
resources :users, only: [:new, :create]
resources :users, only: [:new, :create, :show] do
member do
post "follows" => "follows#create"
delete "follows" => "follows#destroy"
end
end

get "sign_up" => "users#new"
get "sign_in" => "sessions#new"
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20141021002645_create_follow_relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateFollowRelationships < ActiveRecord::Migration
def change
create_table :follow_relationships do |t|
t.references :follower
t.references :followed_user

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20141020212244) do
ActiveRecord::Schema.define(version: 20141021002645) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "follow_relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "shouts", force: true do |t|
t.string "content", null: false
t.integer "user_id", null: false
Expand Down

0 comments on commit 166ec25

Please sign in to comment.