Interest
a gem to follow, follow request and block between any ActiveRecord models.
Installation
Add this line to your application's Gemfile:
gem 'interest'
And then execute:
$ bundle
Or install it yourself as:
$ gem install interest
Post installation
Install migrations and models
bin/rails generate interest
Migrate
bin/rake db:migrate
Usage
Setup
Create User and Blog model as one example.
class User < ActiveRecord::Base
interest
end
class Blog < ActiveRecord::Base
interest
end
user = User.create!
blog = Blog.create!
Follow
To follow,
following = user.follow!(blog)
returns Following
or raises Interest::Followable::Rejected
.
Otherwise,
following = user.follow(blog)
returns Following
or nil
.
Returned Following
is the following.
following.follower == user # => true
following.followee == blog # => true
following.status # => "accepted"
To unfollow,
user.unfollow(blog)
To check whether the user is following the blog or not,
user.following?(blog)
or
blog.followed_by?(user)
Associations
user.following_relationships.of(Blog)
and
blog.follower_relationships.of(User)
returns ActiveRecord::Associations::CollectionProxy
for Following
belongs to User
and Blog
.
user.following_blogs
returns ActiveRecord::Associations::CollectionProxy
for Blog
.
blog.follower_users
returns ActiveRecord::Associations::CollectionProxy
for User
.
"blogs" and "users" part of the above are conditional on a class.
e.g.
other_user = OtherUser.create!
user.follow(other_user) # follows OtherUser
then
user.following_other_users
(It's just like "ClassName".underscore.pluralize
)
Follow request
To request to follow,
following = user.request_to_follow!(blog)
returns Following
or raises Interest::FollowRequestable::Rejected
.
Otherwise,
following = user.request_to_follow!(blog)
returns Following
or nil
.
Returned Following
is the following.
following.status # => "pending"
To cancel request to follow,
user.cancel_request_to_follow(blog)
To check whether the user has requested to follow the blog or not,
user.has_requested_to_follow?(blog)
or
blog.has_been_requested_to_follow?(user)
To accept request to follow, call accept!
of Following
following = blog.incoming_follow_requests.of(User).find(id) # `Following.find(id)' is more simply.
following.accept!
Associations
user.outgoing_follow_requests.of(Blog)
and
blog.incoming_follow_requests.of(User)
returns ActiveRecord::Associations::CollectionProxy
for Following
belongs to User
and Blog
user.follow_requestee_blogs
returns ActiveRecord::Associations::CollectionProxy
for Blog
blog.follow_requester_users
returns ActiveRecord::Associations::CollectionProxy
for User
Note
follow!
and follow
methods don't check whether the user is required to request to follow the blog, so you should check it yourself.
In this case, you need to define (override) requires_request_to_follow?
on Blog
first.
class Blog < ActiveRecord::Base
# ...
def requires_request_to_follow?(follower)
true # or something
end
end
Then, like the following
if user.required_request_to_follow?(blog)
user.request_to_follow(blog)
else
user.follow(blog)
end
or
result = user.follow_or_request_to_follow!(blog)
if result.followed?
# when the user followed the blog
elsif result.requested_to_follow?
# when the user requested to follow the blog
end
Block
To block,
blocking = blog.block!(user)
returns Blocking
or raises Interest::Blockable::Rejected
.
Otherwise,
blocking = blog.block(user)
returns Blocking
or nil
Returned Blocking
is the following
blocking.blocker == blog # => true
blocking.blockee == user # => true
Blocking destroys their folow and follow request relationships if they have.
To unblock,
blog.unblock(user)
To check whether the blog is blocking the user or not,
blog.blocking?(user)
or
user.blocked_by?(blog)
Associations
blog.blocking_relationships.of(User)
and
user.blocker_relationships.of(Blog)
returns ActiveRecord::Associations::CollectionProxy
for Blocking
belongs to Blog
and User
blog.blocking_users
returns ActiveRecord::Associations::CollectionProxy
for User
user.blocker_blogs
returns ActiveRecord::Associations::CollectionProxy
for Blog
Contributing
- Fork it ( https://github.com/conol/interest/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request