Skip to content

Instantly share code, notes, and snippets.

@jacqueline-homan
Created December 23, 2013 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacqueline-homan/8097784 to your computer and use it in GitHub Desktop.
Save jacqueline-homan/8097784 to your computer and use it in GitHub Desktop.
concerns/following.rb
#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
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
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