has_many_friends 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,73 @@
1
+ HasManyFriends - Steve Ehrenberg - http://dnite.org
2
+ ===================================================
3
+ has_many_friends is a plugin based off of the friendship_plugin that can be found
4
+ here (http://svn.webwideconsulting.com/svn/friendship_plugin/). This version of the
5
+ plugin adds some features I wanted as well as eliminates the need for 2 friendship
6
+ rows created in the table. The goal is to make it relatively seamless for anyone to
7
+ create a fully functional friends system for their application.
8
+
9
+
10
+ Setup
11
+ =====
12
+ To use this plugin, you will first need to install it. This can be done quite simply
13
+ with the following command.
14
+
15
+ script/plugin install http://svn.dnite.org/has_many_friends
16
+
17
+ We'll need to generate the model and migration for your friendship table.
18
+
19
+ script/generate hmf_friendship_model Friendship
20
+ rake db:migrate
21
+
22
+ If you will be running tests, you may need to prepare your test database again.
23
+
24
+ rake db:test:prepare
25
+
26
+ You should be just about set! All you need to do is add the following method
27
+ to your User model.
28
+
29
+ class User < ActiveRecord::Base
30
+ has_many_friends
31
+ # the rest of your user model ...
32
+ # ...
33
+ end
34
+
35
+
36
+ Usage
37
+ =====
38
+ After the plugin is installed. All of these super cool methods will be attached to
39
+ any user.
40
+
41
+ These methods are the actual associations. They return what you'd expect.
42
+ user.friends_for_me
43
+ user.friends_by_me
44
+ user.pending_friends_for_me
45
+ user.pending_friends_by_me
46
+
47
+ Again, these will return what you would expect them to return.
48
+ user.friends
49
+ user.pending_friends
50
+ user.pending_or_accepted_friends
51
+
52
+ ## The following 3 methods were changed as of revision 8. They have been listed here
53
+ ## as these methods, but for some reason, i never changed their names in the actual
54
+ ## plugin file. If your using a pre-8 version and things broke for you, use the new
55
+ ## method names in your application. Sorry for the inconvenience.
56
+ Returns true if user is friends with friend.
57
+ user.is_friends_with? friend
58
+ user.is_pending_friends_with? friend
59
+ user.is_friends_or_pending_with? friend
60
+
61
+ Creates, deletes or updates friendship requests.
62
+ user.request_friendship_with friend
63
+ user.delete_friendship_with friend
64
+ user.accept_friendship_with friend
65
+
66
+ Bypass the request and just make a friend.
67
+ user.become_friends_with! friend
68
+
69
+ Returns the friendship object (good for looking up extra attributes about
70
+ the friendship, like when it was accepted and such.
71
+ user.friendship friend
72
+
73
+ Copyright (c) 2007 Steve Ehrenberg, released under the MIT license
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the has_many_friends plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the has_many_friends plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'HasManyFriends'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,135 @@
1
+ module HasManyFriends
2
+
3
+ module UserExtensions
4
+
5
+ def self.included( recipient )
6
+ recipient.extend( ClassMethods )
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_many_friends(options={})
11
+ has_many :friendships_by_me,
12
+ :foreign_key => 'user_id',
13
+ :class_name => 'Friendship'
14
+
15
+ has_many :friendships_for_me,
16
+ :foreign_key => 'friend_id',
17
+ :class_name => 'Friendship'
18
+
19
+ has_many :friends_by_me,
20
+ :through => :friendships_by_me,
21
+ :source => :friendshipped_for_me,
22
+ :conditions => 'accepted_at IS NOT NULL' do
23
+ def online
24
+ find(:all, :conditions => ['status <> ? AND updated_at > ?', 'offline', 65.seconds.ago]) if options[:online_method]
25
+ end
26
+ end
27
+
28
+ has_many :friends_for_me,
29
+ :through => :friendships_for_me,
30
+ :source => :friendshipped_by_me,
31
+ :conditions => 'accepted_at IS NOT NULL' do
32
+ def online
33
+ find(:all, :conditions => ['status <> ? AND updated_at > ?', 'offline', 65.seconds.ago]) if options[:online_method]
34
+ end
35
+ end
36
+
37
+ has_many :pending_friends_by_me,
38
+ :through => :friendships_by_me,
39
+ :source => :friendshipped_for_me,
40
+ :conditions => 'accepted_at IS NULL'
41
+
42
+ has_many :pending_friends_for_me,
43
+ :through => :friendships_for_me,
44
+ :source => :friendshipped_by_me,
45
+ :conditions => 'accepted_at IS NULL'
46
+
47
+ include HasManyFriends::UserExtensions::InstanceMethods
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+
53
+ # Returns a list of all of a users accepted friends.
54
+ def friends
55
+ self.friends_for_me + self.friends_by_me
56
+ end
57
+
58
+ # Return a list of all friends who are currently online.
59
+ # This won't return anything unless you passed the :online_method
60
+ # option to has_many_friends.
61
+ def online_friends
62
+ self.friends_by_me.online + self.friends_for_me.online
63
+ end
64
+
65
+ # Returns a list of all pending friendships.
66
+ def pending_friends
67
+ self.pending_friends_by_me + self.pending_friends_for_me
68
+ end
69
+
70
+ # Returns a full list of all pending and accepted friends.
71
+ def pending_or_accepted_friends
72
+ self.friends + self.pending_friends
73
+ end
74
+
75
+ # Accepts a user object and returns the friendship object
76
+ # associated with both users.
77
+ def friendship(friend)
78
+ Friendship.find(:first, :conditions => ['(user_id = ? AND friend_id = ?) OR (friend_id = ? AND user_id = ?)', self.id, friend.id, self.id, friend.id])
79
+ end
80
+
81
+ # Accepts a user object and returns true if both users are
82
+ # friends and the friendship has been accepted.
83
+ def is_friends_with?(friend)
84
+ self.friends.include?(friend)
85
+ end
86
+
87
+ # Accepts a user object and returns true if both users are
88
+ # friends but the friendship hasn't been accepted yet.
89
+ def is_pending_friends_with?(friend)
90
+ self.pending_friends.include?(friend)
91
+ end
92
+
93
+ # Accepts a user object and returns true if both users are
94
+ # friends regardless of acceptance.
95
+ def is_friends_or_pending_with?(friend)
96
+ self.pending_or_accepted_friends.include?(friend)
97
+ end
98
+
99
+ # Accepts a user object and creates a friendship request
100
+ # between both users.
101
+ def request_friendship_with(friend)
102
+ Friendship.create!(:friendshipped_by_me => self,
103
+ :friendshipped_for_me => friend) unless self.is_friends_or_pending_with?(friend) || self == friend
104
+ end
105
+
106
+ # Accepts a user object and updates an existing friendship to
107
+ # be accepted.
108
+ def accept_friendship_with(friend)
109
+ self.friendship(friend).update_attribute(:accepted_at, Time.now)
110
+ end
111
+
112
+ # Accepts a user object and deletes a friendship between both
113
+ # users.
114
+ def delete_friendship_with(friend)
115
+ self.friendship(friend).destroy if self.is_friends_or_pending_with?(friend)
116
+ end
117
+
118
+ # Accepts a user object and creates a friendship between both
119
+ # users. This method bypasses the request stage and makes both
120
+ # users friends without needing to be accepted.
121
+ def become_friends_with(friend)
122
+ unless self.is_friends_with?(friend)
123
+ unless self.is_pending_friends_with?(friend)
124
+ Friendship.create!(:friendshipped_by_me => self, :friendshipped_for_me => friend, :accepted_at => Time.now)
125
+ else
126
+ self.friendship(friend).update_attribute(:accepted_at, Time.now)
127
+ end
128
+ else
129
+ self.friendship(friend)
130
+ end
131
+ end
132
+
133
+ end
134
+ end
135
+ end
@@ -0,0 +1 @@
1
+ ActiveRecord::Base.send( :include, HasManyFriends::UserExtensions )
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class HasManyFriendsTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_many_friends
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Brian Haberer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-01 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple Models for managing simple friendship relationships.
23
+ email: bhaberer@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - Rakefile
32
+ - lib/has_many_friends.rb
33
+ - rails/init.rb
34
+ - test/has_many_friends_test.rb
35
+ - README
36
+ has_rdoc: true
37
+ homepage: http://weirdo513.org
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project: has_many_friends
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Friendships.
70
+ test_files: []
71
+