mongoid_follow 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Alec Guintu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ = Follow stuff for Mongoid
2
+
3
+ == Installation
4
+
5
+ In Gemfile:
6
+ gem 'mongoid_follow""
7
+
8
+ == To use
9
+
10
+ To make mongoid_follow usable you need to include Mongoid::Followee into your document
11
+ Meanwhile, you also need to include Mongoid::Follower in your follower model:
12
+
13
+ class User
14
+ include Mongoid::Document
15
+ include Mongoid::Followee
16
+ include Mongoid::Follower
17
+ end
18
+
19
+ class Group
20
+ include Mongoid::Document
21
+ include Mongoid::Followee
22
+ end
23
+
24
+ You can then follow a model using:
25
+
26
+ @bonnie = User.new
27
+ @bonnie.save
28
+
29
+ current_user.follow(@clyde)
30
+ current_user.unfollow?(@clyde)
31
+
32
+ You can also see whether a model is a follower of another model or a model is a followee of another model:
33
+
34
+ current_user.follower?(@clyde)
35
+ current_user.followee?(@clyde)
36
+
37
+ Of course, you can get a list of followers/followees:
38
+
39
+ @clyde.followers
40
+ @bonnie.followees
41
+
42
+ * Any bug or issue, please send me an email: aeguintu@gmail.com
43
+
44
+ == TODO
45
+
46
+ * finish up todo's
47
+ * count of followers/followees
48
+ * common followers (or maybe followees)
49
+ * maybe... (un)authorization of who can follow (model)
50
+ * most/least followed/following #FINISHED
51
+
52
+ == Thanks
53
+
54
+ Thanks the mongoid_followable for
55
+
56
+ == Copyright
57
+
58
+ Copyright (c) Alec Guintu. See LICENSE.txt for further details.
@@ -0,0 +1,9 @@
1
+ class Follow
2
+ include Mongoid::Document
3
+
4
+ field :ff_type
5
+ field :ff_id
6
+
7
+ belongs_to :follower, :polymorphic => true
8
+ belongs_to :followee, :polymorphic => true
9
+ end
@@ -0,0 +1,27 @@
1
+ module Mongoid
2
+ module Followee
3
+ extend ActiveSupport::Concern
4
+
5
+ included do |base|
6
+ base.has_many :followers, :class_name => 'Follow', :as => :follower, :dependent => :destroy
7
+ end
8
+
9
+ # see if this model is followed of some model
10
+ #
11
+ # Example:
12
+ # >> @clyde.follower?(@bonnie)
13
+ # => true
14
+ def follower?(model)
15
+ 0 < self.followers.find(:all, conditions: {ff_id: model.id}).limit(1).count
16
+ end
17
+
18
+ # view all selfs followers
19
+ #
20
+ # Example:
21
+ # >> @clyde.follower?(@bonnie)
22
+ # => true
23
+ def followers(model)
24
+ #TODO
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ module Mongoid
2
+ module Follower
3
+ extend ActiveSupport::Concern
4
+
5
+ included do |base|
6
+ base.has_many :followees, :class_name => 'Follow', :as => :followee, :dependent => :destroy
7
+ end
8
+
9
+ # follow a model
10
+ #
11
+ # Example:
12
+ # >> @bonnie.follow(@clyde)
13
+ def follow(model)
14
+ model.followers.create!(:ff_type => self.class.name, :ff_id => self.id)
15
+
16
+ self.followees.create!(:ff_type => model.class.name, :ff_id => model.id)
17
+ end
18
+
19
+ # unfollow a model
20
+ #
21
+ # Example:
22
+ # >> @bonnie.unfollow(@clyde)
23
+ def unfollow(model)
24
+ model.followers.where(:ff_type => self.class.name, :ff_id => self.id).destroy
25
+
26
+ self.followees.where(:ff_type => model.class.name, :ff_id => model.id).destroy
27
+ end
28
+
29
+ # follow a model
30
+ #
31
+ # Example:
32
+ # >> @bonnie.follows?(@clyde)
33
+ # => true
34
+ def follows?(model)
35
+ 0 < self.followees.find(:all, conditions: {ff_id: model.id}).limit(1).count
36
+ end
37
+
38
+ # view all selfs followees
39
+ #
40
+ # Example:
41
+ # >> @alec.followees
42
+ def followees(model)
43
+ # TODO
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoidFollow
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,9 +8,9 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Alec Guintu"]
10
10
  s.email = ["animerei12@gmail.com"]
11
- s.homepage = ""
12
- s.summary = %q{Add basic "follow" features to rails3/mongoid}
13
- s.description = %q{A gem to add a basic "follow" feature if you're using rails3 with mongoid}
11
+ s.homepage = "https://github.com/alecguintu/mongoid_follow"
12
+ s.summary = %q{ Add basic "follow" features to rails3/mongoid }
13
+ s.description = %q{ A gem to add a basic "follow" feature if you're using rails3 with mongoid }
14
14
 
15
15
  s.rubyforge_project = "mongoid_follow"
16
16
 
@@ -0,0 +1,4 @@
1
+ class Group
2
+ include Mongoid::Document
3
+ include Mongoid::Followee
4
+ end
@@ -0,0 +1,5 @@
1
+ class User
2
+ include Mongoid::Document
3
+ include Mongoid::Followee
4
+ include Mongoid::Follower
5
+ end
@@ -0,0 +1,19 @@
1
+ require "rubygems"
2
+
3
+ require "database_cleaner"
4
+ require "mongoid"
5
+ require "rspec"
6
+
7
+ Mongoid.configure do |config|
8
+ name = "mongoid_follow_test"
9
+ config.master = Mongo::Connection.new.db(name)
10
+ end
11
+
12
+ require File.expand_path("../../lib/mongoid_follow", __FILE__)
13
+ require File.expand_path("../models/user", __FILE__)
14
+ require File.expand_path("../models/group", __FILE__)
15
+
16
+ RSpec.configure do |c|
17
+ c.before(:all) { DatabaseCleaner.strategy = :truncation }
18
+ c.before(:each) { DatabaseCleaner.clean }
19
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Follower do
4
+
5
+ describe User do
6
+
7
+ before do
8
+ @bonnie = User.new
9
+ @bonnie.save
10
+
11
+ @clyde = User.new
12
+ @clyde.save
13
+
14
+ @alec = User.new
15
+ @alec.save
16
+
17
+ @gang = Group.new
18
+ @gang.save
19
+ end
20
+
21
+ it "should have no follows or followers" do
22
+ @bonnie.follows?(@clyde).should be_false
23
+
24
+ @bonnie.follow(@clyde)
25
+ @clyde.follower?(@alec).should be_false
26
+ @alec.follows?(@clyde).should be_false
27
+ end
28
+
29
+ it "can follow another User" do
30
+ @bonnie.follow(@clyde)
31
+
32
+ @bonnie.follows?(@clyde).should be_true
33
+ @clyde.follower?(@bonnie).should be_true
34
+ end
35
+
36
+ it "can unfollow another User" do
37
+ @bonnie.follows?(@clyde).should be_false
38
+ @clyde.follower?(@bonnie).should be_false
39
+
40
+ @bonnie.follow(@clyde)
41
+ @bonnie.follows?(@clyde).should be_true
42
+ @clyde.follower?(@bonnie).should be_true
43
+
44
+ @bonnie.unfollow(@clyde)
45
+ @bonnie.follows?(@clyde).should be_false
46
+ @clyde.follower?(@bonnie).should be_false
47
+ end
48
+
49
+ it "can follow a group" do
50
+ @bonnie.follow(@gang)
51
+
52
+ @bonnie.follows?(@gang).should be_true
53
+ @gang.follower?(@bonnie).should be_true
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid_follow
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alec Guintu
@@ -57,7 +57,7 @@ dependencies:
57
57
  version: "0"
58
58
  type: :development
59
59
  version_requirements: *id004
60
- description: A gem to add a basic "follow" feature if you're using rails3 with mongoid
60
+ description: " A gem to add a basic \"follow\" feature if you're using rails3 with mongoid "
61
61
  email:
62
62
  - animerei12@gmail.com
63
63
  executables: []
@@ -69,12 +69,21 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
71
  - Gemfile
72
+ - LICENSE.txt
73
+ - README.rdoc
72
74
  - Rakefile
75
+ - app/models/follow.rb
73
76
  - lib/mongoid_follow.rb
77
+ - lib/mongoid_follow/followee.rb
78
+ - lib/mongoid_follow/follower.rb
74
79
  - lib/mongoid_follow/version.rb
75
80
  - mongoid_follow.gemspec
81
+ - spec/models/group.rb
82
+ - spec/models/user.rb
83
+ - spec/spec_helper.rb
84
+ - spec/specs/follow_spec.rb
76
85
  has_rdoc: true
77
- homepage: ""
86
+ homepage: https://github.com/alecguintu/mongoid_follow
78
87
  licenses: []
79
88
 
80
89
  post_install_message:
@@ -101,5 +110,8 @@ rubygems_version: 1.6.2
101
110
  signing_key:
102
111
  specification_version: 3
103
112
  summary: Add basic "follow" features to rails3/mongoid
104
- test_files: []
105
-
113
+ test_files:
114
+ - spec/models/group.rb
115
+ - spec/models/user.rb
116
+ - spec/spec_helper.rb
117
+ - spec/specs/follow_spec.rb