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. Any Model can be buddied or lopped on to itself. Main uses would be for Users create friends etc.. Eg: User has many friends and friends are members of users table itself. This is expected to be heavily useful in social networking domain.
The master branch supports rails 3
Add the gem to the gemfile:
Run the generator:
rails generate act_as_buddy
This will generate a migration file as well as a model called BuddyMapper.Then do: rake db:migrate This will migrate the buddy_mappers table to database.
Rails <=3.0.0 is NOT SUPPORTED
The gem version does not work with rails <=3.0.0. Sorry guys its right time you guys think about an upgrade ;)
Make your model(s) that you want to allow to be buddied act_as_buddy, just add the mixin:
class User < ActiveRecord::Base ... act_as_buddeable ... end
To have an object start buddying another object of same model use the following:
user1 = User.first(1) user2 = User.first(2) user1.add_buddy(user2) # Creates a messaage of containing the id of buddied records.
To remove a buddy from buddy list
user1.remove_buddy(user2) # Returns a message saying that the object ids are un-buddied.
You can check to see if an object is buddied to other object of same type:
user1.is_a_buddy_of?(user2) # Returns true or false
To get the total number (count) of buddies of an object.
user1.get_buddy_count # Returns an integer
To get the actual list of all buddies of an object.
user1.fetch_all_buddies # Returns an array of all buddied objects
To find a buddy of an object with a field exactly matching a value
user1.find_buddies_with(:name => "ashik") # Returns the buddied object of the parent object matching the field value exactly. Eg: friends of user1 with name='ashik'.
To find buddies of an object with a field matching a value like argument string.
user1.find_buddies_like(:name => "ash") # 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>]
To add multiple buddies to the buddy list (or 'Bulk Buddying') of an object.
user1.add_multiple_buddies(args) # args is an array of all buddies to be added. They should all the of same type as parent object.
To remove multiple buddies from the buddy list (or 'Bulk Buddy Removal') of an object.
user1.remove_multiple_buddies(args) # args is an array of all buddies to be added. They should all the of same type as parent object.
To remove all buddies from the buddy list (or 'Bulk Buddy Removal') of an object.
Testing the Gem
Fork the project.
Goto the root folder.
Execute 'bundle install'.
Execute 'bundle exec dummier'.
This will create the test app and all dependencies for testing.
Execute 'rake test' or simply 'rake'.
If anyone has comments or questions please let me know (firstname.lastname@example.org or email@example.com). If you have updates or patches or want to contribute I would love to see what you have or want to add.
Note on Patches/Pull Requests
Fork the project.
Make your feature addition or bug fix.
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)
Send me a pull request. Bonus points for topic branches.
This list is open to all. You are all welcome :).
kannacet (Kannan Reghu a.k.a 'Shrank') -
Copyright © 2013 kannancet(firstname.lastname@example.org, email@example.com). See LICENSE.txt for further details.