Commit 463bb4de by Rocky Duan

getting a little messy; put code back to models and probably extract later

parent 568b120b
......@@ -4,8 +4,6 @@ require 'bundler'
Bundler.setup
Bundler.require
require './lib/feedstream'
require './models/comment.rb'
require './models/comment_thread.rb'
require './models/user.rb'
......
......@@ -4,8 +4,6 @@ require 'bundler'
Bundler.setup
Bundler.require
require './lib/feedstream'
require './models/comment'
require './models/comment_thread'
require './models/commentable'
......
require_relative 'feedstream/watchable'
require_relative 'feedstream/watcher'
require_relative 'feedstream/followable'
require_relative 'feedstream/actor'
require_relative 'feedstream/feed'
module Mongoid
module FeedStream
module Actor
extend ActiveSupport::Concern
included do
has_many :activities, class_name: "Feed", inverse_of: :actor, autosave: true
end
end
end
end
module Mongoid
module FeedStream
module Feed
extend ActiveSupport::Concern
included do
include ::Mongoid::Document
include ::Mongoid::Timestamps
field :feed_type, type: String
field :info, type: Hash
belongs_to :actor, class_name: "User", inverse_of: :activities, index: true
belongs_to :target, inverse_of: :activities, polymorphic: true
attr_accessible :feed_type, :info
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :feeds
end
end
end
end
module Mongoid
module FeedStream
module Followable
extend ActiveSupport::Concern
included do
has_and_belongs_to_many :followers, class_name: self.name, inverse_of: :followings
has_and_belongs_to_many :followings, class_name: self.name, inverse_of: :followers
end
def follow(user)
if self.id != user.id and not self.following.include? user
self.following << user
end
end
def unfollow(user)
self.following.delete(user)
end
end
end
end
module Mongoid
module FeedStream
module Watchable
extend ActiveSupport::Concern
included do
has_and_belongs_to_many :watchers, class_name: "User", inverse_of: "watched_#{self.name.underscore.pluralize}".intern
end
end
end
end
module Mongoid
module FeedStream
module Watcher
extend ActiveSupport::Concern
included do
has_and_belongs_to_many :feeds, inverse_of: :subscribers
end
module ClassMethods
def watching(class_plural_sym)
class_plural = class_plural_sym.to_s
class_single = class_plural.singularize
class_name = class_single.camelize
watched_symbol = "watched_#{class_plural}".intern
has_and_belongs_to_many watched_symbol, class_name: class_name, inverse_of: :watchers, autosave: true
self.class_eval <<-END
def watch_#{class_single}(watching_object)
if not self.watched_#{class_plural}.include? watching_object
self.watched_#{class_plural} << watching_object
end
end
def unwatch_#{class_single}(watching_object)
self.watched_#{class_plural}.delete(watching_object)
end
END
end
end
end
end
end
......@@ -2,7 +2,6 @@ class CommentThread
include Mongoid::Document
include Mongo::Voteable
include Mongoid::Timestamps
include Mongoid::FeedStream::Watchable
voteable self, :up => +1, :down => -1
......@@ -13,6 +12,7 @@ class CommentThread
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true
belongs_to :commentable, index: true
has_many :comments, dependent: :destroy # Use destroy to envoke callback on the top-level comments
has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_comment_threads
attr_accessible :title, :body, :course_id
......
class Commentable
include Mongoid::Document
include Mongoid::FeedStream::Watchable
field :commentable_type, type: String
field :commentable_id, type: String
has_many :comment_threads, dependent: :destroy
has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_commentables
attr_accessible :commentable_type, :commentable_id
......
class Feed
include Mongoid::FeedStream::Feed
include Mongoid::Document
include Mongoid::Timestamps
field :feed_type, type: String
field :info, type: Hash
belongs_to :actor, class_name: "User", inverse_of: :activities, index: true
belongs_to :target, inverse_of: :activities, polymorphic: true
attr_accessible :feed_type, :info
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_feeds
end
class User
include Mongoid::Document
include Mongo::Voter
include Mongoid::FeedStream::Watcher
include Mongoid::FeedStream::Actor
include Mongoid::FeedStream::Followable
field :external_id, type: String
watching :comment_threads
watching :commentables
has_many :comments
has_many :comment_threads, inverse_of: :author
has_many :activities, class_name: "Feed", inverse_of: :actor
has_and_belongs_to_many :subscribed_feeds, class_name: "Feed", inverse_of: :subscribers
has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings
has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers
attr_accessible :external_id
......@@ -20,4 +18,38 @@ class User
index :external_id, unique: true
def follow(user)
if self.id != user.id and not self.following.include? user
self.following << user
end
end
def unfollow(user)
self.following.delete(user)
end
def self.watching(class_plural_sym)
class_plural = class_plural_sym.to_s
class_single = class_plural.singularize
class_name = class_single.camelize
watched_symbol = "watched_#{class_plural}".intern
has_and_belongs_to_many watched_symbol, class_name: class_name, inverse_of: :watchers, autosave: true
self.class_eval <<-END
def watch_#{class_single}(watching_object)
if not self.watched_#{class_plural}.include? watching_object
self.watched_#{class_plural} << watching_object
end
end
def unwatch_#{class_single}(watching_object)
self.watched_#{class_plural}.delete(watching_object)
end
END
end
watching :comment_threads
watching :commentables
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment