Created
February 2, 2011 01:16
-
-
Save xwmx/807073 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 DashboardController < ApplicationController | |
| before_filter :require_user | |
| def index | |
| @feed_items = FeedItem.followed_by(current_user.id). | |
| includes(:post).paginate(:page => params[:page]) | |
| @page_title = "#{APP_CONFIG['site_name']} | Dashboard" | |
| end | |
| end |
This file contains hidden or 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 FeedItem < ActiveRecord::Base | |
| attr_accessible *%W[ | |
| actor_id | |
| actor | |
| subject | |
| secondary_subject | |
| event_type | |
| ] | |
| belongs_to :subject, :polymorphic => true | |
| belongs_to :secondary_subject, :polymorphic => true | |
| belongs_to :actor, :class_name => 'User' | |
| belongs_to :post, :class_name => "Post", :foreign_key => "subject_id" | |
| default_scope :order => 'feed_items.created_at DESC' | |
| scope :followed_by, lambda { |user_id| { | |
| :joins => "INNER JOIN \"followings\" ON followings.feeder_id = feed_items.actor_id", | |
| :conditions => ['followings.follower_id = ?', user_id], | |
| :select => 'DISTINCT feed_items.*' | |
| }} | |
| end |
This file contains hidden or 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 Following < ActiveRecord::Base | |
| attr_accessible *%W[ | |
| feeder | |
| feeder_id | |
| follower | |
| follower_id | |
| ] | |
| belongs_to :feeder, :class_name => 'User' | |
| belongs_to :follower, :class_name => 'User' | |
| validates_presence_of :feeder_id, :follower_id | |
| validates_uniqueness_of :follower_id, :scope => :feeder_id | |
| end |
This file contains hidden or 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 Post < ActiveRecord::Base | |
| attr_accessible *%W[ | |
| body | |
| ] | |
| belongs_to :author, :class_name => 'User' | |
| belongs_to :project | |
| has_many :feed_items, :as => :subject, :dependent => :destroy | |
| after_create :generate_feed_item | |
| protected | |
| def generate_feed_item | |
| FeedItem.create({ | |
| :event_type => "new_post", | |
| :actor => self.author, | |
| :subject => self, | |
| :secondary_subject => self.project | |
| }) | |
| end | |
| end |
This file contains hidden or 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 | |
| has_many :feeder_followings, | |
| :foreign_key => 'follower_id', | |
| :class_name => 'Following' | |
| has_many :feeders, :through => :feeder_followings | |
| has_many :follower_followings, | |
| :foreign_key => 'feeder_id', | |
| :class_name => 'Following' | |
| has_many :followers, :through => :follower_followings | |
| # following | |
| def follow!(feeder) | |
| self.feeders << feeder | |
| end | |
| def unfollow!(feeder) | |
| if f = follows?(feeder) | |
| f.destroy | |
| end | |
| end | |
| def follows?(feeder) | |
| self.feeder_followings.where(:feeder_id => feeder.id).first | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment