Created
December 23, 2013 14:17
-
-
Save jacqueline-homan/8097784 to your computer and use it in GitHub Desktop.
concerns/following.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This will allow us to evaluate a block | |
# in the scope of a class | |
module Concerns | |
module Following | |
extend ActiveSupport::Concerns | |
included do | |
has_many :followed_user_relationships, | |
foreign_key: :follower_id, | |
class_name: 'FollowingRelationship' | |
has_many :followed_users, through: :followed_user_relationships | |
has_many :follower_relationships, | |
foreign_key: :followed_user_id, | |
class_name: 'FollowingRelationship' | |
has_many :followers, through: :follower_relationships | |
end | |
def following? user | |
followed_user_ids.include? user.id | |
end | |
def follow user | |
followed_users << user | |
end | |
def unfollow user | |
followed_users.delete(user) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeController < ApplicationController | |
before_filter :check_logged_in_user | |
def show | |
@dashboard = Dashboard.new | |
end | |
private | |
def check_logged_in_user | |
if signed_in? | |
redirect_to dashboard_path | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
include Concerns::Following | |
has_many :shouts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment