act_as_buddy 1.0.0
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/LICENSE.txt +20 -0
- data/README.rdoc +112 -0
- data/lib/act_as_buddy.rb +8 -0
- data/lib/act_as_buddy/buddeable.rb +211 -0
- data/lib/act_as_buddy/railtie.rb +15 -0
- data/lib/act_as_buddy/utils.rb +27 -0
- data/lib/generators/USAGE +5 -0
- data/lib/generators/act_as_buddy_generator.rb +36 -0
- data/lib/generators/templates/migration.rb +17 -0
- data/lib/generators/templates/model.rb +11 -0
- data/test/dummy_hooks/after_app_generator.rb +21 -0
- data/test/test_buddy_functions.rb +112 -0
- data/test/test_helper.rb +14 -0
- metadata +228 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 kannancet
|
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,112 @@
|
|
1
|
+
= act_as_buddy
|
2
|
+
|
3
|
+
act_as_buddy is a gem to allow any model to implement self relation. For eg: the friendships of a user can be implemented easily by self relation technique of act_as_buddy.
|
4
|
+
Any Model can be buddied or lopped on to itself. Main uses would be for Users create friends etc..
|
5
|
+
Eg: User has many friends and friends are members of users table itself.
|
6
|
+
This is expected to be heavily useful in social networking domain.
|
7
|
+
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
=== The master branch supports rails 3
|
12
|
+
|
13
|
+
Add the gem to the gemfile:
|
14
|
+
gem "act_as_buddy"
|
15
|
+
|
16
|
+
Run the generator:
|
17
|
+
rails generate act_as_buddy
|
18
|
+
|
19
|
+
This will generate a migration file as well as a model called BuddyMapper.Then do:
|
20
|
+
rake db:migrate
|
21
|
+
|
22
|
+
This will migrate the buddy_mappers table to database.
|
23
|
+
|
24
|
+
=== Rails <=3.0.0 is NOT SUPPORTED
|
25
|
+
|
26
|
+
The gem version does not work with rails <=3.0.0. Sorry guys its right time you guys think about an upgrade ;)
|
27
|
+
|
28
|
+
|
29
|
+
== Usage
|
30
|
+
|
31
|
+
=== Setup
|
32
|
+
|
33
|
+
Make your model(s) that you want to allow to be buddied act_as_buddy, just add the mixin:
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
...
|
36
|
+
act_as_buddeable
|
37
|
+
...
|
38
|
+
end
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
=== act_as_buddy methods
|
43
|
+
|
44
|
+
To have an object start buddying another object of same model use the following:
|
45
|
+
user1 = User.first(1)
|
46
|
+
user2 = User.first(2)
|
47
|
+
user1.add_buddy(user2) # Creates a messaage of containing the id of buddied records.
|
48
|
+
|
49
|
+
To remove a buddy from buddy list
|
50
|
+
user1.remove_buddy(user2) # Returns a message saying that the object ids are un-buddied.
|
51
|
+
|
52
|
+
You can check to see if an object is buddied to other object of same type:
|
53
|
+
user1.is_a_buddy_of?(user2) # Returns true or false
|
54
|
+
|
55
|
+
To get the total number (count) of buddies of an object.
|
56
|
+
user1.get_buddy_count # Returns an integer
|
57
|
+
|
58
|
+
To get the actual list of all buddies of an object.
|
59
|
+
user1.fetch_all_buddies # Returns an array of all buddied objects
|
60
|
+
|
61
|
+
To find a buddy of an object with a field exactly matching a value
|
62
|
+
user1.find_buddies_with(:name => "ashik")
|
63
|
+
# Returns the buddied object of the parent object matching the field value exactly. Eg: friends of user1 with name='ashik'.
|
64
|
+
|
65
|
+
To find buddies of an object with a field matching a value like argument string.
|
66
|
+
user1.find_buddies_like(:name => "ash")
|
67
|
+
# Returns an array of all buddied objects of the parent object having name similar to field value. Eg: friends of user1 with name like 'ash'.result - [<#User3ashik>]
|
68
|
+
|
69
|
+
To add multiple buddies to the buddy list (or 'Bulk Buddying') of an object.
|
70
|
+
user1.add_multiple_buddies(args)
|
71
|
+
# args is an array of all buddies to be added. They should all the of same type as parent object.
|
72
|
+
|
73
|
+
To remove multiple buddies from the buddy list (or 'Bulk Buddy Removal') of an object.
|
74
|
+
user1.remove_multiple_buddies(args)
|
75
|
+
# args is an array of all buddies to be added. They should all the of same type as parent object.
|
76
|
+
|
77
|
+
To remove all buddies from the buddy list (or 'Bulk Buddy Removal') of an object.
|
78
|
+
user1.remove_all_buddies()
|
79
|
+
|
80
|
+
|
81
|
+
=== Testing the Gem
|
82
|
+
* Fork the project.
|
83
|
+
* Goto the root folder.
|
84
|
+
* Execute 'bundle install'.
|
85
|
+
* Execute 'bundle exec dummier'.
|
86
|
+
* This will create the test app and all dependencies for testing.
|
87
|
+
* Execute 'rake test' or simply 'rake'.
|
88
|
+
|
89
|
+
== Comments/Requests
|
90
|
+
|
91
|
+
If anyone has comments or questions please let me know (krxcet@gmail.com or kannanr@qburst.com).
|
92
|
+
If you have updates or patches or want to contribute I would love to see what you have or want to add.
|
93
|
+
|
94
|
+
|
95
|
+
== Note on Patches/Pull Requests
|
96
|
+
|
97
|
+
* Fork the project.
|
98
|
+
* Make your feature addition or bug fix.
|
99
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally (act_as_buddy uses Shoulda and Dummier)
|
100
|
+
* Send me a pull request. Bonus points for topic branches.
|
101
|
+
|
102
|
+
|
103
|
+
== Contributers
|
104
|
+
|
105
|
+
This list is open to all. You are all welcome :).
|
106
|
+
|
107
|
+
* kannacet (Kannan Reghu a.k.a 'Shrank') - https://github.com/kannancet
|
108
|
+
|
109
|
+
|
110
|
+
Copyright (c) 2013 kannancet(krxcet@gmail.com, kannanr@qburst.com). See LICENSE.txt for
|
111
|
+
further details.
|
112
|
+
|
data/lib/act_as_buddy.rb
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
=begin
|
2
|
+
This file contains the functions and classes for adding, removing, finding buddies.
|
3
|
+
The core logic is implemented here.
|
4
|
+
=end
|
5
|
+
module ActAsBuddy
|
6
|
+
module Buddeable
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
=begin
|
12
|
+
Class methods that add the relations to the class on which ac_as_buddy is called.
|
13
|
+
=end
|
14
|
+
module ClassMethods
|
15
|
+
def act_as_buddeable
|
16
|
+
has_many :buddy_mappers, :foreign_key => "buddeable_parent_id", :as => :buddeable
|
17
|
+
include ActAsBuddy::Buddeable::InstanceMethods
|
18
|
+
include ActAsBuddy::Util
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
=begin
|
23
|
+
Instance methods on the class on which act_as_buddy is defined are implemented here.
|
24
|
+
=end
|
25
|
+
module InstanceMethods
|
26
|
+
|
27
|
+
=begin
|
28
|
+
This function is used to add buddies to the given object.
|
29
|
+
=end
|
30
|
+
def add_buddy(*args)
|
31
|
+
couple_buddies, child_buddy = check_argument_pattern(*args)
|
32
|
+
build_buddy_collection([child_buddy])
|
33
|
+
p "#{self.class.to_s}s with id:#{self.id} and #{child_buddy.id} are now buddies."
|
34
|
+
end
|
35
|
+
|
36
|
+
=begin
|
37
|
+
This function is used to remove buddies to the given object.
|
38
|
+
=end
|
39
|
+
def remove_buddy(*args)
|
40
|
+
couple_buddies, child_buddy = check_argument_pattern(*args)
|
41
|
+
couple_buddies.map(&:destroy)
|
42
|
+
p "#{self.class.to_s}s with id:#{self.id} and #{child_buddy.id} are now un-buddied."
|
43
|
+
end
|
44
|
+
|
45
|
+
=begin
|
46
|
+
This function is used to get all buddies of an object.
|
47
|
+
=end
|
48
|
+
def fetch_all_buddies
|
49
|
+
self.class.joins(:buddy_mappers).where("buddy_mappers.buddeable_child_id=?", self.id)
|
50
|
+
end
|
51
|
+
|
52
|
+
=begin
|
53
|
+
This function is used to get the count of all buddies.
|
54
|
+
=end
|
55
|
+
def get_buddy_count
|
56
|
+
self.class.joins(:buddy_mappers).where("buddy_mappers.buddeable_child_id=?", self.id).size
|
57
|
+
end
|
58
|
+
|
59
|
+
=begin
|
60
|
+
This function is used to destroy all
|
61
|
+
=end
|
62
|
+
def remove_all_buddies
|
63
|
+
BuddyMapper.delete_all("buddeable_parent_id=#{self.id} OR buddeable_child_id=#{self.id} AND buddeable_type='#{self.class.to_s}'")
|
64
|
+
end
|
65
|
+
|
66
|
+
=begin
|
67
|
+
This function is used to add bulk number of buddies.
|
68
|
+
=end
|
69
|
+
def add_multiple_buddies(*args)
|
70
|
+
if args.blank?
|
71
|
+
raise "Argument empty!Add buddy needs an argument of same type which invokes the method."
|
72
|
+
return
|
73
|
+
end
|
74
|
+
buddy_collection = args.first
|
75
|
+
if buddy_collection.collect(&:id).include?(self.id)
|
76
|
+
raise "Loop association error! Cannot associate a record to itself."
|
77
|
+
return
|
78
|
+
end
|
79
|
+
unless buddy_collection.is_a?(Array) || (buddy_collection.select{|elmnt| elmnt.class != self.class}).blank?
|
80
|
+
raise "Argument type mismatch! Argument should be an array of objects of same type and of the type same class which invokes the function."
|
81
|
+
return
|
82
|
+
end
|
83
|
+
build_buddy_collection(buddy_collection)
|
84
|
+
end
|
85
|
+
|
86
|
+
=begin
|
87
|
+
This function is used to delete bulk number of buddies.
|
88
|
+
=end
|
89
|
+
def remove_multiple_buddies(*args)
|
90
|
+
if args.blank?
|
91
|
+
raise "Argument empty!Add buddy needs an argument of same type which invokes the method."
|
92
|
+
return
|
93
|
+
end
|
94
|
+
buddy_collection = args.first
|
95
|
+
if buddy_collection.collect(&:id).include?(self.id)
|
96
|
+
raise "Loop association error! Cannot associate a record to itself."
|
97
|
+
return
|
98
|
+
end
|
99
|
+
unless buddy_collection.is_a?(Array) || (buddy_collection.select{|elmnt| elmnt.class != self.class}).blank?
|
100
|
+
raise "Argument type mismatch! Argument should be an array of objects of same type and of the type same class which invokes the function."
|
101
|
+
return
|
102
|
+
end
|
103
|
+
associated_buddy_mappers = BuddyMapper.where("(buddeable_parent_id=#{self.id} AND buddeable_child_id in (?)) OR ((buddeable_child_id=#{self.id} AND buddeable_parent_id in (?)))", buddy_collection.collect(&:id), buddy_collection.collect(&:id))
|
104
|
+
associated_buddy_mappers.destroy_all
|
105
|
+
end
|
106
|
+
|
107
|
+
=begin
|
108
|
+
This function is used to find the buddies of a parent by conditions.
|
109
|
+
=end
|
110
|
+
def find_buddies_with(params = {})
|
111
|
+
(raise "Argument cannot be blank." and return) if params.empty?
|
112
|
+
(raise "Argument needs to be a hash." and return) unless params.is_a?(Hash)
|
113
|
+
self.class.joins(:buddy_mappers).where("buddy_mappers.buddeable_child_id=? AND #{self.class.table_name}.#{params.keys.first.to_s}=?", self.id, params.values.first.to_s).first
|
114
|
+
end
|
115
|
+
|
116
|
+
=begin
|
117
|
+
This function is used to find the buddies of a parent by with like conditions.
|
118
|
+
=end
|
119
|
+
def find_buddies_like(params = {})
|
120
|
+
(raise "Argument cannot be blank." and return) if params.empty?
|
121
|
+
(raise "Argument needs to be a hash." and return) unless params.is_a?(Hash)
|
122
|
+
self.class.joins(:buddy_mappers).where("buddy_mappers.buddeable_child_id=? AND #{self.class.table_name}.#{params.keys.first.to_s} like ?", self.id, "%#{params.values.first.to_s}%")
|
123
|
+
end
|
124
|
+
|
125
|
+
=begin
|
126
|
+
This function is used to test if two objects are buddied to each other.
|
127
|
+
=end
|
128
|
+
def is_a_buddy_of?(*args)
|
129
|
+
if args.blank?
|
130
|
+
raise "Argument empty!Add buddy needs an argument of same type which invokes the method."
|
131
|
+
return
|
132
|
+
end
|
133
|
+
child_buddy = args.first
|
134
|
+
unless child_buddy.is_a?(Object)
|
135
|
+
raise "Argument mismatch! Argument needs to be an Object."
|
136
|
+
return
|
137
|
+
end
|
138
|
+
check_buddy = BuddyMapper.where("(buddeable_parent_id=#{self.id} AND buddeable_child_id=#{child_buddy.id}) OR (buddeable_parent_id=#{child_buddy.id} AND buddeable_child_id=#{self.id})")
|
139
|
+
check_buddy.blank? ? false : true
|
140
|
+
end
|
141
|
+
|
142
|
+
=begin
|
143
|
+
Private functions begin here.
|
144
|
+
=end
|
145
|
+
private
|
146
|
+
|
147
|
+
=begin
|
148
|
+
This function is used to check the argument type and render errors.
|
149
|
+
=end
|
150
|
+
def check_argument_pattern(*args)
|
151
|
+
if args.blank?
|
152
|
+
raise "Argument empty!Add buddy needs an argument of same type which invokes the method."
|
153
|
+
return
|
154
|
+
end
|
155
|
+
child_buddy = args.first
|
156
|
+
unless child_buddy.is_a?(Object)
|
157
|
+
raise "Argument mismatch! Argument needs to be an Object."
|
158
|
+
return
|
159
|
+
end
|
160
|
+
if child_buddy.class != self.class
|
161
|
+
raise "Argument mismatch! Buddy can associate only objects of same type."
|
162
|
+
return
|
163
|
+
end
|
164
|
+
if child_buddy.id == self.id
|
165
|
+
raise "Loop association error! Cannot associate a record to itself."
|
166
|
+
return
|
167
|
+
end
|
168
|
+
couple_buddies = find_couple_buddies(child_buddy)
|
169
|
+
unless couple_buddies.blank?
|
170
|
+
if caller[0].include? "add_buddy"
|
171
|
+
raise "This buddy is already associated."
|
172
|
+
return
|
173
|
+
end
|
174
|
+
else
|
175
|
+
if caller[0].include? "remove_buddy"
|
176
|
+
raise "These buddies are not associated yet."
|
177
|
+
return
|
178
|
+
end
|
179
|
+
end
|
180
|
+
return couple_buddies, child_buddy
|
181
|
+
end
|
182
|
+
|
183
|
+
=begin
|
184
|
+
This function is used to find the couple buddies given two records.
|
185
|
+
=end
|
186
|
+
def find_couple_buddies(child_buddy)
|
187
|
+
BuddyMapper.where("(buddeable_child_id=? AND buddeable_parent_id=?) OR (buddeable_child_id=? AND buddeable_parent_id=?) AND buddeable_type=?", child_buddy.id, self.id, self.id, child_buddy.id, self.class.to_s)
|
188
|
+
end
|
189
|
+
|
190
|
+
=begin
|
191
|
+
This function is used to build the buddy records.
|
192
|
+
This function accepts a acollection of buddies and builds the records.
|
193
|
+
=end
|
194
|
+
def build_buddy_collection(*args)
|
195
|
+
buddy_collection = args.first
|
196
|
+
buddy_collection.each do |buddy|
|
197
|
+
parent, child = self.id, buddy.id
|
198
|
+
BuddyMapper.transaction do
|
199
|
+
2.times do
|
200
|
+
BuddyMapper.create(:buddeable_parent_id => parent,
|
201
|
+
:buddeable_child_id => child,
|
202
|
+
:buddeable_type => self.class.to_s)
|
203
|
+
parent, child = child, parent
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
=begin
|
2
|
+
This module defines a class a communicate to Railtie and add a middle ware.
|
3
|
+
=end
|
4
|
+
require 'act_as_buddy'
|
5
|
+
require 'rails'
|
6
|
+
|
7
|
+
module ActAsBuddy
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
initializer 'act_as_buddy.initialize' do
|
10
|
+
ActiveSupport.on_load(:active_record) do
|
11
|
+
include ActAsBuddy::Buddeable
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
=begin
|
2
|
+
All the module level utility functions are defined here.
|
3
|
+
=end
|
4
|
+
module ActAsBuddy
|
5
|
+
|
6
|
+
=begin
|
7
|
+
This defines the basic getter and setter methods on module level.
|
8
|
+
=end
|
9
|
+
def self.buddy_attr_accessor(*args)
|
10
|
+
args.each do |arg|
|
11
|
+
self.class_eval("def self.#{arg};@@#{arg};end")
|
12
|
+
self.class_eval("def self.#{arg}=(val);@@#{arg}=val;end")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Util
|
17
|
+
=begin
|
18
|
+
Retrieves the parent class name if using STI.
|
19
|
+
=end
|
20
|
+
def parent_class_name(obj)
|
21
|
+
if obj.class.superclass != ActiveRecord::Base
|
22
|
+
return obj.class.superclass.name
|
23
|
+
end
|
24
|
+
return obj.class.name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
=begin
|
2
|
+
This class implements the generator creation for creating the models and migrations.
|
3
|
+
These are needed for proper functioning of gem.
|
4
|
+
=end
|
5
|
+
require 'rails/generators'
|
6
|
+
require 'rails/generators/migration'
|
7
|
+
|
8
|
+
class ActAsBuddyGenerator < Rails::Generators::Base
|
9
|
+
|
10
|
+
include Rails::Generators::Migration
|
11
|
+
|
12
|
+
def self.source_root
|
13
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
14
|
+
end
|
15
|
+
|
16
|
+
=begin
|
17
|
+
Implement the required interface for Rails::Generators::Migration.
|
18
|
+
taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
19
|
+
=end
|
20
|
+
def self.next_migration_number(dirname)
|
21
|
+
if ActiveRecord::Base.timestamped_migrations
|
22
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
23
|
+
else
|
24
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_migration_file
|
29
|
+
migration_template 'migration.rb', 'db/migrate/act_as_buddy_migration.rb'
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_model
|
33
|
+
template "model.rb", File.join('app/models', "buddy_mapper.rb")
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ActAsBuddyMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :buddy_mappers, :force => true do |t|
|
4
|
+
t.string :buddeable_type
|
5
|
+
t.integer :buddeable_parent_id
|
6
|
+
t.integer :buddeable_child_id
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :buddy_mappers, ["buddeable_parent_id"], :name => "index_buddeable_parent"
|
11
|
+
add_index :buddy_mappers, ["buddeable_child_id"], :name => "index_buddeable_child"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :buddy_mappers
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
run "rails g scaffold user name:string"
|
2
|
+
|
3
|
+
gsub_file "app/models/user.rb", "end", %(
|
4
|
+
act_as_buddeable
|
5
|
+
|
6
|
+
end)
|
7
|
+
run "rails g model BuddyMapper buddeable_child_id:integer buddeable_parent_id:integer buddeable_type:string"
|
8
|
+
|
9
|
+
gsub_file "app/models/buddy_mapper.rb", "end", %(
|
10
|
+
include ActiveModel::MassAssignmentSecurity
|
11
|
+
attr_accessible :id,
|
12
|
+
:buddeable_child_id,
|
13
|
+
:buddeable_parent_id,
|
14
|
+
:buddeable_type,
|
15
|
+
:created_at,
|
16
|
+
:updated_at
|
17
|
+
belongs_to :buddeable, :polymorphic => true
|
18
|
+
|
19
|
+
end)
|
20
|
+
run "rake db:create"
|
21
|
+
run "rake db:migrate"
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class FunctionTest < ActiveSupport::TestCase
|
4
|
+
USERS = ['kannan', 'rahul', 'ashik', 'sunil', 'arun', 'jojith']
|
5
|
+
|
6
|
+
context "General - " do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
BuddyMapper.delete_all
|
10
|
+
USERS.collect do |user|
|
11
|
+
created_user = User.find_or_create_by_name(user)
|
12
|
+
instance_variable_set('@'+user, created_user)
|
13
|
+
end
|
14
|
+
@kannan.add_buddy(@sunil)
|
15
|
+
@kannan.add_buddy(@ashik)
|
16
|
+
end
|
17
|
+
|
18
|
+
context "All buddy records" do
|
19
|
+
|
20
|
+
should "define act_as_buddy instance methods" do
|
21
|
+
assert @kannan.respond_to?(:add_buddy)
|
22
|
+
assert @kannan.respond_to?(:remove_buddy)
|
23
|
+
assert @kannan.respond_to?(:fetch_all_buddies)
|
24
|
+
assert @kannan.respond_to?(:remove_all_buddies)
|
25
|
+
assert @kannan.respond_to?(:add_multiple_buddies)
|
26
|
+
assert @kannan.respond_to?(:find_buddies_with)
|
27
|
+
assert @kannan.respond_to?(:find_buddies_like)
|
28
|
+
assert @kannan.respond_to?(:is_a_buddy_of?)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context "Testing add_buddy method - " do
|
34
|
+
|
35
|
+
should "return true for correct buddy mapping" do
|
36
|
+
assert @kannan.is_a_buddy_of?(@sunil)
|
37
|
+
assert @kannan.is_a_buddy_of?(@ashik)
|
38
|
+
assert @ashik.is_a_buddy_of?(@kannan)
|
39
|
+
assert @sunil.is_a_buddy_of?(@kannan)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "return false for incorrect buddy mapping" do
|
43
|
+
assert "false", @rahul.is_a_buddy_of?(@kannan).to_s
|
44
|
+
assert "false", @sunil.is_a_buddy_of?(@ashik).to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
context "Testing fetch_all_buddies - " do
|
50
|
+
|
51
|
+
should "return buddy count " do
|
52
|
+
assert "2", @kannan.fetch_all_buddies.size.to_s
|
53
|
+
assert "1", @sunil.fetch_all_buddies.size.to_s
|
54
|
+
assert "1", @ashik.fetch_all_buddies.size.to_s
|
55
|
+
assert "0", @rahul.fetch_all_buddies.size.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
context "Testing add_multiple buddies - " do
|
61
|
+
|
62
|
+
setup do
|
63
|
+
@kannan.add_multiple_buddies([@jojith, @rahul])
|
64
|
+
end
|
65
|
+
|
66
|
+
should "return buddy count " do
|
67
|
+
assert "4", @kannan.fetch_all_buddies.size.to_s
|
68
|
+
assert "1", @jojith.fetch_all_buddies.size.to_s
|
69
|
+
assert "1", @rahul.fetch_all_buddies.size.to_s
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
context "Testing remove_multiple_buddies - " do
|
75
|
+
|
76
|
+
setup do
|
77
|
+
@kannan.remove_multiple_buddies([@jojith, @rahul])
|
78
|
+
end
|
79
|
+
|
80
|
+
should "return buddy count " do
|
81
|
+
assert "2", @kannan.fetch_all_buddies.size.to_s
|
82
|
+
assert "0", @jojith.fetch_all_buddies.size.to_s
|
83
|
+
assert "0", @rahul.fetch_all_buddies.size.to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
context "Testing find_buddies_with and find_buddies_like- " do
|
89
|
+
|
90
|
+
should "return buddy name " do
|
91
|
+
assert "sunil", @kannan.find_buddies_with(:name => 'sunil').name
|
92
|
+
assert "sunil", @kannan.find_buddies_like(:name => 'sun').first.name
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
context "Testing remove_all_buddies - " do
|
98
|
+
|
99
|
+
setup do
|
100
|
+
@kannan.remove_all_buddies
|
101
|
+
end
|
102
|
+
|
103
|
+
should "return buddy count " do
|
104
|
+
assert "0", @kannan.fetch_all_buddies.size.to_s
|
105
|
+
assert "0", @sunil.fetch_all_buddies.size.to_s
|
106
|
+
assert "0", @ashik.fetch_all_buddies.size.to_s
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
#Configure Rails Environment
|
3
|
+
ENV["RAILS_ENV"] = "test"
|
4
|
+
|
5
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
6
|
+
require "rails/test_help"
|
7
|
+
|
8
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
|
9
|
+
ActiveRecord::Migration.verbose = false
|
10
|
+
|
11
|
+
|
12
|
+
require File.dirname(__FILE__) + '/../lib/generators/templates/model.rb'
|
13
|
+
|
14
|
+
require 'shoulda'
|
metadata
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: act_as_buddy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- kannancet
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: shoulda
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rdoc
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.12'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.2.3
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.2.3
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: jeweler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.8.4
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.8.4
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rcov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - '='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 0.9.11
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - '='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.9.11
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rake
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: activerecord
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 3.1.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 3.1.0
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: rails
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: dummier
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 0.3.2
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ! '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 0.3.2
|
158
|
+
- !ruby/object:Gem::Dependency
|
159
|
+
name: sqlite3
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ! '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
type: :development
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ! '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
description: This gem is used to implement self relation in a rails project for any
|
175
|
+
table. The gem will automatically manage self relation implementation on mumtiple
|
176
|
+
tables.
|
177
|
+
email: krxcet@gmail.com
|
178
|
+
executables: []
|
179
|
+
extensions: []
|
180
|
+
extra_rdoc_files:
|
181
|
+
- LICENSE.txt
|
182
|
+
- README.rdoc
|
183
|
+
files:
|
184
|
+
- lib/act_as_buddy.rb
|
185
|
+
- lib/act_as_buddy/buddeable.rb
|
186
|
+
- lib/act_as_buddy/railtie.rb
|
187
|
+
- lib/act_as_buddy/utils.rb
|
188
|
+
- lib/generators/USAGE
|
189
|
+
- lib/generators/act_as_buddy_generator.rb
|
190
|
+
- lib/generators/templates/migration.rb
|
191
|
+
- lib/generators/templates/model.rb
|
192
|
+
- LICENSE.txt
|
193
|
+
- README.rdoc
|
194
|
+
- test/test_helper.rb
|
195
|
+
- test/test_buddy_functions.rb
|
196
|
+
- test/dummy_hooks/after_app_generator.rb
|
197
|
+
homepage: http://github.com/kannancet/act_as_buddy
|
198
|
+
licenses:
|
199
|
+
- BSD
|
200
|
+
post_install_message:
|
201
|
+
rdoc_options: []
|
202
|
+
require_paths:
|
203
|
+
- lib
|
204
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
205
|
+
none: false
|
206
|
+
requirements:
|
207
|
+
- - ! '>='
|
208
|
+
- !ruby/object:Gem::Version
|
209
|
+
version: '0'
|
210
|
+
segments:
|
211
|
+
- 0
|
212
|
+
hash: -175955441
|
213
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
|
+
none: false
|
215
|
+
requirements:
|
216
|
+
- - ! '>='
|
217
|
+
- !ruby/object:Gem::Version
|
218
|
+
version: '0'
|
219
|
+
requirements: []
|
220
|
+
rubyforge_project:
|
221
|
+
rubygems_version: 1.8.24
|
222
|
+
signing_key:
|
223
|
+
specification_version: 3
|
224
|
+
summary: A gem to implement self relation on any table.
|
225
|
+
test_files:
|
226
|
+
- test/test_helper.rb
|
227
|
+
- test/test_buddy_functions.rb
|
228
|
+
- test/dummy_hooks/after_app_generator.rb
|